Skip to content

Commit

Permalink
Merge pull request #4620 from rynobax/queryFragmentFix
Browse files Browse the repository at this point in the history
Fix IntrospectionFragmentMatcher handling of fragment on root query
  • Loading branch information
benjamn authored Apr 8, 2019
2 parents a9952c7 + 2d87cef commit 2469e3f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 6 deletions.
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

0 comments on commit 2469e3f

Please sign in to comment.