0

I have already looked at: Writing nested object to Apollo client cache

And I guess everywhere else on the internet.

So what I want to do is to write an object to the Apollo cache, to store it locally. This is my setup:

const cache = new InMemoryCache()
const client = new ApolloClient({
    uri: '/hasura/v1/graphql',
    cache,
    credentials: 'include',
})

Then I write this to initialize the cache, taken from: https://www.apollographql.com/docs/react/data/local-state/#querying-local-state

cache.writeData({
    data: {
        todos: [],
        visibilityFilter: 'SHOW_ALL',
        networkStatus: {
            __typename: 'NetworkStatus',
            id: 1,
            isConnected: false,
        },
    },
})

Now, when I want to query the cache using this query:

const lolquery = gql`{
    visibilityFilter @client
}`

const result = cache.readQuery({ query: lolquery })
console.log(result)

I can get the visibilityFilter and todos, but when I try to query for networkStatus i.e.,

const lolquery = gql`{
    networkStatus @client
}`

I get the following error:

Uncaught Invariant Violation: Invariant Violation: 10

Googling this error doesn't give me much of an answer.

Am I the only one experiencing this? Am I missing something really obvious?

1 Answer 1

0

networkStatus was initialized as an object, so you have to query it with a subselection of one or more fields.

{
  networkStatus @client {
    isConnected
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Okay, wow, actually yes. I don't know how I haven't tried this... But I guess because my endgoal is to fetch a list of objects/I don't know if I have read anywhere that you NEED to do this specification when fetching objects with GraphQL. How frustrating... but thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.