Search code examples
reactjsgraphqlapolloapollo-client

How to update apollo cache state BEFORE mutation, for actions like Like, Upvote etc.?


I'm making a react app using Apollo client for GraphQL queries, where I want to update upvotes state, so that upvoting doesn't take time for users. If I don't, I have to wait for the mutation to return data, which takes 1 second. Is there any way to fix this?


Solution

  • I think optimisticResponse is your solution. (as @xadm pointed out)

    https://www.apollographql.com/docs/react/performance/optimistic-ui/

    If you already have implemented the update function in your mutation (which is normally used to update your query, otherwise you should), it will be called twice, once instantly with your optimisticResponse, and once again with the server response.