Am I using readQuery and writeQuery properly?


#1

I’m a bit confused on how to call readQuery() and writeQuery() within an update, when I have variables I’m passing to my query (e.g. for pagination).

.mutate({
  mutation: createTrackMutation,
  variables: newTrack,
  update: (store, { data: { createTrack } }) => {
    const variables = {
      limit: this.pageSize,
      order: this.order === 'ascending' ? 'asc' : 'desc',
      sortBy: this.sortBy,
    };
    const data = store.readQuery({
      query: listTracksQuery,
      variables,
    });
    data.listTracks.items.push(createTrack);
    store.writeQuery({
      query: listTracksQuery,
      variables,
      data,
    });
  },
  optimisticResponse: {
    __typename: 'Mutation',
    createTrack: {
      __typename: 'Track',
      ...newTrack,
      createdAt_trackId: `${new Date().toISOString()}_x`,
    },
  },
})

It seems overkill, but I think the idea is that I’m replicating in the cache store exactly what the user has queried, correct?