@@ -80,7 +80,7 @@ import type {
80
80
* Namely, schema of the type system that is currently executing,
81
81
* and the fragments defined in the query document
82
82
*/
83
- type ExecutionContext = {
83
+ export type ExecutionContext = {
84
84
schema : GraphQLSchema ;
85
85
fragments: { [ key : string ] : FragmentDefinitionNode } ;
86
86
rootValue: mixed ;
@@ -117,22 +117,6 @@ export function execute(
117
117
variableValues ?: ?{ [ key : string ] : mixed } ,
118
118
operationName ?: ?string
119
119
) : Promise < ExecutionResult > {
120
- invariant ( schema , 'Must provide schema' ) ;
121
- invariant ( document , 'Must provide document' ) ;
122
- invariant (
123
- schema instanceof GraphQLSchema ,
124
- 'Schema must be an instance of GraphQLSchema. Also ensure that there are ' +
125
- 'not multiple versions of GraphQL installed in your node_modules directory.'
126
- ) ;
127
-
128
- // Variables, if provided, must be an object.
129
- invariant (
130
- ! variableValues || typeof variableValues === 'object' ,
131
- 'Variables must be provided as an Object where each property is a ' +
132
- 'variable value. Perhaps look to see if an unparsed JSON string ' +
133
- 'was provided.'
134
- ) ;
135
-
136
120
// If a valid context cannot be created due to incorrect arguments,
137
121
// this will throw an error.
138
122
const context = buildExecutionContext (
@@ -183,8 +167,11 @@ export function responsePathAsArray(
183
167
return flattened . reverse ( ) ;
184
168
}
185
169
186
-
187
- function addPath ( prev : ResponsePath , key : string | number ) {
170
+ /**
171
+ * Given a ResponsePath and a key, return a new ResponsePath containing the
172
+ * new key.
173
+ */
174
+ export function addPath ( prev : ResponsePath , key : string | number ) {
188
175
return { prev , key } ;
189
176
}
190
177
@@ -194,14 +181,30 @@ function addPath(prev: ResponsePath, key: string | number) {
194
181
*
195
182
* Throws a GraphQLError if a valid execution context cannot be created.
196
183
*/
197
- function buildExecutionContext (
184
+ export function buildExecutionContext (
198
185
schema : GraphQLSchema ,
199
186
document : DocumentNode ,
200
187
rootValue : mixed ,
201
188
contextValue : mixed ,
202
189
rawVariableValues : ?{ [ key : string ] : mixed } ,
203
190
operationName : ?string
204
191
) : ExecutionContext {
192
+ invariant ( schema , 'Must provide schema' ) ;
193
+ invariant ( document , 'Must provide document' ) ;
194
+ invariant (
195
+ schema instanceof GraphQLSchema ,
196
+ 'Schema must be an instance of GraphQLSchema. Also ensure that there are ' +
197
+ 'not multiple versions of GraphQL installed in your node_modules directory.'
198
+ ) ;
199
+
200
+ // Variables, if provided, must be an object.
201
+ invariant (
202
+ ! rawVariableValues || typeof rawVariableValues === 'object' ,
203
+ 'Variables must be provided as an Object where each property is a ' +
204
+ 'variable value. Perhaps look to see if an unparsed JSON string ' +
205
+ 'was provided.'
206
+ ) ;
207
+
205
208
const errors : Array < GraphQLError > = [ ] ;
206
209
let operation : ?OperationDefinitionNode ;
207
210
const fragments : { [ name : string ] : FragmentDefinitionNode } =
@@ -280,7 +283,7 @@ function executeOperation(
280
283
/**
281
284
* Extracts the root type of the operation from the schema.
282
285
*/
283
- function getOperationRootType(
286
+ export function getOperationRootType (
284
287
schema : GraphQLSchema ,
285
288
operation : OperationDefinitionNode
286
289
) : GraphQLObjectType {
@@ -408,7 +411,7 @@ function executeFields(
408
411
* returns an Interface or Union type, the "runtime type" will be the actual
409
412
* Object type returned by that field.
410
413
*/
411
- function collectFields (
414
+ export function collectFields (
412
415
exeContext : ExecutionContext ,
413
416
runtimeType : GraphQLObjectType ,
414
417
selectionSet : SelectionSetNode ,
@@ -577,60 +580,68 @@ function resolveField(
577
580
return ;
578
581
}
579
582
580
- const returnType = fieldDef . type ;
581
583
const resolveFn = fieldDef . resolve || defaultFieldResolver ;
582
584
583
- // The resolve function's optional third argument is a context value that
584
- // is provided to every resolve function within an execution. It is commonly
585
- // used to represent an authenticated user, or request-specific caches.
586
- const context = exeContext . contextValue ;
587
-
588
- // The resolve function's optional fourth argument is a collection of
589
- // information about the current execution state.
590
- const info : GraphQLResolveInfo = {
591
- fieldName,
585
+ const info = buildResolveInfo (
586
+ exeContext ,
587
+ fieldDef ,
592
588
fieldNodes ,
593
- returnType,
594
589
parentType ,
595
- path,
596
- schema : exeContext . schema ,
597
- fragments : exeContext . fragments ,
598
- rootValue : exeContext . rootValue ,
599
- operation : exeContext . operation ,
600
- variableValues : exeContext . variableValues ,
601
- } ;
590
+ path
591
+ ) ;
602
592
603
593
// Get the resolve function, regardless of if its result is normal
604
594
// or abrupt (error).
605
- const result = resolveOrError (
595
+ const result = resolveFieldValueOrError (
606
596
exeContext ,
607
597
fieldDef ,
608
- fieldNode ,
598
+ fieldNodes ,
609
599
resolveFn ,
610
600
source ,
611
- context ,
612
601
info
613
602
) ;
614
603
615
604
return completeValueCatchingError (
616
605
exeContext ,
617
- returnType ,
606
+ fieldDef . type ,
618
607
fieldNodes ,
619
608
info ,
620
609
path ,
621
610
result
622
611
) ;
623
612
}
624
613
614
+ export function buildResolveInfo (
615
+ exeContext : ExecutionContext ,
616
+ fieldDef : GraphQLField < * , * > ,
617
+ fieldNodes : Array < FieldNode > ,
618
+ parentType : GraphQLObjectType ,
619
+ path : ResponsePath
620
+ ) : GraphQLResolveInfo {
621
+ // The resolve function's optional fourth argument is a collection of
622
+ // information about the current execution state.
623
+ return {
624
+ fieldName : fieldNodes [ 0 ] . name . value ,
625
+ fieldNodes ,
626
+ returnType : fieldDef . type ,
627
+ parentType ,
628
+ path ,
629
+ schema : exeContext . schema ,
630
+ fragments : exeContext . fragments ,
631
+ rootValue : exeContext . rootValue ,
632
+ operation : exeContext . operation ,
633
+ variableValues : exeContext . variableValues ,
634
+ } ;
635
+ }
636
+
625
637
// Isolates the "ReturnOrAbrupt" behavior to not de-opt the `resolveField`
626
638
// function. Returns the result of resolveFn or the abrupt-return Error object.
627
- function resolveOrError < TSource , TContext > (
639
+ export function resolveFieldValueOrError < TSource > (
628
640
exeContext : ExecutionContext ,
629
- fieldDef: GraphQLField< TSource , TContext > ,
630
- fieldNode: FieldNode,
631
- resolveFn: GraphQLFieldResolver< TSource , TContext > ,
641
+ fieldDef : GraphQLField < TSource , * > ,
642
+ fieldNodes : Array < FieldNode > ,
643
+ resolveFn : GraphQLFieldResolver < TSource , * > ,
632
644
source : TSource ,
633
- context: TContext,
634
645
info : GraphQLResolveInfo
635
646
) : Error | mixed {
636
647
try {
@@ -639,10 +650,15 @@ function resolveOrError<TSource, TContext>(
639
650
// TODO: find a way to memoize, in case this field is within a List type.
640
651
const args = getArgumentValues (
641
652
fieldDef ,
642
- fieldNode ,
653
+ fieldNodes [ 0 ] ,
643
654
exeContext . variableValues
644
655
) ;
645
656
657
+ // The resolve function's optional third argument is a context value that
658
+ // is provided to every resolve function within an execution. It is commonly
659
+ // used to represent an authenticated user, or request-specific caches.
660
+ const context = exeContext . contextValue ;
661
+
646
662
return resolveFn ( source , args , context , info ) ;
647
663
} catch ( error ) {
648
664
// Sometimes a non-error is thrown, wrap it as an Error for a
@@ -1178,7 +1194,7 @@ function getPromise<T>(value: Promise<T> | mixed): Promise<T> | void {
1178
1194
* added to the query type, but that would require mutating type
1179
1195
* definitions, which would cause issues.
1180
1196
*/
1181
- function getFieldDef (
1197
+ export function getFieldDef (
1182
1198
schema : GraphQLSchema ,
1183
1199
parentType : GraphQLObjectType ,
1184
1200
fieldName : string
0 commit comments