From 1c4bd38e10fcbd3a352a5948ff424a2894ed5a63 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Thu, 13 May 2021 07:03:21 +0100 Subject: [PATCH] Replace Runtime package with Mirror --- .dockerignore | 2 ++ Package.resolved | 18 ------------- Package.swift | 2 -- Sources/GraphQL/Execution/Execute.swift | 27 ++------------------ Sources/GraphQL/Subscription/Subscribe.swift | 2 -- Sources/GraphQL/SwiftUtilities/Mirror.swift | 22 ++++++++++++++++ Sources/GraphQL/SwiftUtilities/Unwrap.swift | 14 ---------- 7 files changed, 26 insertions(+), 61 deletions(-) create mode 100644 .dockerignore create mode 100644 Sources/GraphQL/SwiftUtilities/Mirror.swift delete mode 100644 Sources/GraphQL/SwiftUtilities/Unwrap.swift diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..2fb33437 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +.build +.git \ No newline at end of file diff --git a/Package.resolved b/Package.resolved index 969c267a..f3e6804f 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,24 +1,6 @@ { "object": { "pins": [ - { - "package": "CRuntime", - "repositoryURL": "https://github.com/wickwirew/CRuntime.git", - "state": { - "branch": null, - "revision": "95f911318d8c885f6fc05e971471f94adfd39405", - "version": "2.1.2" - } - }, - { - "package": "Runtime", - "repositoryURL": "https://github.com/wickwirew/Runtime.git", - "state": { - "branch": null, - "revision": "61c9776f47d2951bdc562486ad348e5e1ca2e180", - "version": "2.1.1" - } - }, { "package": "swift-nio", "repositoryURL": "https://github.com/apple/swift-nio.git", diff --git a/Package.swift b/Package.swift index 311f7bf6..317bda88 100644 --- a/Package.swift +++ b/Package.swift @@ -8,14 +8,12 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.10.1")), - .package(url: "https://github.com/wickwirew/Runtime.git", .upToNextMinor(from: "2.1.0")) ], targets: [ .target( name: "GraphQL", dependencies: [ .product(name: "NIO", package: "swift-nio"), - .product(name: "Runtime", package: "Runtime") ] ), .testTarget(name: "GraphQLTests", dependencies: ["GraphQL"]), diff --git a/Sources/GraphQL/Execution/Execute.swift b/Sources/GraphQL/Execution/Execute.swift index 62acdb25..26105f79 100644 --- a/Sources/GraphQL/Execution/Execute.swift +++ b/Sources/GraphQL/Execution/Execute.swift @@ -1,5 +1,4 @@ import Dispatch -import Runtime import NIO /** @@ -1175,33 +1174,11 @@ func defaultResolve( return eventLoopGroup.next().makeSucceededFuture(value) } - guard let encodable = source as? Encodable else { + let mirror = Mirror(reflecting: source) + guard let value = mirror.getValue(named: info.fieldName) else { return eventLoopGroup.next().makeSucceededFuture(nil) } - - guard - let typeInfo = try? typeInfo(of: type(of: encodable)), - let property = try? typeInfo.property(named: info.fieldName) - else { - return eventLoopGroup.next().makeSucceededFuture(nil) - } - - guard let value = try? property.get(from: encodable) else { - return eventLoopGroup.next().makeSucceededFuture(nil) - } - return eventLoopGroup.next().makeSucceededFuture(value) - -// guard let any = try? AnyEncoder().encode(AnyEncodable(encodable)) else { -// return eventLoopGroup.next().newSucceededFuture(result: nil) -// } -// -// guard let dictionary = any as? [String: Any] else { -// return eventLoopGroup.next().newSucceededFuture(result: nil) -// } -// -// let value = dictionary[info.fieldName] -// return eventLoopGroup.next().newSucceededFuture(result: value) } /** diff --git a/Sources/GraphQL/Subscription/Subscribe.swift b/Sources/GraphQL/Subscription/Subscribe.swift index 0ada8d31..030736af 100644 --- a/Sources/GraphQL/Subscription/Subscribe.swift +++ b/Sources/GraphQL/Subscription/Subscribe.swift @@ -1,5 +1,3 @@ -import Dispatch -import Runtime import NIO /** diff --git a/Sources/GraphQL/SwiftUtilities/Mirror.swift b/Sources/GraphQL/SwiftUtilities/Mirror.swift new file mode 100644 index 00000000..ec65931b --- /dev/null +++ b/Sources/GraphQL/SwiftUtilities/Mirror.swift @@ -0,0 +1,22 @@ +func unwrap(_ value: Any) -> Any? { + let mirror = Mirror(reflecting: value) + + if mirror.displayStyle != .optional { + return value + } + + guard let child = mirror.children.first else { + return nil + } + + return child.value +} + +extension Mirror { + func getValue(named key: String) -> Any? { + guard let matched = children.filter({ $0.label == key }).first else { + return nil + } + return unwrap(matched.value) + } +} diff --git a/Sources/GraphQL/SwiftUtilities/Unwrap.swift b/Sources/GraphQL/SwiftUtilities/Unwrap.swift deleted file mode 100644 index 9786756b..00000000 --- a/Sources/GraphQL/SwiftUtilities/Unwrap.swift +++ /dev/null @@ -1,14 +0,0 @@ -func unwrap(_ value: Any) -> Any? { - let mirror = Mirror(reflecting: value) - - if mirror.displayStyle != .optional { - return value - } - - if mirror.children.isEmpty { - return nil - } - - let child = mirror.children.first! - return child.value -}