@@ -18,8 +18,6 @@ import {
18
18
} from '../type/schema' ;
19
19
import {
20
20
type GraphQLType ,
21
- type GraphQLInputType ,
22
- type GraphQLOutputType ,
23
21
type GraphQLNamedType ,
24
22
isInputType ,
25
23
isOutputType ,
@@ -47,8 +45,6 @@ import {
47
45
type IntrospectionEnumType ,
48
46
type IntrospectionInputObjectType ,
49
47
type IntrospectionTypeRef ,
50
- type IntrospectionInputTypeRef ,
51
- type IntrospectionOutputTypeRef ,
52
48
type IntrospectionNamedTypeRef ,
53
49
} from './getIntrospectionQuery' ;
54
50
@@ -139,13 +135,17 @@ export function buildClientSchema(
139
135
const nullableType = getType ( nullableRef ) ;
140
136
return GraphQLNonNull ( assertNullableType ( nullableType ) ) ;
141
137
}
142
- if ( ! typeRef . name ) {
138
+ return getNamedType ( typeRef ) ;
139
+ }
140
+
141
+ function getNamedType (
142
+ typeRef : IntrospectionNamedTypeRef < > ,
143
+ ) : GraphQLNamedType {
144
+ const typeName = typeRef . name ;
145
+ if ( ! typeName ) {
143
146
throw new Error ( `Unknown type reference : ${inspect ( typeRef ) } . `) ;
144
147
}
145
- return getNamedType ( typeRef . name ) ;
146
- }
147
148
148
- function getNamedType ( typeName : string ) : GraphQLNamedType {
149
149
const type = typeMap [ typeName ] ;
150
150
if ( ! type ) {
151
151
throw new Error (
@@ -156,42 +156,16 @@ export function buildClientSchema(
156
156
return type ;
157
157
}
158
158
159
- function getInputType ( typeRef : IntrospectionInputTypeRef ) : GraphQLInputType {
160
- const type = getType ( typeRef ) ;
161
- if ( isInputType ( type ) ) {
162
- return type ;
163
- }
164
- const typeStr = inspect ( type ) ;
165
- throw new Error (
166
- `Introspection must provide input type for arguments , but received : ${typeStr } . `,
167
- ) ;
168
- }
169
-
170
- function getOutputType (
171
- typeRef : IntrospectionOutputTypeRef ,
172
- ) : GraphQLOutputType {
173
- const type = getType ( typeRef ) ;
174
- if ( isOutputType ( type ) ) {
175
- return type ;
176
- }
177
- const typeStr = inspect ( type ) ;
178
- throw new Error (
179
- `Introspection must provide output type for fields , but received : ${typeStr } . `,
180
- ) ;
181
- }
182
-
183
159
function getObjectType (
184
160
typeRef : IntrospectionNamedTypeRef < IntrospectionObjectType > ,
185
161
) : GraphQLObjectType {
186
- const type = getType ( typeRef ) ;
187
- return assertObjectType ( type ) ;
162
+ return assertObjectType ( getNamedType ( typeRef ) ) ;
188
163
}
189
164
190
165
function getInterfaceType (
191
- typeRef : IntrospectionTypeRef ,
166
+ typeRef : IntrospectionNamedTypeRef < IntrospectionInterfaceType > ,
192
167
) : GraphQLInterfaceType {
193
- const type = getType ( typeRef ) ;
194
- return assertInterfaceType ( type ) ;
168
+ return assertInterfaceType ( getNamedType ( typeRef ) ) ;
195
169
}
196
170
197
171
// Given a type's introspection result, construct the correct
@@ -335,26 +309,38 @@ export function buildClientSchema(
335
309
`Introspection result missing fields : ${inspect ( typeIntrospection ) } . `,
336
310
) ;
337
311
}
312
+
338
313
return keyValMap (
339
314
typeIntrospection . fields ,
340
315
fieldIntrospection => fieldIntrospection . name ,
341
- fieldIntrospection => {
342
- if ( ! fieldIntrospection . args ) {
343
- const fieldIntrospectionStr = inspect ( fieldIntrospection ) ;
344
- throw new Error (
345
- `Introspection result missing field args : ${fieldIntrospectionStr } . `,
346
- ) ;
347
- }
348
- return {
349
- description : fieldIntrospection . description ,
350
- deprecationReason : fieldIntrospection . deprecationReason ,
351
- type : getOutputType ( fieldIntrospection . type ) ,
352
- args : buildInputValueDefMap ( fieldIntrospection . args ) ,
353
- } ;
354
- } ,
316
+ buildField ,
355
317
) ;
356
318
}
357
319
320
+ function buildField ( fieldIntrospection ) {
321
+ const type = getType ( fieldIntrospection . type ) ;
322
+ if ( ! isOutputType ( type ) ) {
323
+ const typeStr = inspect ( type ) ;
324
+ throw new Error (
325
+ `Introspection must provide output type for fields , but received : ${typeStr } . `,
326
+ ) ;
327
+ }
328
+
329
+ if ( ! fieldIntrospection . args ) {
330
+ const fieldIntrospectionStr = inspect ( fieldIntrospection ) ;
331
+ throw new Error (
332
+ `Introspection result missing field args : ${fieldIntrospectionStr } . `,
333
+ ) ;
334
+ }
335
+
336
+ return {
337
+ description : fieldIntrospection . description ,
338
+ deprecationReason : fieldIntrospection . deprecationReason ,
339
+ type ,
340
+ args : buildInputValueDefMap ( fieldIntrospection . args ) ,
341
+ } ;
342
+ }
343
+
358
344
function buildInputValueDefMap ( inputValueIntrospections ) {
359
345
return keyValMap (
360
346
inputValueIntrospections ,
@@ -364,7 +350,14 @@ export function buildClientSchema(
364
350
}
365
351
366
352
function buildInputValue ( inputValueIntrospection ) {
367
- const type = getInputType ( inputValueIntrospection . type ) ;
353
+ const type = getType ( inputValueIntrospection . type ) ;
354
+ if ( ! isInputType ( type ) ) {
355
+ const typeStr = inspect ( type ) ;
356
+ throw new Error (
357
+ `Introspection must provide input type for arguments , but received : ${typeStr } . `,
358
+ ) ;
359
+ }
360
+
368
361
const defaultValue =
369
362
inputValueIntrospection . defaultValue != null
370
363
? valueFromAST ( parseValue ( inputValueIntrospection . defaultValue ) , type )
0 commit comments