Skip to content

Commit d57f097

Browse files
committed
✨ __typename support
1 parent 15ab8c2 commit d57f097

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-js-tree",
3-
"version": "0.3.3",
3+
"version": "0.3.4",
44
"private": false,
55
"license": "MIT",
66
"description": "GraphQL Parser providing simplier structure",

src/GqlParser/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { OperationType, ParserField, TypeDefinition } from '@/Models';
33
import { GqlParserTree, VariableDefinitionWithoutLoc } from '@/Models/GqlParserTree';
44
import { Parser } from '@/Parser';
55
import { TypeResolver } from '@/Parser/typeResolver';
6-
import { compileType, getTypeName } from '@/shared';
6+
import { compileType, createPlainField, getTypeName } from '@/shared';
77
import {
88
DefinitionNode,
99
parse,
@@ -123,7 +123,13 @@ export const parseGql = (gql: string, schema: string) => {
123123
};
124124

125125
const composeFieldNode = (s: FieldNode, parentNode: ParserField): GqlParserTree => {
126-
const fieldNode = parentNode.args.find((a) => a.name === s.name.value);
126+
const fieldNode =
127+
s.name.value === '__typename'
128+
? createPlainField({
129+
name: '__typename',
130+
type: 'String!',
131+
})
132+
: parentNode.args.find((a) => a.name === s.name.value);
127133
if (!fieldNode) {
128134
throw new Error(`Field "${s.name.value}" does not exist in "${parentNode.name}" node`);
129135
}

src/__tests__/GqlParser/GqlParserTreeToGql.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,38 @@ describe('Test generation of gql strings from the GqlParserTree', () => {
224224
const gqlParserResultVars = enrichGqlQueryWithAllVars(mockStartingQuery, mockSchema);
225225
expectTrimmedEqual(gqlParserResultVars, mockQueryVars);
226226
});
227+
228+
it('Works with __typename', () => {
229+
const mockQueryFragment = `
230+
query MyQuery {
231+
user{
232+
__typename
233+
}
234+
}`;
235+
const GqlTreeMockWithFragments: GqlParserTree[] = [
236+
{
237+
name: 'MyQuery',
238+
node: queryNode,
239+
operation: OperationType.query,
240+
children: [
241+
{
242+
name: 'user',
243+
node: queryNode.args[1],
244+
children: [
245+
{
246+
name: '__typename',
247+
node: createPlainField({ name: '__typename', type: 'String!' }),
248+
},
249+
],
250+
},
251+
],
252+
},
253+
];
254+
255+
const gqlParserResult = parseGqlTrees(GqlTreeMockWithFragments);
256+
expectTrimmedEqual(gqlParserResult, mockQueryFragment);
257+
});
258+
227259
it('Works with fragments and fragment spreads', () => {
228260
const mockQueryFragment = `
229261
fragment Full on User{
@@ -292,6 +324,7 @@ describe('Test generation of gql strings from the GqlParserTree', () => {
292324
const gqlParserResult = parseGqlTrees(GqlTreeMockWithFragments);
293325
expectTrimmedEqual(gqlParserResult, mockQueryFragment);
294326
});
327+
295328
it('works with inline fragments', () => {
296329
const mockQueryInline = `query MyQuery {
297330
namings{

src/__tests__/GqlParser/index.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,37 @@ describe('Test generation of GqlParserTrees from gql', () => {
194194
const gqlParserResult = parseGql(mockQueryVars, mockSchema);
195195
expect(gqlParserResult[0]).toEqual(GqlTreeMockWithVars);
196196
});
197+
it('Creates __typename node', () => {
198+
const mockQueryFragment = `
199+
query MyQuery {
200+
user{
201+
__typename
202+
}
203+
}`;
204+
const GqlTreeMockWithFragments: GqlParserTree[] = [
205+
{
206+
name: 'MyQuery',
207+
node: queryNode,
208+
operation: OperationType.query,
209+
children: [
210+
{
211+
name: 'user',
212+
node: queryNode.args[1],
213+
children: [
214+
{
215+
name: '__typename',
216+
node: createPlainField({ name: '__typename', type: 'String!' }),
217+
},
218+
],
219+
},
220+
],
221+
},
222+
];
223+
224+
const gqlParserResult = parseGql(mockQueryFragment, mockSchema);
225+
expect(gqlParserResult[0]).toEqual(GqlTreeMockWithFragments[0]);
226+
});
227+
197228
it('Creates gql with fragment and fragment spread', () => {
198229
const mockQueryFragment = `
199230
fragment Full on User{
@@ -263,6 +294,7 @@ describe('Test generation of GqlParserTrees from gql', () => {
263294
expect(gqlParserResult[0]).toEqual(GqlTreeMockWithFragments[0]);
264295
expect(gqlParserResult[1]).toEqual(GqlTreeMockWithFragments[1]);
265296
});
297+
266298
it('Parses inline fragments from gql on unions', () => {
267299
const mockQueryInline = `query MyQuery {
268300
namings{

0 commit comments

Comments
 (0)