Skip to content

Commit d5348ac

Browse files
committed
Swift 4.1 support. Switch to a more mature reflection dependency.
1 parent 09542e4 commit d5348ac

28 files changed

+63
-676
lines changed

Package.resolved

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1+
// swift-tools-version:4.0
12
import PackageDescription
23

34
let package = Package(
4-
name: "GraphQL"
5+
name: "GraphQL",
6+
7+
dependencies: [
8+
.package(url: "https://github.com/wickwirew/Runtime.git", .branch("swift-4.1")),
9+
],
10+
11+
targets: [
12+
.target(name: "GraphQL", dependencies: ["Runtime"]),
13+
14+
.testTarget(name: "GraphQLTests", dependencies: ["GraphQL"]),
15+
]
516
)

Sources/GraphQL/Execution/Execute.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Dispatch
2+
import Runtime
23

34
/**
45
* Terminology
@@ -1128,7 +1129,12 @@ func defaultResolve(source: Any, args: Map, context: Any, info: GraphQLResolveIn
11281129
}
11291130

11301131
// TODO: check why Reflection fails
1131-
guard let value = try? get(info.fieldName, from: s) else {
1132+
guard let typeInfo = try? typeInfo(of: type(of: s)),
1133+
let property = try? typeInfo.property(named: info.fieldName) else {
1134+
return nil
1135+
}
1136+
1137+
guard let value = try? property.get(from: s) else {
11321138
return nil
11331139
}
11341140

Sources/GraphQL/Map/MapInitializable.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1+
import Runtime
2+
3+
enum RuntimeReflectionError: Error {
4+
case requiredValueMissing(key: String)
5+
}
6+
17
extension MapInitializable {
28
public init(map: Map) throws {
39
guard case .dictionary(let dictionary) = map else {
410
throw MapError.cannotInitialize(type: Self.self, from: try type(of: map.get()))
511
}
6-
self = try construct { property in
12+
13+
self = try createInstance()
14+
let info = try typeInfo(of: Self.self)
15+
16+
for property in info.properties {
717
guard let initializable = property.type as? MapInitializable.Type else {
818
throw MapError.notMapInitializable(property.type)
919
}
10-
switch dictionary[property.key] ?? .null {
20+
switch dictionary[property.name] ?? .null {
1121
case .null:
1222
guard let expressibleByNilLiteral = property.type as? ExpressibleByNilLiteral.Type else {
13-
throw ReflectionError.requiredValueMissing(key: property.key)
23+
throw RuntimeReflectionError.requiredValueMissing(key: property.name)
1424
}
15-
return expressibleByNilLiteral.init(nilLiteral: ())
25+
try property.set(value: expressibleByNilLiteral.init(nilLiteral: ()), on: &self)
1626
case let x:
17-
return try initializable.init(map: x)
27+
try property.set(value: initializable.init(map: x), on: &self)
1828
}
1929
}
2030
}

Sources/GraphQL/Map/MapRepresentable.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Runtime
2+
13
public func map(from value: Any?) throws -> Map {
24
guard let value = value else {
35
return .null
@@ -10,13 +12,14 @@ public func map(from value: Any?) throws -> Map {
1012
if let mapFallibleRepresentable = value as? MapFallibleRepresentable {
1113
return try mapFallibleRepresentable.asMap()
1214
}
13-
14-
let props = try properties(value)
15+
16+
let info = try typeInfo(of: type(of: value))
17+
let props = info.properties
1518

1619
var dictionary = [String: Map](minimumCapacity: props.count)
1720

1821
for property in props {
19-
dictionary[property.key] = try map(from: property.value)
22+
dictionary[property.name] = try map(from: property.get(from: value))
2023
}
2124

2225
return .dictionary(dictionary)
@@ -31,20 +34,22 @@ public func assertMappable(_ type: Any.Type) throws {
3134
return
3235
}
3336

34-
for property in try properties(type) {
37+
let info = try typeInfo(of: type)
38+
for property in info.properties {
3539
try assertMappable(property.type)
3640
}
3741
}
3842

3943
extension MapFallibleRepresentable {
4044
public func asMap() throws -> Map {
41-
let props = try properties(self)
45+
let info = try typeInfo(of: type(of: self))
46+
let props = info.properties
4247
var dictionary = [String: Map](minimumCapacity: props.count)
4348
for property in props {
44-
guard let representable = property.value as? MapFallibleRepresentable else {
45-
throw MapError.notMapRepresentable(type(of: property.value))
49+
guard let representable = try property.get(from: self) as? MapFallibleRepresentable else {
50+
throw MapError.notMapRepresentable(property.type)
4651
}
47-
dictionary[property.key] = try representable.asMap()
52+
dictionary[property.name] = try representable.asMap()
4853
}
4954
return .dictionary(dictionary)
5055
}

Sources/GraphQL/Reflection/Advance.swift

Lines changed: 0 additions & 6 deletions
This file was deleted.

Sources/GraphQL/Reflection/Any+Extensions.swift

Lines changed: 0 additions & 50 deletions
This file was deleted.

Sources/GraphQL/Reflection/Array+Extensions.swift

Lines changed: 0 additions & 23 deletions
This file was deleted.

Sources/GraphQL/Reflection/Construct.swift

Lines changed: 0 additions & 64 deletions
This file was deleted.

Sources/GraphQL/Reflection/Get.swift

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)