Am I correct that ApolloClient caches my query, -- even across components -- so useContext isn't necessary?

30 Views Asked by At

I have an ApolloClient performing a query through useQuery.

I'd like that object to be available to the whole app, without having to re-fetch it from the database. So I was implementing useContext.

However, i remembered that ApolloClient caches its results.

So if I have this line of code:

const { loading, error, data } = useQuery(USER);

and I repeat that line of code across multiple components, will the components further down the tree just access the cached version? Basically negating the need for useContext?

Thank you.

here's my client config:

const client = new ApolloClient({
  link: new HttpLink({
    uri: graphql_url,
    fetch: async (uri: string, options: any) => {
      options.headers.Authorization = `Bearer ${realmUserToken}`;
      return fetch(uri, options);
    },
  }),
  cache: new InMemoryCache(),
});
0

There are 0 best solutions below