Apollo-client: call refetchQueries without mutation

Created on 28 Oct 2016  路  9Comments  路  Source: apollographql/apollo-client

Is it possible to call refetchQueries without mutation?
Here is my scenario:
Logged user is trying to logout. That means I will just delete bearer token locally. But I need to refetch viewer from server after this operation.

something like:

@graphql(gql'', { refetchQueries: ['viewer'] })

I tried but without any success

this.context.client.mutate({ refetchQueries: ['viewer'] });

This ticket is related to react-apollo ticket https://github.com/apollostack/react-apollo/issues/241

Most helpful comment

Would very much like this feature.

All 9 comments

I would either call refetch on the individual queries, or use resetStore, which refetches all active queries. I think in your case resetStore is the right approach anyway, because you want to make sure there's no other data in the store that should no longer be there after logging out.

@helfer I see. I will use resetStore for login/logout but I think that sometimes you want to call refetchQueries without mutation as well. It can be a nice feature.

@seeden Yeah, sure! When you have a specific use-case for it, I will be happy to accept a PR 馃檪

Would very much like this feature.

@helfer @TSMMark @seeden Currently, I am doing:apolloclient.queryManager.refetchQueryByName('queryName')
then I made a function for refetching an array of queries:

export const refetchQueriesByName = (queryNames) => {
  queryNames.forEach(queryName => client.queryManager.refetchQueryByName(queryName))
}

If this is a good way of doing it, maybe we can make this easier to call? Or add this to the docs?

Why not just call refetch on that query? Or run the same query again with forceFetch set to true?

I've slowly changed my opinion over time and now think it's not a good pattern to have to refer to queries by name. I think it would be better to pass around query documents and refer to them as variables.

@helfer if we're using react-apollo, how would we access, from outside of its component, a query by reference rather than by name?

@TSMMark by moving the query to a variable:

const myQuery = gql`...`;

graphql(myQuery)(...)

We could also add a static method to the container component. Something like getGraphQLDocument.

how do get the queryManager,refetchQueryByName object? I've tried this:

import withApollo from 'react-apollo'
// ...
console.log(this.props.client) // ApolloClient {...}
console.log (this.props.client.queryManager) // undefined
// ...
export default withApollo(myComponent)

but withou success

Was this page helpful?
0 / 5 - 0 ratings