Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

optimisticResponse vs update in Apollo Client?

I want to use optimistic UI updates after a mutation: https://www.apollographql.com/docs/react/basics/mutations.html

Im confused about the relationship between 'optimisticResponse' and 'update'.

Here optimisticResponse is used:

    const CommentPageWithData = graphql(submitComment, {
      props: ({ ownProps, mutate }) => ({
        submit: ({ repoFullName, commentContent }) => mutate({
          variables: { repoFullName, commentContent },

          optimisticResponse: {
            __typename: 'Mutation',
            submitComment: {
              __typename: 'Comment',
              // Note that we can access the props of the container at `ownProps` if we
              // need that information to compute the optimistic response
              postedBy: ownProps.currentUser,
              createdAt: +new Date,
              content: commentContent,
            },
          },
        }),
      }),
    })(CommentPage);

Will just using this update the store?

The documentation then says this is used to update the cache:

    const text = 'Hello, world!';

    client.mutate({
      mutation: TodoCreateMutation,
      variables: {
        text,
      },
      update: (proxy, { data: { createTodo } }) => {
        // Read the data from our cache for this query.
        const data = proxy.readQuery({ query: TodoAppQuery });

        // Add our todo from the mutation to the end.
        data.todos.push(createTodo);

        // Write our data back to the cache.
        proxy.writeQuery({ query: TodoAppQuery, data });
      },
    });

This is what I've used to successfully update the UI without using the optimisticResponse function.

What is the difference between the two? Should you use both or just one?

like image 498
Evanss Avatar asked Mar 09 '18 04:03

Evanss


People also ask

What is Optimisticresponse?

Optimistic UI is a pattern that you can use to simulate the results of a mutation and update the UI even before receiving a response from the server. Once the response is received from the server, the optimistic result is thrown away and replaced with the actual result.

How do you update the Apollo Client cache after a mutation?

If a mutation updates a single existing entity, Apollo Client can automatically update that entity's value in its cache when the mutation returns. To do so, the mutation must return the id of the modified entity, along with the values of the fields that were modified.

Can GraphQL do updates?

Update mutations let you update existing objects of a particular type. With update mutations, you can filter nodes and set or remove any field belonging to a type.


1 Answers

To expand on the other two answers, the distinction lies in whether or not the thing you are 'updating' already exists in the cache or not.

As per the docs, if you are updating an existing item, say editing a title of a todo item, you only need optimisticResponse. Why? because the cache contains the node, and you only need to tell it that something new happened with the new node, which is immediately reflected on the UI.

optimisticResponse just provides an 'immediate' result data from a mutation.

Now we have a second case, you want to add a new Todo item to the list. First, the cache needs to know that a new item is created. As soon as you provide the update attribute to a Mutation, you are taking control of the caches' state.

update takes place of refetchQueries, which means you are in control of the cache state.

With update you can reach into the cache and modify/append exclusively the node you need, as opposed to maybe refetching an entire hierarchy of data. However, you are still waiting for the Mutation to finish. If you provide the update along side the optimisticResponse, you are providing an immediate assumed response, and giving it to your personal update function, which then instantly updates the cache.

The reason these two are paired in scenario two, is that you are completely bypassing the server responses. If you just give a 'immediate' response, Apollo is still in the mode where it waits for the server to update cache. update lets you highjack that as well, and do it client side.

Final Note: you are assuming the server is always responding without errors. Error handling elsewhere will still work, but you might get into UI inconsistencies if you are frequently catching errors (say isLoggedIn scenarios) so def make sure that the queries you 'fast track' are typically healthy.

like image 82
Pix3l Avatar answered Sep 19 '22 15:09

Pix3l