Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixes FieldFunctionOptions return incorrect variables #9808

Merged
merged 2 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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