Replies: 5 comments 2 replies
-
I assume you're referring to Graphcache's optimistic mutation? those indeed complete synchronously so something's definitely off here. In most cases, you're probably missing a field in your optimistic configuration that's present in your selection set. In that case you may see a warning in the console. But it's extremely important to provide it enough data to fulfil the mutation itself (and to not update other queries subsequently with missing / partial data that you want to also update optimistically) |
Beta Was this translation helpful? Give feedback.
-
I'm facing what I believe to be the same issue. Here's an example akin to my setup: type Post {
id: ID!
name: String!
}
input InputPost {
id: ID!
name: String!
}
type Mutation {
updatePost($post: InputPost!): Post!
}
fragment FullPost on Post {
id
name
}
mutation UpdatePost($post: InputPost!) {
updatePost(post: $InputPost) {
...FullPost
}
} // Set as cacheExchange.optimistic.updatePost
function updatePostOptimistic(
args: UpdatePostMutationVariables
): FullPostFragment {
return {
__typename: "Post",
...args.post,
};
} const [result, executeUpdatePost] =
useMutation(UpdatePostDocument);
useEffect(async () => {
const result = await executeUpdatePost({ variables: { post: { id: "random-uuid", name: "Hello" } } });
// This will only hit once the server has returned a result, *after* the optimistic update has been applied.
}, []); I can see the optimistic update being applied live, but I'm not seeing any warnings in the console. |
Beta Was this translation helpful? Give feedback.
-
I have the same request, it would be very helpful if you can choose to not let the promise wait for the server to respond. |
Beta Was this translation helpful? Give feedback.
-
if the results of optimistic mutations don't surface themselves anywhere, what is the point of them? |
Beta Was this translation helpful? Give feedback.
-
There are several reasons why this is the case, but essentially we thought through this and the best way to achieve optimistic mutations while catering to an average app’s need is to still resolve and block on the mutation that’s executed on the API. First of all, the main principle here is that optimistic mutations are inherently treated as unreliable.
Furthermore, because But, from a UI bindings standpoint, resolving immediately is unexpected for optimistic updates because it prevents us from representing a state. For example, let's say we have a mutation that does something important that alters multiple pieces of data on the current page. If we resolved immediately, you'd lose the ability to actually indicate to the user that this action has been committed, i.e. sent to the API. Also, from a standpoint of the optimistic update, any locally resolved value is one that is mostly based on local data already. In other words, the API of Graphcache encourages you to only write optimistic updates for mutations that have all their inputs and outputs known at the call site. This means that you can always treat the mutation as a fire-and-forget if you must (and use |
Beta Was this translation helpful? Give feedback.
-
Hello,
When applying an optimistic result on a mutation, the promise returned by the
executeMutation
callback does not trigger until the API responds :Wouldn't it be nice to be able to react to a mutation's optimistic answer ?
(There might already be a way to do it but I couldn't find it in the docs)
Regards,
Beta Was this translation helpful? Give feedback.
All reactions