9
9
10
10
import { forEach , isCollection } from 'iterall' ;
11
11
import { GraphQLError , locatedError } from '../error' ;
12
- import getPromise from '../jsutils/getPromise' ;
13
12
import invariant from '../jsutils/invariant' ;
14
13
import isInvalid from '../jsutils/isInvalid' ;
15
14
import isNullish from '../jsutils/isNullish' ;
15
+ import isPromise from '../jsutils/isPromise' ;
16
16
import memoize3 from '../jsutils/memoize3' ;
17
17
import promiseForObject from '../jsutils/promiseForObject' ;
18
18
import promiseReduce from '../jsutils/promiseReduce' ;
@@ -229,9 +229,8 @@ function buildResponse(
229
229
context : ExecutionContext ,
230
230
data : MaybePromise < ObjMap < mixed > | null> ,
231
231
) {
232
- const promise = getPromise ( data ) ;
233
- if ( promise ) {
234
- return promise . then ( resolved => buildResponse ( context , resolved ) ) ;
232
+ if ( isPromise ( data ) ) {
233
+ return data . then ( resolved => buildResponse ( context , resolved ) ) ;
235
234
}
236
235
return context.errors.length === 0
237
236
? { data }
@@ -403,9 +402,8 @@ function executeOperation(
403
402
operation . operation === 'mutation'
404
403
? executeFieldsSerially ( exeContext , type , rootValue , path , fields )
405
404
: executeFields ( exeContext , type , rootValue , path , fields ) ;
406
- const promise = getPromise ( result ) ;
407
- if ( promise ) {
408
- return promise . then ( undefined , error => {
405
+ if ( isPromise ( result ) ) {
406
+ return result . then ( undefined , error => {
409
407
exeContext . errors . push ( error ) ;
410
408
return Promise . resolve ( null ) ;
411
409
} ) ;
@@ -484,9 +482,8 @@ function executeFieldsSerially(
484
482
if ( result === undefined ) {
485
483
return results ;
486
484
}
487
- const promise = getPromise ( result ) ;
488
- if ( promise ) {
489
- return promise . then ( resolvedResult => {
485
+ if ( isPromise ( result ) ) {
486
+ return result . then ( resolvedResult => {
490
487
results [ responseName ] = resolvedResult ;
491
488
return results ;
492
489
} ) ;
@@ -525,7 +522,7 @@ function executeFields(
525
522
return results ;
526
523
}
527
524
results [ responseName ] = result ;
528
- if ( getPromise ( result ) ) {
525
+ if ( ! containsPromise && isPromise ( result ) ) {
529
526
containsPromise = true ;
530
527
}
531
528
return results ;
@@ -684,7 +681,7 @@ function resolveField(
684
681
source: mixed,
685
682
fieldNodes: $ReadOnlyArray< FieldNode > ,
686
683
path: ResponsePath,
687
- ): mixed {
684
+ ): MaybePromise < mixed > {
688
685
const fieldNode = fieldNodes [ 0 ] ;
689
686
const fieldName = fieldNode . name . value ;
690
687
@@ -773,8 +770,7 @@ export function resolveFieldValueOrError<TSource>(
773
770
const context = exeContext . contextValue ;
774
771
775
772
const result = resolveFn ( source , args , context , info ) ;
776
- const promise = getPromise ( result ) ;
777
- return promise ? promise . then ( undefined , asErrorInstance ) : result ;
773
+ return isPromise ( result ) ? result . then ( undefined , asErrorInstance ) : result ;
778
774
} catch ( error ) {
779
775
return asErrorInstance ( error ) ;
780
776
}
@@ -795,7 +791,7 @@ function completeValueCatchingError(
795
791
info: GraphQLResolveInfo,
796
792
path: ResponsePath,
797
793
result: mixed,
798
- ): mixed {
794
+ ): MaybePromise < mixed > {
799
795
// If the field type is non-nullable, then it is resolved without any
800
796
// protection from errors, however it still properly locates the error.
801
797
if ( isNonNullType ( returnType ) ) {
@@ -820,13 +816,12 @@ function completeValueCatchingError(
820
816
path ,
821
817
result ,
822
818
) ;
823
- const promise = getPromise ( completed ) ;
824
- if ( promise ) {
819
+ if ( isPromise ( completed ) ) {
825
820
// If `completeValueWithLocatedError` returned a rejected promise, log
826
821
// the rejection error and resolve to null.
827
822
// Note: we don't rely on a `catch` method, but we do expect "thenable"
828
823
// to take a second callback for the error case.
829
- return promise . then ( undefined , error => {
824
+ return completed . then ( undefined , error => {
830
825
exeContext . errors . push ( error ) ;
831
826
return Promise . resolve ( null ) ;
832
827
} ) ;
@@ -849,7 +844,7 @@ function completeValueWithLocatedError(
849
844
info: GraphQLResolveInfo,
850
845
path: ResponsePath,
851
846
result: mixed,
852
- ): mixed {
847
+ ): MaybePromise < mixed > {
853
848
try {
854
849
const completed = completeValue (
855
850
exeContext ,
@@ -859,9 +854,8 @@ function completeValueWithLocatedError(
859
854
path ,
860
855
result ,
861
856
) ;
862
- const promise = getPromise ( completed ) ;
863
- if ( promise ) {
864
- return promise . then ( undefined , error =>
857
+ if ( isPromise ( completed ) ) {
858
+ return completed . then ( undefined , error =>
865
859
Promise . reject (
866
860
locatedError (
867
861
asErrorInstance ( error ) ,
@@ -909,11 +903,10 @@ function completeValue(
909
903
info: GraphQLResolveInfo,
910
904
path: ResponsePath,
911
905
result: mixed,
912
- ): mixed {
906
+ ): MaybePromise < mixed > {
913
907
// If result is a Promise, apply-lift over completeValue.
914
- const promise = getPromise ( result ) ;
915
- if ( promise ) {
916
- return promise . then ( resolved =>
908
+ if ( isPromise ( result ) ) {
909
+ return result . then ( resolved =>
917
910
completeValue ( exeContext , returnType , fieldNodes , info , path , resolved ) ,
918
911
) ;
919
912
}
@@ -1012,7 +1005,7 @@ function completeListValue(
1012
1005
info: GraphQLResolveInfo,
1013
1006
path: ResponsePath,
1014
1007
result: mixed,
1015
- ) : mixed {
1008
+ ): MaybePromise < $ReadOnlyArray < mixed > > {
1016
1009
invariant (
1017
1010
isCollection ( result ) ,
1018
1011
`Expected Iterable, but did not find one for field ${
@@ -1038,7 +1031,7 @@ function completeListValue(
1038
1031
item ,
1039
1032
) ;
1040
1033
1041
- if ( ! containsPromise && getPromise ( completedItem ) ) {
1034
+ if ( ! containsPromise && isPromise ( completedItem ) ) {
1042
1035
containsPromise = true ;
1043
1036
}
1044
1037
completedResults . push ( completedItem ) ;
@@ -1074,14 +1067,13 @@ function completeAbstractValue(
1074
1067
info: GraphQLResolveInfo,
1075
1068
path: ResponsePath,
1076
1069
result: mixed,
1077
- ) : mixed {
1070
+ ): MaybePromise < ObjMap < mixed > > {
1078
1071
const runtimeType = returnType . resolveType
1079
1072
? returnType . resolveType ( result , exeContext . contextValue , info )
1080
1073
: defaultResolveTypeFn ( result , exeContext . contextValue , info , returnType ) ;
1081
1074
1082
- const promise = getPromise ( runtimeType ) ;
1083
- if ( promise ) {
1084
- return promise . then ( resolvedRuntimeType =>
1075
+ if ( isPromise ( runtimeType ) ) {
1076
+ return runtimeType . then ( resolvedRuntimeType =>
1085
1077
completeObjectValue (
1086
1078
exeContext ,
1087
1079
ensureValidRuntimeType (
@@ -1103,7 +1095,7 @@ function completeAbstractValue(
1103
1095
return completeObjectValue (
1104
1096
exeContext ,
1105
1097
ensureValidRuntimeType (
1106
- ( ( runtimeType : any ) : ? GraphQLObjectType | string ) ,
1098
+ runtimeType ,
1107
1099
exeContext ,
1108
1100
returnType ,
1109
1101
fieldNodes ,
@@ -1163,17 +1155,16 @@ function completeObjectValue(
1163
1155
info : GraphQLResolveInfo ,
1164
1156
path : ResponsePath ,
1165
1157
result : mixed ,
1166
- ) : mixed {
1158
+ ) : MaybePromise < ObjMap < mixed >> {
1167
1159
// If there is an isTypeOf predicate function, call it with the
1168
1160
// current result. If isTypeOf returns false, then raise an error rather
1169
1161
// than continuing execution.
1170
1162
if ( returnType . isTypeOf ) {
1171
1163
const isTypeOf = returnType . isTypeOf ( result , exeContext . contextValue , info ) ;
1172
1164
1173
- const promise = getPromise ( isTypeOf ) ;
1174
- if ( promise ) {
1175
- return promise . then ( isTypeOfResult => {
1176
- if ( ! isTypeOfResult ) {
1165
+ if ( isPromise ( isTypeOf ) ) {
1166
+ return isTypeOf . then ( resolvedIsTypeOf => {
1167
+ if ( ! resolvedIsTypeOf ) {
1177
1168
throw invalidReturnTypeError ( returnType , result , fieldNodes ) ;
1178
1169
}
1179
1170
return collectAndExecuteSubfields (
@@ -1220,7 +1211,7 @@ function collectAndExecuteSubfields(
1220
1211
info : GraphQLResolveInfo ,
1221
1212
path : ResponsePath ,
1222
1213
result : mixed ,
1223
- ) : mixed {
1214
+ ) : MaybePromise < ObjMap < mixed >> {
1224
1215
// Collect sub-fields to execute to complete this value.
1225
1216
const subFieldNodes = collectSubfields ( exeContext , returnType , fieldNodes ) ;
1226
1217
return executeFields ( exeContext , returnType , result , path , subFieldNodes ) ;
@@ -1289,9 +1280,8 @@ function defaultResolveTypeFn(
1289
1280
if ( type . isTypeOf ) {
1290
1281
const isTypeOfResult = type . isTypeOf ( value , context , info ) ;
1291
1282
1292
- const promise = getPromise ( isTypeOfResult ) ;
1293
- if ( promise ) {
1294
- promisedIsTypeOfResults [ i ] = promise ;
1283
+ if ( isPromise ( isTypeOfResult ) ) {
1284
+ promisedIsTypeOfResults [ i ] = isTypeOfResult ;
1295
1285
} else if ( isTypeOfResult ) {
1296
1286
return type ;
1297
1287
}
0 commit comments