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 IntrospectionFragmentMatcher handling of fragment on root query #4620

Merged
merged 3 commits into from
Apr 8, 2019
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
- Support `new InMemoryCache({ freezeResults: true })` to help enforce immutability. <br/>
[@benjamn](https://github.com/benjamn) in [#4514](https://github.com/apollographql/apollo-client/pull/4514)

- Allow `IntrospectionFragmentMatcher` to match fragments against the root `Query`, as `HeuristicFragmentMatcher` does. <br/>
[@rynobax](https://github.com/rynobax) in [#4620](https://github.com/apollographql/apollo-client/pull/4620)

## Apollo Client 2.5.1

### apollo-client 2.5.1
Expand Down
10 changes: 4 additions & 6 deletions packages/apollo-cache-inmemory/src/fragmentMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,9 @@ export class HeuristicFragmentMatcher implements FragmentMatcherInterface {
): boolean | 'heuristic' {
const obj = context.store.get(idValue.id);

if (!obj && idValue.id === 'ROOT_QUERY') {
return true;
}

if (!obj) {
return false;
// https://github.com/apollographql/apollo-client/pull/3507
return idValue.id === 'ROOT_QUERY';
}

if (!obj.__typename) {
Expand Down Expand Up @@ -125,7 +122,8 @@ export class IntrospectionFragmentMatcher implements FragmentMatcherInterface {
const obj = context.store.get(idValue.id);

if (!obj) {
return false;
// https://github.com/apollographql/apollo-client/pull/4620
return idValue.id === 'ROOT_QUERY';
}

invariant(
Expand Down
72 changes: 72 additions & 0 deletions packages/apollo-client/src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,78 @@ describe('client', () => {
});

it('should allow fragments on root query', () => {
const query = gql`
query {
...QueryFragment
}

fragment QueryFragment on Query {
records {
id
name
__typename
}
__typename
}
`;

const data = {
records: [
{ id: 1, name: 'One', __typename: 'Record' },
{ id: 2, name: 'Two', __typename: 'Record' },
],
__typename: 'Query',
};

return clientRoundtrip(query, { data }, null);
});

it('should allow fragments on root query with ifm', () => {
const query = gql`
query {
...QueryFragment
}

fragment QueryFragment on Query {
records {
id
name
__typename
}
__typename
}
`;

const data = {
records: [
{ id: 1, name: 'One', __typename: 'Record' },
{ id: 2, name: 'Two', __typename: 'Record' },
],
__typename: 'Query',
};

const ifm = new IntrospectionFragmentMatcher({
introspectionQueryResultData: {
__schema: {
types: [
{
kind: 'UNION',
name: 'Query',
possibleTypes: [
{
name: 'Record',
},
],
},
],
},
},
});

return clientRoundtrip(query, { data }, null, ifm);
});

it('should merge fragments on root query', () => {
// The fragment should be used after the selected fields for the query.
// Otherwise, the results aren't merged.
// see: https://github.com/apollographql/apollo-client/issues/1479
Expand Down