Skip to content

Commit 1d4290c

Browse files
committed
use Any instead of MapRepresentable
1 parent 097888c commit 1d4290c

22 files changed

+323
-304
lines changed

.swift-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.0
1+
3.0.1

Sources/GraphQL/Execution/Execute.swift

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@
2727
final class ExecutionContext {
2828
let schema: GraphQLSchema
2929
let fragments: [String: FragmentDefinition]
30-
let rootValue: MapRepresentable
31-
let contextValue: MapRepresentable
30+
let rootValue: Any
31+
let contextValue: Any
3232
let operation: OperationDefinition
3333
let variableValues: [String: Map]
3434
var errors: [GraphQLError]
3535

3636
init(
3737
schema: GraphQLSchema,
3838
fragments: [String: FragmentDefinition],
39-
rootValue: MapRepresentable,
40-
contextValue: MapRepresentable,
39+
rootValue: Any,
40+
contextValue: Any,
4141
operation: OperationDefinition,
4242
variableValues: [String: Map],
4343
errors: [GraphQLError]
@@ -62,8 +62,8 @@ final class ExecutionContext {
6262
func execute(
6363
schema: GraphQLSchema,
6464
documentAST: Document,
65-
rootValue: MapRepresentable,
66-
contextValue: MapRepresentable,
65+
rootValue: Any,
66+
contextValue: Any,
6767
variableValues: [String: Map] = [:],
6868
operationName: String? = nil
6969
) throws -> Map {
@@ -88,7 +88,7 @@ func execute(
8888
var dataMap: Map = [:]
8989

9090
for (key, value) in data {
91-
dataMap[key] = value.map
91+
dataMap[key] = try map(from: value)
9292
}
9393

9494
var result: [String: Map] = ["data": dataMap]
@@ -112,8 +112,8 @@ func execute(
112112
func buildExecutionContext(
113113
schema: GraphQLSchema,
114114
documentAST: Document,
115-
rootValue: MapRepresentable,
116-
contextValue: MapRepresentable,
115+
rootValue: Any,
116+
contextValue: Any,
117117
rawVariableValues: [String: Map],
118118
operationName: String?
119119
) throws -> ExecutionContext {
@@ -176,8 +176,8 @@ func buildExecutionContext(
176176
func executeOperation(
177177
exeContext: ExecutionContext,
178178
operation: OperationDefinition,
179-
rootValue: MapRepresentable
180-
) throws -> [String : MapRepresentable] {
179+
rootValue: Any
180+
) throws -> [String : Any] {
181181
let type = try getOperationRootType(schema: exeContext.schema, operation: operation)
182182

183183
var inputFields: [String : [Field]] = [:]
@@ -250,10 +250,10 @@ func getOperationRootType(
250250
func executeFieldsSerially(
251251
exeContext: ExecutionContext,
252252
parentType: GraphQLObjectType,
253-
sourceValue: MapRepresentable,
253+
sourceValue: Any,
254254
path: [IndexPathElement],
255255
fields: [String: [Field]]
256-
) throws -> [String: MapRepresentable] {
256+
) throws -> [String: Any] {
257257
return try fields.reduce([:]) { results, field in
258258
var results = results
259259
let fieldASTs = field.value
@@ -267,7 +267,7 @@ func executeFieldsSerially(
267267
path: fieldPath
268268
)
269269

270-
results[field.key] = result
270+
results[field.key] = result ?? Map.null
271271
return results
272272
}
273273
}
@@ -279,10 +279,10 @@ func executeFieldsSerially(
279279
func executeFields(
280280
exeContext: ExecutionContext,
281281
parentType: GraphQLObjectType,
282-
sourceValue: MapRepresentable,
282+
sourceValue: Any,
283283
path: [IndexPathElement],
284284
fields: [String: [Field]]
285-
) throws -> [String : MapRepresentable] {
285+
) throws -> [String : Any] {
286286
return try executeFieldsSerially(
287287
exeContext: exeContext,
288288
parentType: parentType,
@@ -470,10 +470,10 @@ func getFieldEntryKey(node: Field) -> String {
470470
func resolveField(
471471
exeContext: ExecutionContext,
472472
parentType: GraphQLObjectType,
473-
source: MapRepresentable,
473+
source: Any,
474474
fieldASTs: [Field],
475475
path: [IndexPathElement]
476-
) throws -> MapRepresentable {
476+
) throws -> Any? {
477477
let fieldAST = fieldASTs[0]
478478
let fieldName = fieldAST.name.value
479479

@@ -536,17 +536,17 @@ func resolveField(
536536
}
537537

538538
enum ResultOrError {
539-
case result(MapRepresentable)
539+
case result(Any?)
540540
case error(Error)
541541
}
542542

543543
// Isolates the "ReturnOrAbrupt" behavior to not de-opt the `resolveField`
544544
// function. Returns the result of `resolve` or the abrupt-return Error object.
545545
func resolveOrError(
546546
resolve: GraphQLFieldResolve,
547-
source: MapRepresentable,
547+
source: Any,
548548
args: Map,
549-
context: MapRepresentable,
549+
context: Any,
550550
info: GraphQLResolveInfo
551551
)-> ResultOrError {
552552
do {
@@ -565,7 +565,7 @@ func completeValueCatchingError(
565565
info: GraphQLResolveInfo,
566566
path: [IndexPathElement],
567567
result: ResultOrError
568-
) throws -> MapRepresentable {
568+
) throws -> Any? {
569569
// If the field type is non-nullable, then it is resolved without any
570570
// protection from errors, however it still properly locates the error.
571571
if let returnType = returnType as? GraphQLNonNull {
@@ -596,7 +596,7 @@ func completeValueCatchingError(
596596
// If `completeValueWithLocatedError` returned abruptly (threw an error),
597597
// log the error and return .null.
598598
exeContext.errors.append(error)
599-
return Map.null
599+
return nil
600600
} catch {
601601
fatalError()
602602
}
@@ -611,7 +611,7 @@ func completeValueWithLocatedError(
611611
info: GraphQLResolveInfo,
612612
path: [IndexPathElement],
613613
result: ResultOrError
614-
) throws -> MapRepresentable {
614+
) throws -> Any? {
615615
do {
616616
let completed = try completeValue(
617617
exeContext: exeContext,
@@ -660,7 +660,7 @@ func completeValue(
660660
info: GraphQLResolveInfo,
661661
path: [IndexPathElement],
662662
result: ResultOrError
663-
) throws -> MapRepresentable {
663+
) throws -> Any? {
664664
switch result {
665665
case .error(let error):
666666
throw error
@@ -677,18 +677,18 @@ func completeValue(
677677
result: .result(result)
678678
)
679679

680-
guard !isNullish(completed) else {
680+
guard let reallyCompleted = completed else {
681681
throw GraphQLError(
682682
message: "Cannot return null for non-nullable field \(info.parentType.name).\(info.fieldName)."
683683
)
684684
}
685685

686-
return completed
686+
return reallyCompleted
687687
}
688688

689689
// 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
692692
}
693693

694694
// If field type is List, complete each item in the list with the inner type
@@ -699,14 +699,14 @@ func completeValue(
699699
fieldASTs: fieldASTs,
700700
info: info,
701701
path: path,
702-
result: result
702+
result: r
703703
)
704704
}
705705

706706
// If field type is a leaf type, Scalar or Enum, serialize to a valid value,
707707
// returning .null if serialization is not possible.
708708
if let returnType = returnType as? GraphQLLeafType {
709-
return try completeLeafValue(returnType: returnType, result: result)
709+
return try completeLeafValue(returnType: returnType, result: r)
710710
}
711711

712712
// If field type is an abstract type, Interface or Union, determine the
@@ -718,7 +718,7 @@ func completeValue(
718718
fieldASTs: fieldASTs,
719719
info: info,
720720
path: path,
721-
result: result
721+
result: r
722722
)
723723
}
724724

@@ -730,7 +730,7 @@ func completeValue(
730730
fieldASTs: fieldASTs,
731731
info: info,
732732
path: path,
733-
result: result
733+
result: r
734734
)
735735
}
736736

@@ -751,9 +751,9 @@ func completeListValue(
751751
fieldASTs: [Field],
752752
info: GraphQLResolveInfo,
753753
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 {
757757
throw GraphQLError(
758758
message:
759759
"Expected array, but did not find one for field " +
@@ -762,7 +762,7 @@ func completeListValue(
762762
}
763763

764764
let itemType = returnType.ofType
765-
var completedResults: [MapRepresentable] = []
765+
var completedResults: [Any?] = []
766766

767767
for (index, item) in result.enumerated() {
768768
// No need to modify the info object containing the path,
@@ -788,10 +788,15 @@ func completeListValue(
788788
* Complete a Scalar or Enum by serializing to a valid value, returning
789789
* .null if serialization is not possible.
790790
*/
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+
792797
let serializedResult = try returnType.serialize(value: result)
793798

794-
if isNullish(serializedResult) {
799+
if serializedResult == .null {
795800
throw GraphQLError(
796801
message:
797802
"Expected a value of type \"\(returnType)\" but " +
@@ -812,11 +817,11 @@ func completeAbstractValue(
812817
fieldASTs: [Field],
813818
info: GraphQLResolveInfo,
814819
path: [IndexPathElement],
815-
result: MapRepresentable
816-
) throws -> MapRepresentable {
820+
result: Any
821+
) throws -> Any? {
817822
var resolveRes = try returnType.resolveType?(result, exeContext.contextValue, info).typeResolveResult
818823

819-
resolveRes = resolveRes ?? defaultResolveType(
824+
resolveRes = try resolveRes ?? defaultResolveType(
820825
value: result,
821826
context: exeContext.contextValue,
822827
info: info,
@@ -878,12 +883,12 @@ func completeObjectValue(
878883
fieldASTs: [Field],
879884
info: GraphQLResolveInfo,
880885
path: [IndexPathElement],
881-
result: MapRepresentable
882-
) throws -> MapRepresentable {
886+
result: Any
887+
) throws -> Any? {
883888
// If there is an isTypeOf predicate func, call it with the
884889
// current result. If isTypeOf returns false, then raise an error rather
885890
// than continuing execution.
886-
guard returnType.isTypeOf?(result, exeContext.contextValue, info) ?? true else {
891+
guard try returnType.isTypeOf?(result, exeContext.contextValue, info) ?? true else {
887892
throw GraphQLError(
888893
message:
889894
"Expected value of type \"\(returnType.name)\" but got: \(result).",
@@ -922,14 +927,14 @@ func completeObjectValue(
922927
* isTypeOf for the object being coerced, returning the first type that matches.
923928
*/
924929
func defaultResolveType(
925-
value: MapRepresentable,
926-
context: MapRepresentable,
930+
value: Any,
931+
context: Any,
927932
info: GraphQLResolveInfo,
928933
abstractType: GraphQLAbstractType
929-
) -> TypeResolveResult? {
934+
) throws -> TypeResolveResult? {
930935
let possibleTypes = info.schema.getPossibleTypes(abstractType: abstractType)
931936

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 {
933938
return nil
934939
}
935940

@@ -941,13 +946,14 @@ func defaultResolveType(
941946
* which takes the property of the source object of the same name as the field
942947
* and returns it as the result.
943948
*/
944-
func defaultResolve(source: MapRepresentable, args: Map, context: MapRepresentable, info: GraphQLResolveInfo) -> MapRepresentable {
949+
func defaultResolve(source: Any, args: Map, context: Any, info: GraphQLResolveInfo) -> Any? {
945950
guard let source = unwrap(source) else {
946-
return Map.null
951+
return nil
947952
}
948953

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
951957
}
952958

953959
return value

0 commit comments

Comments
 (0)