Skip to content

Commit 8a7a580

Browse files
committed
complete introspection
1 parent 045bae9 commit 8a7a580

File tree

9 files changed

+257
-203
lines changed

9 files changed

+257
-203
lines changed

Sources/GraphQL/Execution/Execute.swift

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -817,8 +817,14 @@ func completeAbstractValue(
817817
path: [IndexPathElement],
818818
result: MapRepresentable
819819
) throws -> MapRepresentable {
820-
let resolveRes = try returnType.resolveType?(result, exeContext.contextValue, info) ??
821-
defaultResolveType(value: result, context: exeContext.contextValue, info: info, abstractType: returnType).map({ .type($0) })
820+
var resolveRes = try returnType.resolveType?(result, exeContext.contextValue, info).typeResolveResult
821+
822+
resolveRes = resolveRes ?? defaultResolveType(
823+
value: result,
824+
context: exeContext.contextValue,
825+
info: info,
826+
abstractType: returnType
827+
)
822828

823829
guard let resolveResult = resolveRes else {
824830
throw GraphQLError(
@@ -841,7 +847,7 @@ func completeAbstractValue(
841847
throw GraphQLError(
842848
message:
843849
"Abstract type \(returnType.name) must resolve to an Object type at " +
844-
"runtime for field \(info.parentType.name).\(info.fieldName) with " +
850+
"runtime for field \(info.parentType.name).\(info.fieldName) with " +
845851
"value \"\(resolveResult)\", received \"\(runtimeType)\".",
846852
nodes: fieldASTs
847853
)
@@ -880,7 +886,7 @@ func completeObjectValue(
880886
// If there is an isTypeOf predicate func, call it with the
881887
// current result. If isTypeOf returns false, then raise an error rather
882888
// than continuing execution.
883-
if returnType.isTypeOf?(result, exeContext.contextValue, info) ?? false {
889+
guard returnType.isTypeOf?(result, exeContext.contextValue, info) ?? true else {
884890
throw GraphQLError(
885891
message:
886892
"Expected value of type \"\(returnType.name)\" but got: \(result).",
@@ -923,24 +929,14 @@ func defaultResolveType(
923929
context: MapRepresentable,
924930
info: GraphQLResolveInfo,
925931
abstractType: GraphQLAbstractType
926-
) -> GraphQLObjectType? {
932+
) -> TypeResolveResult? {
927933
let possibleTypes = info.schema.getPossibleTypes(abstractType: abstractType)
928-
return possibleTypes.find({ $0.isTypeOf?(value, context, info) ?? false })
929-
}
930934

931-
func unwrap(_ value: MapRepresentable) -> MapRepresentable? {
932-
let mirror = Mirror(reflecting: value)
933-
934-
if mirror.displayStyle != .optional {
935-
return value
936-
}
937-
938-
if mirror.children.isEmpty {
935+
guard let type = possibleTypes.find({ $0.isTypeOf?(value, context, info) ?? false }) else {
939936
return nil
940937
}
941938

942-
let child = mirror.children.first!
943-
return child.value as? MapRepresentable
939+
return .type(type)
944940
}
945941

946942
/**
@@ -949,11 +945,10 @@ func unwrap(_ value: MapRepresentable) -> MapRepresentable? {
949945
* and returns it as the result.
950946
*/
951947
func defaultResolve(source: MapRepresentable, args: Map, context: MapRepresentable, info: GraphQLResolveInfo) -> MapRepresentable {
952-
print(type(of: source))
953948
guard let source = unwrap(source) else {
954949
return Map.null
955950
}
956-
print(type(of: source))
951+
957952
guard let anyValue = try? get(info.fieldName, from: source), let value = anyValue as? MapRepresentable else {
958953
return Map.null
959954
}

Sources/GraphQL/Reflection/Storage.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ func mutableStorageForInstance(_ instance: inout Any) -> UnsafeMutablePointer<UI
44

55
func storageForInstance(_ instance: inout Any) -> UnsafePointer<UInt8> {
66
return withUnsafePointer(to: &instance) { pointer in
7-
if instance is AnyClass {
7+
if type(of: instance) is AnyClass {
88
return UnsafePointer(bitPattern: UnsafeRawPointer(pointer).assumingMemoryBound(to: Int.self).pointee)!
99
} else if sizeofValue(instance) <= 3 * sizeof(Int.self) {
1010
return UnsafeRawPointer(pointer).assumingMemoryBound(to: UInt8.self)
@@ -20,7 +20,7 @@ func mutableStorageForInstance<T>(_ instance: inout T) -> UnsafeMutablePointer<U
2020

2121
func storageForInstance<T>(_ instance: inout T) -> UnsafePointer<UInt8> {
2222
return withUnsafePointer(to: &instance) { pointer in
23-
if instance is AnyClass {
23+
if type(of: instance) is AnyClass {
2424
return UnsafePointer(bitPattern: UnsafeRawPointer(pointer).assumingMemoryBound(to: Int.self).pointee)!
2525
} else {
2626
return UnsafeRawPointer(pointer).assumingMemoryBound(to: UInt8.self)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public func unwrap(_ value: MapRepresentable) -> MapRepresentable? {
2+
let mirror = Mirror(reflecting: value)
3+
4+
if mirror.displayStyle != .optional {
5+
return value
6+
}
7+
8+
if mirror.children.isEmpty {
9+
return nil
10+
}
11+
12+
let child = mirror.children.first!
13+
return child.value as? MapRepresentable
14+
}

0 commit comments

Comments
 (0)