From d554072c62ee6be0d1b4bcd3bbd02b4c4977a789 Mon Sep 17 00:00:00 2001 From: Eric Lee Date: Fri, 10 Jun 2022 11:58:45 -0700 Subject: [PATCH 1/2] fix: fixes 9804 --- src/cache/inmemory/__tests__/policies.ts | 74 ++++++++++++++++++++++++ src/cache/inmemory/policies.ts | 2 +- 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/cache/inmemory/__tests__/policies.ts b/src/cache/inmemory/__tests__/policies.ts index d8b5d16d64c..843d7ce8542 100644 --- a/src/cache/inmemory/__tests__/policies.ts +++ b/src/cache/inmemory/__tests__/policies.ts @@ -1650,6 +1650,80 @@ describe("type policies", function () { expect(cache.extract(true)).toEqual(expectedExtraction); }); + it("should return correct variables in read function", function () { + const cache = new InMemoryCache({ + typePolicies: { + Country: { + fields: { + isCanada: { + read(_, { readField }) { + return readField("name") === "CA"; + } + }, + name: { + read(_, { variables }) { + return variables?.code; + } + } + } + } + } + }); + + cache.writeQuery({ + query: gql` + query Countries($code: ID!) { + country(code: $code) { + name + } + } + `, + data: { + country: { + __typename: "Country", + name: "CA", + }, + }, + variables: { + code: "CA", + }, + }); + + const expectedExtraction = { + ROOT_QUERY: { + __typename: "Query", + "country({\"code\":\"CA\"})": { + __typename: "Country", + name: "CA", + }, + }, + }; + + expect(cache.extract(true)).toEqual(expectedExtraction); + + const expectedResult = { + country: { + __typename: "Country", + name: "CA", + isCanada: true, + }, + }; + + expect(cache.readQuery({ + query: gql` + query Countries($code: ID!) { + country(code: $code) { + name + isCanada @client + } + } + `, + variables: { + code: "CA", + }, + })).toEqual(expectedResult); + }); + it("read and merge can cooperate through options.storage", function () { const cache = new InMemoryCache({ typePolicies: { diff --git a/src/cache/inmemory/policies.ts b/src/cache/inmemory/policies.ts index b067265186b..19c574c0af9 100644 --- a/src/cache/inmemory/policies.ts +++ b/src/cache/inmemory/policies.ts @@ -912,7 +912,7 @@ function makeFieldFunctionOptions( canRead, readField() { return policies.readField( - normalizeReadFieldOptions(arguments, objectOrReference, context), + normalizeReadFieldOptions(arguments, objectOrReference, variables), context, ); }, From ae6cb384484b747512c1f70b4abd6c0b62781146 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Fri, 10 Jun 2022 18:16:57 -0400 Subject: [PATCH 2/2] Mention PR #9808 in CHANGELOG.md. --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3ea0dd87ab..b02971bbaa7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## Apollo Client 3.6.8 (unreleased) + +### Bug Fixes + +- Fix incorrect `variables` passed in `FieldFunctionOptions` for nested `readField` calls in `read` and `merge` functions.
+ [@stardustxx](https://github.com/stardustxx) in [#9808](https://github.com/apollographql/apollo-client/pull/9808) + ## Apollo Client 3.6.7 (2022-06-10) ### Bug Fixes