React-apollo: Can I update local state in updateQuery method of fetchMore?

Created on 15 Aug 2018  路  4Comments  路  Source: apollographql/react-apollo

I use react-apollo and apollo-link-state for local state management.

I want to update local state in updateQuery method of fetchMore

  graphql(Q.COMMENT_BY_PAGE, {
    options: () => {
      return {
        // https://github.com/apollographql/react-apollo/issues/289
        // Do not query when react component is mounted. Query when user click "Load More" button
        variables: { skip: true }
      };
    },
    props: ({ data }) => {
      return {
        onLoadMore: ({ id, offset, limit, skip }) => {
          return data.fetchMore({
            variables: {
              id,
              offset,
              limit,
              skip
            },
            updateQuery: (prev, { fetchMoreResult }) => {
              if (!fetchMoreResult) return prev;
              // Can I update local state here? Like:

              // const data = cache.readQuery({query: Q.BOOK_BY_ID})
              // data.comments.push(fetchMoreResult.comments);
              // cache.writeQuery({query: Q.BOOK_BY_ID, data})

              return prev;
            }
          });
        }
      };
    }
  }),

Most helpful comment

@danilobuerger clarified on slack that it is the mutation options updateQueries that is slated to be removed https://www.apollographql.com/docs/react/api/react-apollo.html#graphql-mutation-options-updateQueries

All 4 comments

updateQuery is slated to be removed in future releases. I suggest looking at the apollo-client API, specifically writeQuery and writeData.

Please submit questions to the slack channel or stack overflow. We need to keep this tracker focused on actionable issues.

@rosskevin Why is updateQuery going to be removed? Can you link docs related to its deprecation? Does this also apply to the updateQuery function available on the QueryResult object? https://www.apollographql.com/docs/react/essentials/queries.html#render-prop

@syed-mohsin - I can't find the comment now - it was from a core maintainer either here or in apollo-client, it wasn't a sure thing but contemplated if I recall so there is no documentation on it. If it is removed from apollo-client it will almost certainly be removed here as react-apollo is just a react-specific pattern of use for apollo-client.

@danilobuerger clarified on slack that it is the mutation options updateQueries that is slated to be removed https://www.apollographql.com/docs/react/api/react-apollo.html#graphql-mutation-options-updateQueries

Was this page helpful?
0 / 5 - 0 ratings