2

I am trying to use Apollo Cache specifically for local state management. I am using Apollo Client 3. I am trying to write default state ( a simple object ) using writeQuery in the cache, but it doesn't get written in the cache. I can't find what I am missing here.

const DEFAULT_STATE_QUERY = gql`
  query defaultState{
      id
      name
      age
  }
`;

and

 client.writeQuery({
      query: DEFAULT_STATE_QUERY,
      data: {
        defaultState: {
          __typename: "DefaultState",
          id: 1,
          name: "Biryani",
          age: 45
        },
      },
    });

I checked that we don't need typeDefs for reading and writing to cache as long as we don't want introspection to work out in apollo dev-tools for example. Hence I initialised a simple client

  const client = new ApolloClient({
    cache: new InMemoryCache()
  });

and provided it to my components using ApolloProvider

Inspite of running the code which does client.writeQuery, I dont see anything in Apollo cache and expectedly, one of the components running a useQuery too returns undefined data. What am I missing ?

alchemist95
  • 759
  • 2
  • 9
  • 26
  • Everything looks correct but if `client.writeQuery` is not called from the correct place, then it may not work. Could you provide more sample code as to where you are making this call? – forrestDinos Sep 01 '21 at 05:48

0 Answers0