I'm trying to implement a cache update after mutations for react-apollo-hooks.

const onDeleteHandler = useMutation(DELETE_POST, {
        update: (proxy, mutationResult) => {
            /* custom update logic */
            try {
                const { deletePost } = mutationResult.data;
                const postFeed = proxy.readQuery({
                    query: GET_POSTS, variables
                })
                console.log("postFeed", postFeed)
                proxy.writeQuery({ query: GET_POSTS })
            }
            catch(error){
                console.log("Cache update error", error)
            }
          },
        })

I'm currently getting the following error message:

Error writing result to store for query "kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"firstName"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"lastName"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}}]}}],"loc":{"start":0,"end":434}

My hunch is that the writeQuery() method needs a secondary variable like the following:

proxy.writeQuery({ query: GET_POSTS, data })

But, I'm not sure what should be included or if that's even the issue

My client-side query is this:

export const GET_POSTS = gql`
    query Posts($query: String, $skip: Int, $first: Int, $orderBy: UserOrderByInput){
        posts (
            query: $query
            skip: $skip
            first: $first
            orderBy: $orderBy
        ){
            id
            title
            body
            location
            published
            author {
                id
                firstName
                lastName
            }
        }
    }
`

And variables:

const variables = { ...queryVariables, query: value.state.searchTerm, orderBy: filter }

0 Answers