Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetchPolicy: 'network-only' not working on client.query({...}) when 'ssrMode': true set on ApolloClient. #2983

Closed
kapilpipaliya opened this issue Feb 9, 2018 · 13 comments

Comments

@kapilpipaliya
Copy link

fetchPolicy: 'network-only' not working on client.query({...})

@kapilpipaliya kapilpipaliya changed the title fetchPolicy: 'network-only' not working on client.query({...}) fetchPolicy: 'network-only' not working on client.query({...}) when 'ssrMode': true set on ApolloClient. Feb 9, 2018
@dennislaupman
Copy link

Facing weird problems. It seems that client is still reading from cach, after that the network is storing the data in the cache. What means that you always one step behind.

@fc
Copy link

fc commented Feb 14, 2018

In apollo-client 2.2.2, a fetchPolicy of 'no-cache' was added which may fix this issue.

#2934

@dennislaupman
Copy link

dennislaupman commented Feb 19, 2018

const getDetails = graphql(DetailsViewQuery, {
options: ( props ) => {
return {
variables: { id: props.navigation.state.params._id },
fetchPolicy: 'network-only', // skip the cache
}
}
});

Still gives cached results in React Native

@hwillson
Copy link
Member

The original issue here

fetchPolicy: 'network-only' not working on client.query({...}) when 'ssrMode': true set on ApolloClient.

actually functions as designed:

if (this.disableNetworkFetches && options.fetchPolicy === 'network-only') {
options = { ...options, fetchPolicy: 'cache-first' } as WatchQueryOptions;
}

Setting ssrMode to true effectively sets disableNetworkFetches to true, which means your fetchPolicy is being changed to cache-first. You can read more about why this is happening in the Server-side rendering > Server initialization section of the apollo-client docs. In a nutshell - network-only is overkill with SSR, since we really only ever want to fetch each query result once. Closing for now - thanks!

@sea129
Copy link

sea129 commented Jun 1, 2018

@hwillson with ssrMode: true and Query component in react-apollo, fetchPolicy 'network-only' returns cached value https://github.com/apollographql/react-apollo/issues/556

which means if ssrMode is true, all queries return cached data, even set ssr={false} on Query component

@hwillson
Copy link
Member

hwillson commented Jun 1, 2018

Thanks @sea129 - apollographql/react-apollo#556 is still open, so we'll get to it shortly.

@duwerq
Copy link

duwerq commented Jan 27, 2019

@kapilpipaliya I had issues with client.query also. I solved it by chanaging:

  const { data: { getUser } } = await client.query({
        query: gql(queries.getUser)
      }, {
        options: {
          fetchPolicy: 'network-only'
        }
      });

to

const { data: { getUser } } = await client.query({
    query: gql(queries.getUser),
    fetchPolicy: 'network-only'
});

If dig into the code, you can see the all params passed into client.readQuery() after the gql query are treated as one object. Hence it looks for options.fetchPolicy instead of options.options.fetchPolicy
screen shot 2019-01-27 at 12 48 36 pm

@jack1012t
Copy link

thank you @duwerq

@spyshower
Copy link

Thank you so much @duwerq !

@hmmhmmhm
Copy link

thank you. you saved me at dawn. @duwerq

@lilrooness
Copy link

lilrooness commented May 31, 2019

Where is the use of client.query documented? It seems like we're all just debating the spec of the function here

Also thank you @duwerq you saved me many hours of trial and error

@raajnadar
Copy link

I used this config now the data is not cached. In my react native application.

export default new ApolloClient({
    ...
    defaultOptions: {
        watchQuery: {
            fetchPolicy: 'no-cache'
        },
       query: {
            fetchPolicy: 'no-cache'
       },
       mutate: {
            fetchPolicy: 'no-cache'
        }
    }
})

@Aarthysekar
Copy link

For me, in graphql() HOC, when i changed the options to functions it worked. Its weird, but worked.

graphql(SAMPLE_DATA_GQL, {
name: 'sampleData',
options: () => ({
fetchPolicy: 'network-only'
})
})

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests