apollo-client: .readQuery errors on queries which originally had NULL variables
Trying to read a query which originally had NULL
values (perfectly acceptable in the case of cursor pagination for example) will fail, because the call to .readQuery
will strip NULL
values.
Example: Given that this query have been executed on the client:
query getUsers($cursor: String) {
users(first: 50, after: $cursor) {
id
}
}
Apollo will cache it on a store key looking something like $ROOT_QUERY.users({"first":"10","after": NULL})
. Then, when trying to .readQuery
with that exact same query, one will get an error like:
Error: Can't find field users({"first":50}) on object ($ROOT_QUERY) {
"users({\"first\":50,\"after\":null})": {
"type": "id",
"id": "$ROOT_QUERY.users({\"first\":50,\"after\":null})",
"generated": true
},
"__typename": "User"
}
Notice that the object passed in Can't find field users({"first": 50})
lacks the {"after": NULL}
which is actually in the store key.
Why?
I think I found the problem. Given how the store key is resolved in https://github.com/apollographql/apollo-client/blob/master/src/data/storeUtils.ts#L163, any GraphQL query that has an null
argument, will cause this error.
The keys in the store can look like this $ROOT_QUERY.whatever.foo.bar({"foo":"bar","baz": NULL})
, but when the readStoreResolver
tries to find them in the store, it looks for $ROOT_QUERY.whatever.foo.bar({"foo":"bar"})
because the NULL
values are undefined
at this point, and will be thrown away by JSON.stringify()
.
Version
- apollo-client@latest
Originally discussed in https://github.com/apollographql/apollo-client/issues/1701
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 30
- Comments: 34 (8 by maintainers)
Commits related to this issue
- Update 6-more-mutations-and-updating-the-store.md query arguments are added to early and will cause errors at this stage: https://github.com/qucode1/hackernews-react-apollo#problem-4-wip Which is ca... — committed to qucode1/howtographql by qucode1 6 years ago
- Update 6-more-mutations-and-updating-the-store.md query arguments are added to early and will cause errors at this stage: https://github.com/qucode1/hackernews-react-apollo#problem-4-wip Which is ca... — committed to qucode1/howtographql by qucode1 6 years ago
None of the workaround with including empty fields in the
variables
argument ofreadQuery
worked for me. Would love to see a fix forreadQuery
withoutvariables
.Just noting that this is a problem for me, too (for purposes of assessing issue impact).
Here’s a more detailed example of how I solved this. Hopefully this makes more sense
As a workaround, I provided default argument values in my query. Providing variables in
readQuery
andwriteQuery
didn’t work in my case.Maybe this is helpful!
Edit:
For required arguments (that cannot have default values) I have to pass variables in
update
:@jbaxleyiii Here’s a how it should work, based on my previous comment
How it currently works
String
andID
types.Expected:
Example, given that
after
is an optional cursor variable, then both queries below would return the same result:Similarly, the following should work based on the same principle:
Is there a way to check if the query is loaded before tying to update it?
@jbaxleyiii I cannot get
.readQuery
to work at all in2.0
. Using thegraphql
HoC lets me query just fine, but.readQuery
gives me issues. Example detailed below.Setting up client like:
and then trying to do something like:
Let me know if I can further help.
For anyone else having this problem when using cursors, I got around this by setting a default empty string value i.e.
{ variables: after: '' }
on both the initial query and the.readQuery