Skip to content

Commit 50c3c81

Browse files
rasulomaroffbrainkim
authored andcommitted
changes the way the parser filters the array of definitions
1 parent 9eb93c7 commit 50c3c81

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

src/react/parser/index.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,31 @@ export function parser(document: DocumentNode): IDocumentDefinition {
5151
`to convert your operation into a document`
5252
);
5353

54-
const fragments = document.definitions.filter(
55-
(x: DefinitionNode) => x.kind === 'FragmentDefinition'
56-
);
57-
58-
const queries = document.definitions.filter(
59-
(x: DefinitionNode) =>
60-
x.kind === 'OperationDefinition' && x.operation === 'query'
61-
);
62-
63-
const mutations = document.definitions.filter(
64-
(x: DefinitionNode) =>
65-
x.kind === 'OperationDefinition' && x.operation === 'mutation'
66-
);
67-
68-
const subscriptions = document.definitions.filter(
69-
(x: DefinitionNode) =>
70-
x.kind === 'OperationDefinition' && x.operation === 'subscription'
71-
);
54+
const fragments: DefinitionNode[] = []
55+
const queries: DefinitionNode[] = []
56+
const mutations: DefinitionNode[] = []
57+
const subscriptions: DefinitionNode[] = []
58+
59+
for (const x of document.definitions) {
60+
if (x.kind === 'FragmentDefinition') {
61+
fragments.push(x);
62+
continue
63+
}
64+
65+
if (x.kind === 'OperationDefinition') {
66+
switch (x.operation) {
67+
case 'query':
68+
queries.push(x);
69+
break;
70+
case 'mutation':
71+
mutations.push(x);
72+
break;
73+
case 'subscription':
74+
subscriptions.push(x);
75+
break;
76+
}
77+
}
78+
}
7279

7380
invariant(
7481
!fragments.length ||

0 commit comments

Comments
 (0)