From cb2905b605f65039d8e20b53a35ae412c12a9a11 Mon Sep 17 00:00:00 2001 From: Alessia Bellisario Date: Wed, 3 Jan 2024 15:38:40 -0500 Subject: [PATCH 1/2] chore: add docs for skipping an optimistic update via optimisticResponse --- docs/source/performance/optimistic-ui.mdx | 45 +++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/docs/source/performance/optimistic-ui.mdx b/docs/source/performance/optimistic-ui.mdx index 35332dce9e2..078447dec7a 100644 --- a/docs/source/performance/optimistic-ui.mdx +++ b/docs/source/performance/optimistic-ui.mdx @@ -73,6 +73,51 @@ As this example shows, the value of `optimisticResponse` is an object that match 5. Apollo Client notifies all affected queries again. The associated components re-render, but if the server's response matches our `optimisticResponse`, this is invisible to the user. +## Bailing out of an optimistic update + +In some cases you may want to skip an optimisitic update. For example, you may want to perform an optimistic update _only_ when certain variables are passed to the mutation. In order to skip an update, you can pass a function to `optimisticResponse` and use the `IGNORE` sentinel object available on the second argument passed to your `optimisticResponse` function to bail out of the optimistic update. + +Consider this example: + +```tsx +const UPDATE_COMMENT = gql` + mutation UpdateComment($commentId: ID!, $commentContent: String!) { + updateComment(commentId: $commentId, content: $commentContent) { + id + __typename + content + } + } +`; + +function CommentPageWithData() { + const [mutate] = useMutation(UPDATE_COMMENT); + + return ( + + mutate({ + variables: { commentId, commentContent }, + optimisticResponse: (vars, { IGNORE }) => { + if (commentContent === "foo") { + // conditionally bail out of optimistic updates + return IGNORE; + } + return { + updateComment: { + id: commentId, + __typename: "Comment", + content: commentContent + } + } + }, + }) + } + /> + ); +} +``` + ## Example: Adding a new object to a list The previous example shows how to provide an optimistic result for an object that's _already_ in the Apollo Client cache. But what about a mutation that creates a _new_ object? This works similarly. From 83cc8331e17556e7424c843647893bd243a638d4 Mon Sep 17 00:00:00 2001 From: Alessia Bellisario Date: Wed, 3 Jan 2024 16:48:58 -0500 Subject: [PATCH 2/2] chore: make PR review updates --- docs/source/performance/optimistic-ui.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/performance/optimistic-ui.mdx b/docs/source/performance/optimistic-ui.mdx index 078447dec7a..a6b15b48efd 100644 --- a/docs/source/performance/optimistic-ui.mdx +++ b/docs/source/performance/optimistic-ui.mdx @@ -75,7 +75,7 @@ As this example shows, the value of `optimisticResponse` is an object that match ## Bailing out of an optimistic update -In some cases you may want to skip an optimisitic update. For example, you may want to perform an optimistic update _only_ when certain variables are passed to the mutation. In order to skip an update, you can pass a function to `optimisticResponse` and use the `IGNORE` sentinel object available on the second argument passed to your `optimisticResponse` function to bail out of the optimistic update. +In some cases you may want to skip an optimistic update. For example, you may want to perform an optimistic update _only_ when certain variables are passed to the mutation. To skip an update, pass a function to the `optimisticResponse` option and return the `IGNORE` sentinel object available on the second argument to bail out of the optimistic update. Consider this example: