Skip to content

Commit

Permalink
Throw if Policies#fragmentMatches receives a falsy __typename.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Dec 13, 2019
1 parent 81dc465 commit 83b5837
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/cache/inmemory/__tests__/entityStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1199,10 +1199,16 @@ describe('EntityStore', () => {

expect(() => cache.readQuery({
query: queryWithAliases,
})).toThrow(/Can't find field a on object/);
})).toThrow(
"Attempted to match fragment Rest with type condition ABCs against " +
"object with unknown __typename"
);

expect(() => cache.readQuery({
query: queryWithoutAliases,
})).toThrow(/Can't find field a on object/);
})).toThrow(
"Attempted to match fragment Rest with type condition ABCs against " +
"object with unknown __typename"
);
});
});
9 changes: 6 additions & 3 deletions src/cache/inmemory/__tests__/roundtrip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ describe('roundtrip', () => {
);
});

it('should resolve on union types with inline fragments without typenames with warning', () => {
return withWarning(() => {
it('should error on union types with inline fragments without typenames', () => {
return expect(() => {
storeRoundtrip(
gql`
query {
Expand Down Expand Up @@ -337,7 +337,10 @@ describe('roundtrip', () => {
],
},
);
});
}).toThrowError(
'Attempted to match fragment with type condition Jedi against ' +
'object with unknown __typename'
);
});

// XXX this test is weird because it assumes the server returned an incorrect result
Expand Down
8 changes: 8 additions & 0 deletions src/cache/inmemory/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,14 @@ export class Policies {
if (!fragment.typeCondition) return true;

const supertype = fragment.typeCondition.name.value;

invariant(
typename,
`Attempted to match fragment ${
fragment.kind === "InlineFragment" ? "" : fragment.name.value + " "
}with type condition ${supertype} against object with unknown __typename`,
);

if (typename === supertype) return true;

if (this.usingPossibleTypes) {
Expand Down

0 comments on commit 83b5837

Please sign in to comment.