@@ -46,6 +46,7 @@ import {
46
46
type GraphQLArgumentConfig ,
47
47
type GraphQLEnumValueConfig ,
48
48
type GraphQLInputFieldConfig ,
49
+ type GraphQLFieldResolver ,
49
50
GraphQLScalarType ,
50
51
GraphQLObjectType ,
51
52
GraphQLInterfaceType ,
@@ -72,6 +73,10 @@ import {
72
73
GraphQLSchema ,
73
74
} from '../type/schema' ;
74
75
76
+ export type TypeFieldResolverMap = ObjMap <
77
+ ObjMap < GraphQLFieldResolver < mixed , mixed , mixed >> ,
78
+ > ;
79
+
75
80
export type BuildSchemaOptions = {
76
81
...GraphQLSchemaValidationOptions ,
77
82
@@ -92,6 +97,13 @@ export type BuildSchemaOptions = {
92
97
*/
93
98
assumeValidSDL ?: boolean ,
94
99
100
+ /**
101
+ * Object map of object maps to resolver funtions.
102
+ *
103
+ * Default: undefined
104
+ */
105
+ resolvers ?: TypeFieldResolverMap ,
106
+
95
107
...
96
108
} ;
97
109
@@ -243,14 +255,26 @@ export class ASTDefinitionBuilder {
243
255
} ) ;
244
256
}
245
257
246
- buildField ( field : FieldDefinitionNode ) : GraphQLFieldConfig < mixed , mixed > {
258
+ buildField (
259
+ field : FieldDefinitionNode ,
260
+ typeName ?: string ,
261
+ ) : GraphQLFieldConfig < mixed , mixed > {
262
+ const resolve =
263
+ ( typeName &&
264
+ this . _options &&
265
+ this . _options . resolvers &&
266
+ this . _options . resolvers [ typeName ] &&
267
+ this . _options . resolvers [ typeName ] [ field . name . value ] ) ||
268
+ undefined ;
269
+
247
270
return {
248
271
// Note: While this could make assertions to get the correctly typed
249
272
// value, that would throw immediately while type system validation
250
273
// with validateSchema() will produce more actionable results.
251
274
type : ( this . getWrappedType ( field . type ) : any ) ,
252
275
description : getDescription ( field , this . _options ) ,
253
276
args : keyByNameNode ( field . arguments || [ ] , arg => this . buildArg ( arg ) ) ,
277
+ resolve,
254
278
deprecationReason : getDeprecationReason ( field ) ,
255
279
astNode : field ,
256
280
} ;
@@ -330,13 +354,15 @@ export class ASTDefinitionBuilder {
330
354
? ( ) => interfaceNodes . map ( ref => ( this . getNamedType ( ref ) : any ) )
331
355
: [ ] ;
332
356
357
+ const name = astNode . name . value ;
358
+
333
359
const fields =
334
360
fieldNodes && fieldNodes . length > 0
335
- ? ( ) => keyByNameNode ( fieldNodes , field => this . buildField ( field ) )
361
+ ? ( ) => keyByNameNode ( fieldNodes , field => this . buildField ( field , name ) )
336
362
: Object . create ( null ) ;
337
363
338
364
return new GraphQLObjectType ( {
339
- name : astNode . name . value ,
365
+ name,
340
366
description : getDescription ( astNode , this . _options ) ,
341
367
interfaces,
342
368
fields,
@@ -346,14 +372,15 @@ export class ASTDefinitionBuilder {
346
372
347
373
_makeInterfaceDef ( astNode : InterfaceTypeDefinitionNode ) {
348
374
const fieldNodes = astNode . fields ;
375
+ const name = astNode . name . value ;
349
376
350
377
const fields =
351
378
fieldNodes && fieldNodes . length > 0
352
- ? ( ) => keyByNameNode ( fieldNodes , field => this . buildField ( field ) )
379
+ ? ( ) => keyByNameNode ( fieldNodes , field => this . buildField ( field , name ) )
353
380
: Object . create ( null ) ;
354
381
355
382
return new GraphQLInterfaceType ( {
356
- name : astNode . name . value ,
383
+ name,
357
384
description : getDescription ( astNode , this . _options ) ,
358
385
fields,
359
386
astNode,
0 commit comments