Skip to content

Commit

Permalink
Make keyArgs tolerant of optional arguments. (#7109)
Browse files Browse the repository at this point in the history
As suggested by @tadhglewis in #6973.

The [`keyArgs: ["someArg", "anotherArg"]` configuration for field policies](https://www.apollographql.com/docs/react/caching/cache-field-behavior/#specifying-key-arguments)
should be able to include arguments that are _optional_, while still
specifying an ordering of all possible key arguments for serialization
purposes. When an optional argument is not provided, it will simply
not be included in the serialized `storeFieldName` suffix, and no
exception will be thrown.
  • Loading branch information
benjamn authored Oct 2, 2020
1 parent aa14289 commit 8163dd9
Show file tree
Hide file tree
Showing 4 changed files with 570 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
- The schema link package (`@apollo/client/link/schema`) will now validate incoming queries against its client-side schema, and return `errors` as a GraphQL server would. <br/>
[@amannn](https://github.com/amannn) in [#7094](https://github.com/apollographql/apollo-client/pull/7094)

- Allow optional arguments in `keyArgs: [...]` arrays for `InMemoryCache` field policies. <br/>
[@benjamn](https://github.com/benjamn) in [#7109](https://github.com/apollographql/apollo-client/pull/7109)

## Apollo Client 3.2.2

## Bug Fixes
Expand Down
372 changes: 372 additions & 0 deletions src/cache/inmemory/__tests__/__snapshots__/policies.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,378 @@ Object {
}
`;

exports[`type policies field policies can include optional arguments in keyArgs 1`] = `
Object {
"Author:{\\"name\\":\\"Nadia Eghbal\\"}": Object {
"__typename": "Author",
"name": "Nadia Eghbal",
"writings:{\\"type\\":\\"Book\\"}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
},
"ROOT_QUERY": Object {
"__typename": "Query",
"author": Object {
"__ref": "Author:{\\"name\\":\\"Nadia Eghbal\\"}",
},
},
}
`;

exports[`type policies field policies can include optional arguments in keyArgs 2`] = `
Object {
"Author:{\\"name\\":\\"Nadia Eghbal\\"}": Object {
"__typename": "Author",
"name": "Nadia Eghbal",
"writings:{\\"a\\":1,\\"b\\":2,\\"type\\":\\"Book\\"}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"type\\":\\"Book\\"}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
},
"ROOT_QUERY": Object {
"__typename": "Query",
"author": Object {
"__ref": "Author:{\\"name\\":\\"Nadia Eghbal\\"}",
},
},
}
`;

exports[`type policies field policies can include optional arguments in keyArgs 3`] = `
Object {
"Author:{\\"name\\":\\"Nadia Eghbal\\"}": Object {
"__typename": "Author",
"name": "Nadia Eghbal",
"writings:{\\"a\\":1,\\"b\\":2,\\"type\\":\\"Book\\"}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"a\\":1,\\"b\\":2}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"type\\":\\"Book\\"}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
},
"ROOT_QUERY": Object {
"__typename": "Query",
"author": Object {
"__ref": "Author:{\\"name\\":\\"Nadia Eghbal\\"}",
},
},
}
`;

exports[`type policies field policies can include optional arguments in keyArgs 4`] = `
Object {
"Author:{\\"name\\":\\"Nadia Eghbal\\"}": Object {
"__typename": "Author",
"name": "Nadia Eghbal",
"writings:{\\"a\\":1,\\"b\\":2,\\"type\\":\\"Book\\"}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"a\\":1,\\"b\\":2}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"b\\":2}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"type\\":\\"Book\\"}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
},
"ROOT_QUERY": Object {
"__typename": "Query",
"author": Object {
"__ref": "Author:{\\"name\\":\\"Nadia Eghbal\\"}",
},
},
}
`;

exports[`type policies field policies can include optional arguments in keyArgs 5`] = `
Object {
"Author:{\\"name\\":\\"Nadia Eghbal\\"}": Object {
"__typename": "Author",
"name": "Nadia Eghbal",
"writings:{\\"a\\":1,\\"b\\":2,\\"type\\":\\"Book\\"}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"a\\":1,\\"b\\":2}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"a\\":3}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"b\\":2}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"type\\":\\"Book\\"}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
},
"ROOT_QUERY": Object {
"__typename": "Query",
"author": Object {
"__ref": "Author:{\\"name\\":\\"Nadia Eghbal\\"}",
},
},
}
`;

exports[`type policies field policies can include optional arguments in keyArgs 6`] = `
Object {
"Author:{\\"name\\":\\"Nadia Eghbal\\"}": Object {
"__typename": "Author",
"name": "Nadia Eghbal",
"writings:{\\"a\\":1,\\"b\\":2,\\"type\\":\\"Book\\"}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"a\\":1,\\"b\\":2}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"a\\":3}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"b\\":2}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"type\\":\\"Book\\"}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
},
"ROOT_QUERY": Object {
"__typename": "Query",
"author": Object {
"__ref": "Author:{\\"name\\":\\"Nadia Eghbal\\"}",
},
},
}
`;

exports[`type policies field policies can include optional arguments in keyArgs 7`] = `
Object {
"Author:{\\"name\\":\\"Nadia Eghbal\\"}": Object {
"__typename": "Author",
"name": "Nadia Eghbal",
"writings:{\\"a\\":1,\\"b\\":2,\\"type\\":\\"Book\\"}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"a\\":1,\\"b\\":2}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"a\\":3}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"b\\":2}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"b\\":4}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"type\\":\\"Book\\"}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
},
"ROOT_QUERY": Object {
"__typename": "Query",
"author": Object {
"__ref": "Author:{\\"name\\":\\"Nadia Eghbal\\"}",
},
},
}
`;

exports[`type policies field policies can include optional arguments in keyArgs 8`] = `
Object {
"Author:{\\"name\\":\\"Nadia Eghbal\\"}": Object {
"__typename": "Author",
"name": "Nadia Eghbal",
"writings": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"a\\":1,\\"b\\":2,\\"type\\":\\"Book\\"}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"a\\":1,\\"b\\":2}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"a\\":3}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"b\\":2}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"b\\":4}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{\\"type\\":\\"Book\\"}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
"writings:{}": Array [
Object {
"__typename": "Book",
"isbn": "0578675862",
"title": "Working in Public: The Making and Maintenance of Open Source Software",
},
],
},
"ROOT_QUERY": Object {
"__typename": "Query",
"author": Object {
"__ref": "Author:{\\"name\\":\\"Nadia Eghbal\\"}",
},
},
}
`;

exports[`type policies field policies read, merge, and modify functions can access options.storage 1`] = `
Object {
"ROOT_QUERY": Object {
Expand Down
Loading

0 comments on commit 8163dd9

Please sign in to comment.