1
- import { GraphQLNonNull , GraphQLOutputType , GraphQLType , isNonNullType } from 'graphql/type/definition.js'
2
- import { typeFromAST , TypeInfo , visitWithTypeInfo } from 'graphql/utilities/index.js'
1
+ import {
2
+ GraphQLOutputType ,
3
+ GraphQLType ,
4
+ isNonNullType ,
5
+ } from 'graphql/type/definition.js'
6
+ import {
7
+ typeFromAST ,
8
+ TypeInfo ,
9
+ visitWithTypeInfo ,
10
+ } from 'graphql/utilities/index.js'
3
11
import { parse , print , Source , visit } from 'graphql/language/index.js'
4
12
import { GraphQLSchema } from 'graphql/type/index.js'
5
13
import { GraphQLError } from 'graphql/error/index.js'
@@ -23,7 +31,7 @@ export type Selector = {
23
31
24
32
export type Content = {
25
33
operations : Selector [ ]
26
- fragments : Selector [ ]
34
+ fragments : Map < string , Selector >
27
35
}
28
36
29
37
export function traverse ( schema : GraphQLSchema , source : Source ) : Content {
@@ -32,19 +40,19 @@ export function traverse(schema: GraphQLSchema, source: Source): Content {
32
40
33
41
const content : Content = {
34
42
operations : [ ] ,
35
- fragments : [ ]
43
+ fragments : new Map ( ) ,
36
44
}
37
45
38
46
const stack : Selector [ ] = [ ]
39
47
40
48
const visitor = visitWithTypeInfo ( typeInfo , {
41
49
OperationDefinition : {
42
- enter : function ( node ) {
50
+ enter : function ( node ) {
43
51
if ( node . name === undefined ) {
44
52
throw new GraphQLError (
45
53
firstLetterUpper ( node . operation ) + ' name is required' ,
46
54
node ,
47
- source
55
+ source ,
48
56
)
49
57
}
50
58
checkUnique ( node . name . value , content )
@@ -55,7 +63,7 @@ export function traverse(schema: GraphQLSchema, source: Source): Content {
55
63
variables . push ( {
56
64
name : v . variable . name . value ,
57
65
type : type ,
58
- required : v . defaultValue === undefined && isNonNullType ( type )
66
+ required : v . defaultValue === undefined && isNonNullType ( type ) ,
59
67
} )
60
68
}
61
69
@@ -65,15 +73,15 @@ export function traverse(schema: GraphQLSchema, source: Source): Content {
65
73
fields : [ ] ,
66
74
inlineFragments : [ ] ,
67
75
variables : variables ,
68
- source : print ( node )
76
+ source : print ( node ) ,
69
77
}
70
78
71
79
stack . push ( s )
72
80
content . operations . push ( s )
73
81
} ,
74
82
leave ( ) {
75
83
stack . pop ( )
76
- }
84
+ } ,
77
85
} ,
78
86
79
87
FragmentDefinition : {
@@ -85,15 +93,15 @@ export function traverse(schema: GraphQLSchema, source: Source): Content {
85
93
type : typeInfo . getType ( ) ?? undefined ,
86
94
fields : [ ] ,
87
95
inlineFragments : [ ] ,
88
- source : print ( node )
96
+ source : print ( node ) ,
89
97
}
90
98
91
99
stack . push ( s )
92
- content . fragments . push ( s )
100
+ content . fragments . set ( s . name , s )
93
101
} ,
94
102
leave ( ) {
95
103
stack . pop ( )
96
- }
104
+ } ,
97
105
} ,
98
106
99
107
Field : {
@@ -109,7 +117,7 @@ export function traverse(schema: GraphQLSchema, source: Source): Content {
109
117
} ,
110
118
leave ( ) {
111
119
stack . pop ( )
112
- }
120
+ } ,
113
121
} ,
114
122
115
123
FragmentSpread : {
@@ -121,13 +129,17 @@ export function traverse(schema: GraphQLSchema, source: Source): Content {
121
129
fields : [ ] ,
122
130
inlineFragments : [ ] ,
123
131
} )
124
- }
132
+ } ,
125
133
} ,
126
134
127
135
InlineFragment : {
128
136
enter ( node ) {
129
137
if ( ! node . typeCondition ) {
130
- throw new GraphQLError ( 'Inline fragment must have type condition.' , node , source )
138
+ throw new GraphQLError (
139
+ 'Inline fragment must have type condition.' ,
140
+ node ,
141
+ source ,
142
+ )
131
143
}
132
144
const s : Selector = {
133
145
name : node . typeCondition . name . value ,
@@ -141,7 +153,7 @@ export function traverse(schema: GraphQLSchema, source: Source): Content {
141
153
leave ( ) {
142
154
stack . pop ( )
143
155
} ,
144
- }
156
+ } ,
145
157
} )
146
158
147
159
visit ( ast , visitor )
@@ -153,7 +165,7 @@ function checkUnique(name: string, content: Content) {
153
165
if ( content . operations . find ( ( o ) => o . name === name ) ) {
154
166
throw new GraphQLError ( `Operation with name "${ name } " is already defined.` )
155
167
}
156
- if ( content . fragments . find ( ( f ) => f . name === name ) ) {
168
+ if ( content . fragments . has ( name ) ) {
157
169
throw new GraphQLError ( `Fragment with name "${ name } " is already defined.` )
158
170
}
159
171
}
0 commit comments