Skip to content

Commit

Permalink
Fix incorrect variables in FieldFunctionOptions for nested `readF…
Browse files Browse the repository at this point in the history
…ield` calls (#9808)

Fixes #9804
  • Loading branch information
stardustxx authored Jun 10, 2022
1 parent 83935e8 commit 31fc8df
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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. <br/>
[@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
Expand Down
74 changes: 74 additions & 0 deletions src/cache/inmemory/__tests__/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion src/cache/inmemory/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ function makeFieldFunctionOptions(
canRead,
readField<T>() {
return policies.readField<T>(
normalizeReadFieldOptions(arguments, objectOrReference, context),
normalizeReadFieldOptions(arguments, objectOrReference, variables),
context,
);
},
Expand Down

0 comments on commit 31fc8df

Please sign in to comment.