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 queryType:true, mutationType:true, and subscriptionType:true type policies. #7463

Merged
merged 3 commits into from
Dec 11, 2020
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.3.6

### Bug Fixes

- Immediately apply `queryType: true`, `mutationType: true`, and `subscriptionType: true` type policies, rather than waiting for the first time the policy is used, fixing a [regression](https://github.com/apollographql/apollo-client/issues/7443) introduced by [#7065](https://github.com/apollographql/apollo-client/pull/7065). <br/>
[@benjamn](https://github.com/benjamn) in [#7463](https://github.com/apollographql/apollo-client/pull/7463)

## Apollo Client 3.3.5

### Improvements
Expand Down
50 changes: 50 additions & 0 deletions src/cache/inmemory/__tests__/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4872,6 +4872,12 @@ describe("type policies", function () {
}
});

expect(cache.readQuery({
query: gql`query { __typename }`,
})).toEqual({
__typename: "RootQuery",
});

const ALL_ITEMS = gql`
query Items {
__typename
Expand Down Expand Up @@ -4921,4 +4927,48 @@ describe("type policies", function () {
],
});
});

it("can configure {query,mutation,subscription}Type:true", () => {
const cache = new InMemoryCache({
typePolicies: {
RootQuery: {
queryType: true,
},
RootMutation: {
mutationType: true,
},
RootSubscription: {
subscriptionType: true,
},
}
});

expect(cache.readQuery({
query: gql`query { __typename }`,
})).toEqual({
__typename: "RootQuery",
});

expect(cache.readFragment({
id: "ROOT_MUTATION",
fragment: gql`
fragment MutationTypename on RootMutation {
__typename
}
`,
})).toEqual({
__typename: "RootMutation",
});

expect(cache.readFragment({
id: "ROOT_SUBSCRIPTION",
fragment: gql`
fragment SubscriptionTypename on RootSubscription {
__typename
}
`,
})).toEqual({
__typename: "RootSubscription",
});
});
});
30 changes: 25 additions & 5 deletions src/cache/inmemory/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,31 @@ export class Policies {

public addTypePolicies(typePolicies: TypePolicies) {
Object.keys(typePolicies).forEach(typename => {
const incoming = typePolicies[typename];
const {
queryType,
mutationType,
subscriptionType,
...incoming
} = typePolicies[typename];

// Though {query,mutation,subscription}Type configurations are rare,
// it's important to call setRootTypename as early as possible,
// since these configurations should apply consistently for the
// entire lifetime of the cache. Also, since only one __typename can
// qualify as one of these root types, these three properties cannot
// be inherited, unlike the rest of the incoming properties. That
// restriction is convenient, because the purpose of this.toBeAdded
// is to delay the processing of type/field policies until the first
// time they're used, allowing policies to be added in any order as
// long as all relevant policies (including policies for supertypes)
// have been added by the time a given policy is used for the first
// time. In other words, since inheritance doesn't matter for these
// properties, there's also no need to delay their processing using
// the this.toBeAdded queue.
if (queryType) this.setRootTypename("Query", typename);
if (mutationType) this.setRootTypename("Mutation", typename);
if (subscriptionType) this.setRootTypename("Subscription", typename);

if (hasOwn.call(this.toBeAdded, typename)) {
this.toBeAdded[typename].push(incoming);
} else {
Expand Down Expand Up @@ -390,10 +414,6 @@ export class Policies {
// using field policies to merge child objects.
setMerge(existing, incoming.merge);

if (incoming.queryType) this.setRootTypename("Query", typename);
if (incoming.mutationType) this.setRootTypename("Mutation", typename);
if (incoming.subscriptionType) this.setRootTypename("Subscription", typename);

existing.keyFn =
// Pass false to disable normalization for this typename.
keyFields === false ? nullKeyFieldsFn :
Expand Down