Gql arguments are required if defined in keyArgs
See original GitHub issueI’m unsure if this is intended behaviour and I can’t seem to find much documentation on this feature as the documentation is being worked on (#6711).
I am using relayStylePagination / keyArgs like so
Intended outcome:
keyArgs
should not require the argument to be defined in thegql
query, if a key arg is defined in the InMemoryCache but not the specificgql
, the key arg should be ignored- The error message (unless this is made redundant by above change) needs to be changed to an appropriate message for keyArgs
InMemoryCache
const cache = new InMemoryCache({
Query: {
fields: {
contacts: relayStylePagination(["mine", "scopes", "propertyId"])
},
},
},
});
gql
query GetContacts($mine: Boolean $cursor: String, $scopes: [ScopesEnum!]) { ... }
This causes issues if you have a large application with multiple gql files defined for the same field but with different arguments - my current work around is to write a function for the keyArgs and filter them depending on the arguments like so
const filterKeyArgsToArgs = (keyArgs: any) => (args: any) =>
args ? keyArgs.filter((keyArg: any) => args.hasOwnProperty(keyArg)) : null;
const cache = new InMemoryCache({
Query: {
fields: {
contacts: relayStylePagination(filterKeyArgsToArgs(["mine", "scopes", "propertyId"]))
},
},
},
});
Actual outcome:
I get the following error (which is worded for key fields) when the propertyId
key arg is defined in the InMemoryCache but not in the gql
Invariant Violation: Missing field 'propertyId' while computing key fields
- I am able to replicate this issue for
keyArgs
without using the pagination helper as well. - I have tracked it down to this function https://github.com/apollographql/apollo-client/blob/main/src/cache/inmemory/policies.ts#L854-L883
How to reproduce the issue:
- Define keyargs for a field in the InMemoryCache
- Have a gql query for that field which uses those arguments - works fine
- Have a gql query for that field which does not use those arguments - does not work (gets above error)
Let me know if you need to create a demo
Versions
System:
OS: Linux 5.4 Ubuntu 18.04.5 LTS (Bionic Beaver)
Binaries:
Node: 12.18.3 - /usr/bin/node
Yarn: 1.22.5 - /usr/bin/yarn
npm: 6.14.7 - /usr/bin/npm
Browsers:
Chrome: 85.0.4183.83
Firefox: 80.0.1
npmPackages:
@apollo/client: ^3.1.4 => 3.1.4
Issue Analytics
- State:
- Created 3 years ago
- Reactions:10
- Comments:7 (2 by maintainers)
Top GitHub Comments
@zsaraf I believe relayStylePagination and all the other pagination helpers are more guides on how to guide your pagination helper?
Either way they should still be usable (and the fact that this affects all keyArgs! not just relayStylePagination).
Here is my solution:
Basically you can pass a function into relayStylePagination / keyArgs which will receive the current arguments, then filter out the keyargs which aren’t in the arguments. Let me know if you have any trouble with it, it’s what i’ve been using in production for the past couple weeks after removing updateQuery.
@tadhglewis I think allowing optional arguments in
keyArgs
is the right solution here! Fix incoming: #7109