Skip to content

Commit

Permalink
Prevent adding __typename to toplevel operations (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten authored Aug 21, 2019
1 parent fc6fccd commit 6685f43
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
19 changes: 9 additions & 10 deletions src/utils/typenames.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ describe('formatTypeNames', () => {
id
__typename
}
__typename
}
"
`);
Expand All @@ -29,15 +28,15 @@ describe('formatTypeNames', () => {
__typename
}`)
).toMatchInlineSnapshot(`
"{
todos {
id
__typename
}
__typename
}
"
`);
"{
todos {
id
__typename
}
__typename
}
"
`);
});
});

Expand Down
11 changes: 4 additions & 7 deletions src/utils/typenames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
DocumentNode,
FieldNode,
InlineFragmentNode,
OperationDefinitionNode,
Kind,
visit,
} from 'graphql';

Expand Down Expand Up @@ -35,9 +35,7 @@ const collectTypes = (obj: EntityLike | EntityLike[], types: string[] = []) => {
export const collectTypesFromResponse = (response: object) =>
collectTypes(response as EntityLike).filter((v, i, a) => a.indexOf(v) === i);

const formatNode = (
n: FieldNode | InlineFragmentNode | OperationDefinitionNode
) => {
const formatNode = (n: FieldNode | InlineFragmentNode) => {
if (n.selectionSet === undefined) {
return false;
}
Expand All @@ -57,9 +55,9 @@ const formatNode = (
selections: [
...n.selectionSet.selections,
{
kind: 'Field',
kind: Kind.FIELD,
name: {
kind: 'Name',
kind: Kind.NAME,
value: '__typename',
},
},
Expand All @@ -72,5 +70,4 @@ export const formatDocument = (astNode: DocumentNode) =>
visit(astNode, {
Field: formatNode,
InlineFragment: formatNode,
OperationDefinition: formatNode,
});

0 comments on commit 6685f43

Please sign in to comment.