cache.readQuery with variables doesn't work
See original GitHub issueI 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:
- Created 4 years ago
- Reactions:3
- Comments:24 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
From the Apollo docs
readQuery
section: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 theinvariant 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 atry / catch
.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