T
TanStack16mo ago
national-gold

Mutation context in offline mode

Hi, does anyone know if it is possible to access a mutation context without it being resolved yet (offline mode). I'm creating temporaries ids on my onMutate callbacks that i then put on my query context (offline mode) and i'd like to access it even though onSuccess / onError / onSetteled have being called as I'm offline. This is how my mutatinon function looks like, it is made to handle offline mode
const mutation = useMutation({
mutationFn: (inventory: Inventory) => createInventory(org_slug, inventory),
onMutate: async (inventory: Inventory) => {
//optimistic insert with temp_id
const temp_id = await optimisticInsertList(queryClient, ['orgs', org_slug, 'inventories'], inventory)

return { temp_id };
},
onSettled: () => {
queryClient.invalidateQueries({ queryKey: ['orgs', org_slug, 'inventories'] });
},
onSuccess: async (data, variables, { temp_id }) => {
// update state with new id
await AsyncStorage.setItem(temp_id, data.id.toString());
console.log('temp_id was set to', temp_id, 'and the new id is', data.id);
optimisticUpdateList(queryClient, ['orgs', org_slug, 'inventories'], data, temp_id);
},
mutationKey: ['orgs', org_slug, 'inventories'],
})
const mutation = useMutation({
mutationFn: (inventory: Inventory) => createInventory(org_slug, inventory),
onMutate: async (inventory: Inventory) => {
//optimistic insert with temp_id
const temp_id = await optimisticInsertList(queryClient, ['orgs', org_slug, 'inventories'], inventory)

return { temp_id };
},
onSettled: () => {
queryClient.invalidateQueries({ queryKey: ['orgs', org_slug, 'inventories'] });
},
onSuccess: async (data, variables, { temp_id }) => {
// update state with new id
await AsyncStorage.setItem(temp_id, data.id.toString());
console.log('temp_id was set to', temp_id, 'and the new id is', data.id);
optimisticUpdateList(queryClient, ['orgs', org_slug, 'inventories'], data, temp_id);
},
mutationKey: ['orgs', org_slug, 'inventories'],
})
What I would like is to be abble to call mutation.mutate and get the temp_id. Anyone has an idea of how this could be achieved ? Thanks !
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?