-
Hi! I'm fairly new to urql so I apologise in advance if I'm missing something obvious. I set up a client with an offline exchange offlineExchange({
storage, // <--- I created this using AsyncStorage
schema,
updates: {
Mutation: {
createRecommendation: (result, args, cache, info) => {
// uses cache.updateQuery to update a "RECOMMENDATIONS" query
},
},
},
optimistic: {
createRecommendation: (variables, cache, info) => {
return {
__typename: 'Recommendation',
id: String(Math.random()), // we don't know what ID will be assigned by the API so creates a random one
};
},
},
}); My idea would be the following: when the user creates a recommendation, and the device is offline, they are able to proceed to a "recommendation detail" screen where they can see the newly-created recommendation (which would be filled by the custom updater starting from the arguments provided to the mutation). I have a React component that uses the mutation const [createRecommendationResult, createRecommendation] = useMutation(CREATE_RECOMMENDATION_MUTATION);
createRecommendation({
// rec data
}).then(
({ error, data }) => {
// navigate to recommendation details screen
}
); The problem I have is that Looking at the following code my understanding is that the result of the unsuccessful fetch is filtered out (and scheduled for synchronisation when the device gets back online) if the mutation is optimistic What I don't understand is how the result from the optimistic mutation would "pipe through" back to the client's I'm using these packages
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
So that's expected behaviour (which I suppose we need to highlight more aggressively once we fill out our Offline docs page with more templates and details) The optimistic mutations aren't guaranteed to resolve when you're in offline mode. Conceptually you can think of what Graphcache does with optimistic updates as a barrier: We never want optimistic updates to pollute real data. That means it's not persisted (and instead re-created on startup), the optimistic results are never written to the permanent (non-optimistic) data, queries will never destroy optimistic results (instead they're paused while optimistic mutations are in-flight), and lastly optimistic results are never returned to you when you're offline. This is to encourage you to not actively use optimistic results actively in your code. Instead you can assume that an optimistic mutation is executed immediately and should assume that it has succeeded. This is also to mirror the non-offline behaviour: When you're not offline you won't see the optimistic results for mutations either. So tl;dr the promise in your So what to do? In that case you can rely on any related query updating; Our assumption is that optimistic mutations should only be used for non-critical mutations and not creations, as it's hard to guarantee or know when the actual mutation will go through. In extreme cases of the user closing the app while offline it could even be days later. I'll check in and see what our recommendation is in that case, but it's a tricky one to rely on offline for. |
Beta Was this translation helpful? Give feedback.
-
@kitten does this mean any mutation in offline mode can't succeed? I had the understanding of the documentation if the use of
Would it be otherwise possible to change the I would love to see an offline-first approach demo repo to get a better understanding how |
Beta Was this translation helpful? Give feedback.
So that's expected behaviour (which I suppose we need to highlight more aggressively once we fill out our Offline docs page with more templates and details)
The optimistic mutations aren't guaranteed to resolve when you're in offline mode. Conceptually you can think of what Graphcache does with optimistic updates as a barrier: We never want optimistic updates to pollute real data. That means it's not persisted (and instead re-created on startup), the optimistic results are never written to the permanent (non-optimistic) data, queries will never destroy optimistic results (instead they're paused while optimistic mutations are in-flight), and lastly optimistic results are never returned to …