27
27
final class ExecutionContext {
28
28
let schema : GraphQLSchema
29
29
let fragments : [ String : FragmentDefinition ]
30
- let rootValue : MapRepresentable
31
- let contextValue : MapRepresentable
30
+ let rootValue : Any
31
+ let contextValue : Any
32
32
let operation : OperationDefinition
33
33
let variableValues : [ String : Map ]
34
34
var errors : [ GraphQLError ]
35
35
36
36
init (
37
37
schema: GraphQLSchema ,
38
38
fragments: [ String : FragmentDefinition ] ,
39
- rootValue: MapRepresentable ,
40
- contextValue: MapRepresentable ,
39
+ rootValue: Any ,
40
+ contextValue: Any ,
41
41
operation: OperationDefinition ,
42
42
variableValues: [ String : Map ] ,
43
43
errors: [ GraphQLError ]
@@ -62,8 +62,8 @@ final class ExecutionContext {
62
62
func execute(
63
63
schema: GraphQLSchema ,
64
64
documentAST: Document ,
65
- rootValue: MapRepresentable ,
66
- contextValue: MapRepresentable ,
65
+ rootValue: Any ,
66
+ contextValue: Any ,
67
67
variableValues: [ String : Map ] = [ : ] ,
68
68
operationName: String ? = nil
69
69
) throws -> Map {
@@ -88,7 +88,7 @@ func execute(
88
88
var dataMap : Map = [ : ]
89
89
90
90
for (key, value) in data {
91
- dataMap [ key] = value . map
91
+ dataMap [ key] = try map ( from : value )
92
92
}
93
93
94
94
var result : [ String : Map ] = [ " data " : dataMap]
@@ -112,8 +112,8 @@ func execute(
112
112
func buildExecutionContext(
113
113
schema: GraphQLSchema ,
114
114
documentAST: Document ,
115
- rootValue: MapRepresentable ,
116
- contextValue: MapRepresentable ,
115
+ rootValue: Any ,
116
+ contextValue: Any ,
117
117
rawVariableValues: [ String : Map ] ,
118
118
operationName: String ?
119
119
) throws -> ExecutionContext {
@@ -176,8 +176,8 @@ func buildExecutionContext(
176
176
func executeOperation(
177
177
exeContext: ExecutionContext ,
178
178
operation: OperationDefinition ,
179
- rootValue: MapRepresentable
180
- ) throws -> [ String : MapRepresentable ] {
179
+ rootValue: Any
180
+ ) throws -> [ String : Any ] {
181
181
let type = try getOperationRootType ( schema: exeContext. schema, operation: operation)
182
182
183
183
var inputFields : [ String : [ Field ] ] = [ : ]
@@ -250,10 +250,10 @@ func getOperationRootType(
250
250
func executeFieldsSerially(
251
251
exeContext: ExecutionContext ,
252
252
parentType: GraphQLObjectType ,
253
- sourceValue: MapRepresentable ,
253
+ sourceValue: Any ,
254
254
path: [ IndexPathElement ] ,
255
255
fields: [ String : [ Field ] ]
256
- ) throws -> [ String : MapRepresentable ] {
256
+ ) throws -> [ String : Any ] {
257
257
return try fields. reduce ( [ : ] ) { results, field in
258
258
var results = results
259
259
let fieldASTs = field. value
@@ -267,7 +267,7 @@ func executeFieldsSerially(
267
267
path: fieldPath
268
268
)
269
269
270
- results [ field. key] = result
270
+ results [ field. key] = result ?? Map . null
271
271
return results
272
272
}
273
273
}
@@ -279,10 +279,10 @@ func executeFieldsSerially(
279
279
func executeFields(
280
280
exeContext: ExecutionContext ,
281
281
parentType: GraphQLObjectType ,
282
- sourceValue: MapRepresentable ,
282
+ sourceValue: Any ,
283
283
path: [ IndexPathElement ] ,
284
284
fields: [ String : [ Field ] ]
285
- ) throws -> [ String : MapRepresentable ] {
285
+ ) throws -> [ String : Any ] {
286
286
return try executeFieldsSerially (
287
287
exeContext: exeContext,
288
288
parentType: parentType,
@@ -470,10 +470,10 @@ func getFieldEntryKey(node: Field) -> String {
470
470
func resolveField(
471
471
exeContext: ExecutionContext ,
472
472
parentType: GraphQLObjectType ,
473
- source: MapRepresentable ,
473
+ source: Any ,
474
474
fieldASTs: [ Field ] ,
475
475
path: [ IndexPathElement ]
476
- ) throws -> MapRepresentable {
476
+ ) throws -> Any ? {
477
477
let fieldAST = fieldASTs [ 0 ]
478
478
let fieldName = fieldAST. name. value
479
479
@@ -536,17 +536,17 @@ func resolveField(
536
536
}
537
537
538
538
enum ResultOrError {
539
- case result( MapRepresentable )
539
+ case result( Any ? )
540
540
case error( Error )
541
541
}
542
542
543
543
// Isolates the "ReturnOrAbrupt" behavior to not de-opt the `resolveField`
544
544
// function. Returns the result of `resolve` or the abrupt-return Error object.
545
545
func resolveOrError(
546
546
resolve: GraphQLFieldResolve ,
547
- source: MapRepresentable ,
547
+ source: Any ,
548
548
args: Map ,
549
- context: MapRepresentable ,
549
+ context: Any ,
550
550
info: GraphQLResolveInfo
551
551
) -> ResultOrError {
552
552
do {
@@ -565,7 +565,7 @@ func completeValueCatchingError(
565
565
info: GraphQLResolveInfo ,
566
566
path: [ IndexPathElement ] ,
567
567
result: ResultOrError
568
- ) throws -> MapRepresentable {
568
+ ) throws -> Any ? {
569
569
// If the field type is non-nullable, then it is resolved without any
570
570
// protection from errors, however it still properly locates the error.
571
571
if let returnType = returnType as? GraphQLNonNull {
@@ -596,7 +596,7 @@ func completeValueCatchingError(
596
596
// If `completeValueWithLocatedError` returned abruptly (threw an error),
597
597
// log the error and return .null.
598
598
exeContext. errors. append ( error)
599
- return Map . null
599
+ return nil
600
600
} catch {
601
601
fatalError ( )
602
602
}
@@ -611,7 +611,7 @@ func completeValueWithLocatedError(
611
611
info: GraphQLResolveInfo ,
612
612
path: [ IndexPathElement ] ,
613
613
result: ResultOrError
614
- ) throws -> MapRepresentable {
614
+ ) throws -> Any ? {
615
615
do {
616
616
let completed = try completeValue (
617
617
exeContext: exeContext,
@@ -660,7 +660,7 @@ func completeValue(
660
660
info: GraphQLResolveInfo ,
661
661
path: [ IndexPathElement ] ,
662
662
result: ResultOrError
663
- ) throws -> MapRepresentable {
663
+ ) throws -> Any ? {
664
664
switch result {
665
665
case . error( let error) :
666
666
throw error
@@ -677,18 +677,18 @@ func completeValue(
677
677
result: . result( result)
678
678
)
679
679
680
- guard !isNullish ( completed) else {
680
+ guard let reallyCompleted = completed else {
681
681
throw GraphQLError (
682
682
message: " Cannot return null for non-nullable field \( info. parentType. name) . \( info. fieldName) . "
683
683
)
684
684
}
685
685
686
- return completed
686
+ return reallyCompleted
687
687
}
688
688
689
689
// If result value is null-ish (nil or .null) then return .null.
690
- if isNullish ( result) {
691
- return Map . null
690
+ guard let r = result else {
691
+ return nil
692
692
}
693
693
694
694
// If field type is List, complete each item in the list with the inner type
@@ -699,14 +699,14 @@ func completeValue(
699
699
fieldASTs: fieldASTs,
700
700
info: info,
701
701
path: path,
702
- result: result
702
+ result: r
703
703
)
704
704
}
705
705
706
706
// If field type is a leaf type, Scalar or Enum, serialize to a valid value,
707
707
// returning .null if serialization is not possible.
708
708
if let returnType = returnType as? GraphQLLeafType {
709
- return try completeLeafValue ( returnType: returnType, result: result )
709
+ return try completeLeafValue ( returnType: returnType, result: r )
710
710
}
711
711
712
712
// If field type is an abstract type, Interface or Union, determine the
@@ -718,7 +718,7 @@ func completeValue(
718
718
fieldASTs: fieldASTs,
719
719
info: info,
720
720
path: path,
721
- result: result
721
+ result: r
722
722
)
723
723
}
724
724
@@ -730,7 +730,7 @@ func completeValue(
730
730
fieldASTs: fieldASTs,
731
731
info: info,
732
732
path: path,
733
- result: result
733
+ result: r
734
734
)
735
735
}
736
736
@@ -751,9 +751,9 @@ func completeListValue(
751
751
fieldASTs: [ Field ] ,
752
752
info: GraphQLResolveInfo ,
753
753
path: [ IndexPathElement ] ,
754
- result: MapRepresentable
755
- ) throws -> MapRepresentable {
756
- guard let result = result as? [ MapRepresentable ] else {
754
+ result: Any
755
+ ) throws -> Any ? {
756
+ guard let result = result as? [ Any ? ] else {
757
757
throw GraphQLError (
758
758
message:
759
759
" Expected array, but did not find one for field " +
@@ -762,7 +762,7 @@ func completeListValue(
762
762
}
763
763
764
764
let itemType = returnType. ofType
765
- var completedResults : [ MapRepresentable ] = [ ]
765
+ var completedResults : [ Any ? ] = [ ]
766
766
767
767
for (index, item) in result. enumerated ( ) {
768
768
// No need to modify the info object containing the path,
@@ -788,10 +788,15 @@ func completeListValue(
788
788
* Complete a Scalar or Enum by serializing to a valid value, returning
789
789
* .null if serialization is not possible.
790
790
*/
791
- func completeLeafValue( returnType: GraphQLLeafType , result: MapRepresentable ) throws -> Map {
791
+ func completeLeafValue( returnType: GraphQLLeafType , result: Any ? ) throws -> Map {
792
+ // TODO: check this out
793
+ guard let result = result else {
794
+ return . null
795
+ }
796
+
792
797
let serializedResult = try returnType. serialize ( value: result)
793
798
794
- if isNullish ( serializedResult) {
799
+ if serializedResult == . null {
795
800
throw GraphQLError (
796
801
message:
797
802
" Expected a value of type \" \( returnType) \" but " +
@@ -812,11 +817,11 @@ func completeAbstractValue(
812
817
fieldASTs: [ Field ] ,
813
818
info: GraphQLResolveInfo ,
814
819
path: [ IndexPathElement ] ,
815
- result: MapRepresentable
816
- ) throws -> MapRepresentable {
820
+ result: Any
821
+ ) throws -> Any ? {
817
822
var resolveRes = try returnType. resolveType ? ( result, exeContext. contextValue, info) . typeResolveResult
818
823
819
- resolveRes = resolveRes ?? defaultResolveType (
824
+ resolveRes = try resolveRes ?? defaultResolveType (
820
825
value: result,
821
826
context: exeContext. contextValue,
822
827
info: info,
@@ -878,12 +883,12 @@ func completeObjectValue(
878
883
fieldASTs: [ Field ] ,
879
884
info: GraphQLResolveInfo ,
880
885
path: [ IndexPathElement ] ,
881
- result: MapRepresentable
882
- ) throws -> MapRepresentable {
886
+ result: Any
887
+ ) throws -> Any ? {
883
888
// If there is an isTypeOf predicate func, call it with the
884
889
// current result. If isTypeOf returns false, then raise an error rather
885
890
// than continuing execution.
886
- guard returnType. isTypeOf ? ( result, exeContext. contextValue, info) ?? true else {
891
+ guard try returnType. isTypeOf ? ( result, exeContext. contextValue, info) ?? true else {
887
892
throw GraphQLError (
888
893
message:
889
894
" Expected value of type \" \( returnType. name) \" but got: \( result) . " ,
@@ -922,14 +927,14 @@ func completeObjectValue(
922
927
* isTypeOf for the object being coerced, returning the first type that matches.
923
928
*/
924
929
func defaultResolveType(
925
- value: MapRepresentable ,
926
- context: MapRepresentable ,
930
+ value: Any ,
931
+ context: Any ,
927
932
info: GraphQLResolveInfo ,
928
933
abstractType: GraphQLAbstractType
929
- ) -> TypeResolveResult ? {
934
+ ) throws -> TypeResolveResult ? {
930
935
let possibleTypes = info. schema. getPossibleTypes ( abstractType: abstractType)
931
936
932
- guard let type = possibleTypes. find ( { $0. isTypeOf ? ( value, context, info) ?? false } ) else {
937
+ guard let type = try possibleTypes. find ( { try $0. isTypeOf ? ( value, context, info) ?? false } ) else {
933
938
return nil
934
939
}
935
940
@@ -941,13 +946,14 @@ func defaultResolveType(
941
946
* which takes the property of the source object of the same name as the field
942
947
* and returns it as the result.
943
948
*/
944
- func defaultResolve( source: MapRepresentable , args: Map , context: MapRepresentable , info: GraphQLResolveInfo ) -> MapRepresentable {
949
+ func defaultResolve( source: Any , args: Map , context: Any , info: GraphQLResolveInfo ) -> Any ? {
945
950
guard let source = unwrap ( source) else {
946
- return Map . null
951
+ return nil
947
952
}
948
953
949
- guard let anyValue = try ? get ( info. fieldName, from: source) , let value = anyValue as? MapRepresentable else {
950
- return Map . null
954
+ // TODO: check why Reflection fails
955
+ guard let value = try ? get ( info. fieldName, from: source) else {
956
+ return nil
951
957
}
952
958
953
959
return value
0 commit comments