0
votes

I'm using ApolloClient in my React app.

I have a GraphQL server running with Spring Boot.

I'm noticing an issue in that whenever my query options (as set in the variables) are the same, it doesn't actually make the query/call the GraphQL server, instead it appears to be using results from the cache.

How do I go about forcing it to actually make the search as data may have changed?

Here is my service in which I make the call to the get function in my front end - I'm hoping there is some option like forceRefetch: true:

return graphQLClient
   .query<MultipleSongsQueryResult>({
      query: GET_SONGS,
      variables: options,
   })
.then(response => response.data);

Thanks.

1

1 Answers

1
votes

The default fetch policy for the Apollo Client is cache-first, so if you make the same query twice, the first time Apollo will get the data from the database and the second time it will get the data from your local cache (which is what seems to be happening in your case).

If you want always to fetch data from the server, you could change the fetch-policy to network-only for example.

This might help you decide which fetch policy is better for you: https://www.apollographql.com/docs/react/api/react-apollo/#optionsfetchpolicy.

Also if you change your data with a mutation, you could use the refetchQueries option to reload the data from the server: https://www.apollographql.com/docs/react/data/queries/#updating-cached-query-results.