question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cache.readQuery with variables doesn't work

See original GitHub issue

I have the following code and don’t work, I’m struggling to make it work for the last 4 days.

const newTodo = {
   id: 0,
   text: "text",
   completed: false,
   __typename: "todo"
 };

 const data = {
   todos: [newTodo]
 };

 cache.writeData({ data });

 const GET_TODOS = gql`
   query todo($id: Int!) {
     todos(id: $id) {
       id
       completed
       text
     }
   }
 `;

 const da = cache.readQuery({ query: GET_TODOS, variables: { id: 0 } });

I get the error message:

Uncaught Invariant Violation: Can't find field todos({"id":0}) on object {
  "todos": [
    {
      "type": "id",
      "generated": false,
      "id": "todo:0",
      "typename": "todo"
    }
  ]
}.
    at new InvariantError

please advice what i’m doing wrong…

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:24 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
mmichael0413commented, Apr 17, 2020

From the Apollo docs readQuery section:

If your cache doesn’t contain all of the data necessary to fulfill a specified query, readQuery throws an error. It never attempts to fetch data from a remote server.

I kept running into this when using readQuery to grab an item from the Apollo cache when said item wasn’t cached yet. If you’re getting the invariant violation error and your query is formatted properly (e.g. all the necessary variables are set), check to make sure the item(s) for which you’re querying are, in fact, already in the cache.

In the cases where I suspect the item(s) may not yet be in the cache, and I’m using readQuery to get them, I wrap the block in a try / catch.

3reactions
ziedHamdicommented, Feb 4, 2020

Calling client.query instead works fine, this is maybe a misunderstanding of readQuery that I naively thought will check if exists in cache and if not will call client.query… (otherwise I don’t get the difference between client.readQuery and cache.readQuery).

I also suggest, the update method of a mutation provide client as a param since I always need to pass it using the withApollo() artefact generating unnecessary boilerplate code

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reading and writing data to the cache - Apollo GraphQL Docs
If the cache is missing data for any of the query's fields, readQuery returns null . It does not attempt to fetch data...
Read more >
readQuery not returning expected results - Stack Overflow
I have a function that sets a value in my local storage and should update my Apollo cache: const submitChange = async ()...
Read more >
Reading and Writing Data to the Cache - Ferry Graphql
The readQuery method enables you to run a GraphQL query directly on your cache. ... If your cache doesn't contain all of the...
Read more >
Reading and writing data to the cache - Client (React)
The readQuery method enables you to run a GraphQL query directly on your cache. ... If your cache doesn't contain all of the...
Read more >
Advanced Topics on Caching – Angular
When we hit the "Load More" button, we don't want Apollo Client to throw away the repository information it has already loaded. Instead,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found