Skip to content

Commit a47a2c2

Browse files
committed
Add back runtime.
1 parent b3d9dac commit a47a2c2

File tree

11 files changed

+82
-43
lines changed

11 files changed

+82
-43
lines changed

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ let package = Package(
88
],
99
dependencies: [
1010
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "1.14.1")),
11+
.package(url: "https://github.com/wickwirew/Runtime.git", .upToNextMinor(from: "2.1.0"))
1112
],
1213
targets: [
13-
.target(name: "GraphQL", dependencies: ["NIO"]),
14+
.target(name: "GraphQL", dependencies: ["NIO", "Runtime"]),
1415
.testTarget(name: "GraphQLTests", dependencies: ["GraphQL"]),
1516
]
1617
)

Sources/GraphQL/Execution/Execute.swift

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Dispatch
2+
import Runtime
23
import NIO
34

45
/**
@@ -997,8 +998,7 @@ func completeLeafValue(returnType: GraphQLLeafType, result: Any?) throws -> Map
997998
return .null
998999
}
9991000

1000-
let r = try map(from: result)
1001-
let serializedResult = try returnType.serialize(value: r)
1001+
let serializedResult = try returnType.serialize(value: result)
10021002

10031003
if serializedResult == .null {
10041004
throw GraphQLError(
@@ -1092,7 +1092,10 @@ func completeObjectValue(
10921092
// If there is an isTypeOf predicate func, call it with the
10931093
// current result. If isTypeOf returns false, then raise an error rather
10941094
// than continuing execution.
1095-
guard try returnType.isTypeOf?(result, exeContext.eventLoopGroup, info) ?? true else {
1095+
if
1096+
let isTypeOf = returnType.isTypeOf,
1097+
try !isTypeOf(result, exeContext.eventLoopGroup, info)
1098+
{
10961099
throw GraphQLError(
10971100
message:
10981101
"Expected value of type \"\(returnType.name)\" but got: \(result).",
@@ -1170,16 +1173,29 @@ func defaultResolve(
11701173
return eventLoopGroup.next().newSucceededFuture(result: nil)
11711174
}
11721175

1173-
guard let any = try? AnyEncoder().encode(AnyEncodable(encodable)) else {
1176+
guard
1177+
let typeInfo = try? typeInfo(of: type(of: encodable)),
1178+
let property = try? typeInfo.property(named: info.fieldName)
1179+
else {
11741180
return eventLoopGroup.next().newSucceededFuture(result: nil)
11751181
}
11761182

1177-
guard let dictionary = any as? [String: Any] else {
1183+
guard let value = try? property.get(from: encodable) else {
11781184
return eventLoopGroup.next().newSucceededFuture(result: nil)
11791185
}
11801186

1181-
let value = dictionary[info.fieldName]
11821187
return eventLoopGroup.next().newSucceededFuture(result: value)
1188+
1189+
// guard let any = try? AnyEncoder().encode(AnyEncodable(encodable)) else {
1190+
// return eventLoopGroup.next().newSucceededFuture(result: nil)
1191+
// }
1192+
//
1193+
// guard let dictionary = any as? [String: Any] else {
1194+
// return eventLoopGroup.next().newSucceededFuture(result: nil)
1195+
// }
1196+
//
1197+
// let value = dictionary[info.fieldName]
1198+
// return eventLoopGroup.next().newSucceededFuture(result: value)
11831199
}
11841200

11851201
/**

Sources/GraphQL/GraphQL.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public struct GraphQLResult : Equatable {
44
public var data: Map?
55
public var errors: [GraphQLError]
66

7-
init(data: Map? = nil, errors: [GraphQLError] = []) {
7+
public init(data: Map? = nil, errors: [GraphQLError] = []) {
88
self.data = data
99
self.errors = errors
1010
}
@@ -86,7 +86,7 @@ public func graphql(
8686
/// - throws: throws GraphQLError if an error occurs while parsing the `request`.
8787
///
8888
/// - returns: returns a `Map` dictionary containing the result of the query inside the key `data` and any validation or execution errors inside the key `errors`. The value of `data` might be `null` if, for example, the query is invalid. It's possible to have both `data` and `errors` if an error occurs only in a specific field. If that happens the value of that field will be `null` and there will be an error inside `errors` specifying the reason for the failure and the path of the failed field.
89-
public func graphql<Retrieval:PersistedQueryRetrieval>(
89+
public func graphql<Retrieval: PersistedQueryRetrieval>(
9090
queryStrategy: QueryFieldExecutionStrategy = SerialFieldExecutionStrategy(),
9191
mutationStrategy: MutationFieldExecutionStrategy = SerialFieldExecutionStrategy(),
9292
subscriptionStrategy: SubscriptionFieldExecutionStrategy = SerialFieldExecutionStrategy(),

Sources/GraphQL/Language/Location.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import Foundation
33
public struct SourceLocation : Encodable {
44
public let line: Int
55
public let column: Int
6+
7+
public init(line: Int, column: Int) {
8+
self.line = line
9+
self.column = column
10+
}
611
}
712

813
/**

Sources/GraphQL/Map/Map.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -625,12 +625,18 @@ extension Map : Equatable {}
625625

626626
public func == (lhs: Map, rhs: Map) -> Bool {
627627
switch (lhs, rhs) {
628-
case (.null, .null): return true
629-
case let (.number(l), .number(r)) where l == r: return true
630-
case let (.string(l), .string(r)) where l == r: return true
631-
case let (.array(l), .array(r)) where l == r: return true
632-
case let (.dictionary(l), .dictionary(r)) where l == r: return true
633-
default: return false
628+
case (.null, .null):
629+
return true
630+
case let (.number(l), .number(r)) where l == r:
631+
return true
632+
case let (.string(l), .string(r)) where l == r:
633+
return true
634+
case let (.array(l), .array(r)) where l == r:
635+
return true
636+
case let (.dictionary(l), .dictionary(r)) where l == r:
637+
return true
638+
default:
639+
return false
634640
}
635641
}
636642

Sources/GraphQL/Map/MapSerialization.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,17 @@ public struct MapSerialization {
3333
}
3434

3535
static func object(with map: Map) throws -> NSObject {
36-
throw DecodingError.dataCorrupted(
37-
DecodingError.Context(
38-
codingPath: [],
39-
debugDescription: "The given data was not valid Map."
40-
)
41-
)
36+
switch map {
37+
case .null:
38+
return NSNull()
39+
case var .number(number):
40+
return number.number
41+
case let .string(string):
42+
return string as NSString
43+
case let .array(array):
44+
return try array.map({ try object(with: $0) }) as NSArray
45+
case let .dictionary(dictionary):
46+
return try dictionary.mapValues({ try object(with: $0) }) as NSDictionary
47+
}
4248
}
4349
}

Sources/GraphQL/Map/Number.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ public struct Number {
157157
}
158158

159159
extension Number : Hashable {}
160-
extension Number : Equatable {}
160+
161+
extension Number : Equatable {
162+
public static func == (lhs: Self, rhs: Self) -> Bool {
163+
return lhs._number == rhs._number
164+
}
165+
}
161166

162167
extension Number : Comparable {
163168
public static func < (lhs: Number, rhs: Number) -> Bool {

Sources/GraphQL/PersistedQueries/PersistedQueries.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
public enum PersistedQueryRetrievalResult<T> {
32
case unknownId(T)
43
case parseError(GraphQLError)

Sources/GraphQL/Type/Definition.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ extension GraphQLNonNull : GraphQLOutputType {}
5050
* These types may describe types which may be leaf values.
5151
*/
5252
public protocol GraphQLLeafType : GraphQLNamedType {
53-
func serialize(value: Map) throws -> Map
53+
func serialize(value: Any) throws -> Map
5454
func parseValue(value: Map) throws -> Map
5555
func parseLiteral(valueAST: Value) throws -> Map
5656
}
@@ -165,14 +165,14 @@ public final class GraphQLScalarType {
165165
public let description: String?
166166
public let kind: TypeKind = .scalar
167167

168-
let serialize: (Map) throws -> Map
168+
let serialize: (Any) throws -> Map
169169
let parseValue: ((Map) throws -> Map)?
170170
let parseLiteral: ((Value) throws -> Map)?
171171

172172
public init(
173173
name: String,
174174
description: String? = nil,
175-
serialize: @escaping (Map) throws -> Map
175+
serialize: @escaping (Any) throws -> Map
176176
) throws {
177177
try assertValid(name: name)
178178
self.name = name
@@ -185,7 +185,7 @@ public final class GraphQLScalarType {
185185
public init(
186186
name: String,
187187
description: String? = nil,
188-
serialize: @escaping (Map) throws -> Map,
188+
serialize: @escaping (Any) throws -> Map,
189189
parseValue: @escaping (Map) throws -> Map,
190190
parseLiteral: @escaping (Value) throws -> Map
191191
) throws {
@@ -198,7 +198,7 @@ public final class GraphQLScalarType {
198198
}
199199

200200
// Serializes an internal value to include in a response.
201-
public func serialize(value: Map) throws -> Map {
201+
public func serialize(value: Any) throws -> Map {
202202
return try self.serialize(value)
203203
}
204204

@@ -534,31 +534,31 @@ public struct GraphQLField {
534534
description: String? = nil,
535535
deprecationReason: String? = nil,
536536
args: GraphQLArgumentConfigMap = [:],
537-
resolve: @escaping GraphQLFieldResolveInput
537+
resolve: GraphQLFieldResolve?
538538
) {
539539
self.type = type
540540
self.args = args
541541
self.deprecationReason = deprecationReason
542542
self.description = description
543-
544-
self.resolve = { source, args, context, eventLoopGroup, info in
545-
let result = try resolve(source, args, context, info)
546-
return eventLoopGroup.next().newSucceededFuture(result: result)
547-
}
543+
self.resolve = resolve
548544
}
549545

550546
public init(
551547
type: GraphQLOutputType,
552548
description: String? = nil,
553549
deprecationReason: String? = nil,
554550
args: GraphQLArgumentConfigMap = [:],
555-
resolve: @escaping GraphQLFieldResolve
551+
resolve: @escaping GraphQLFieldResolveInput
556552
) {
557553
self.type = type
558554
self.args = args
559555
self.deprecationReason = deprecationReason
560556
self.description = description
561-
self.resolve = resolve
557+
558+
self.resolve = { source, args, context, eventLoopGroup, info in
559+
let result = try resolve(source, args, context, info)
560+
return eventLoopGroup.next().newSucceededFuture(result: result)
561+
}
562562
}
563563
}
564564

@@ -991,8 +991,8 @@ public final class GraphQLEnumType {
991991
self.nameLookup = nameLookup
992992
}
993993

994-
public func serialize(value: Map) throws -> Map {
995-
return valueLookup[value].map({ .string($0.name) }) ?? .null
994+
public func serialize(value: Any) throws -> Map {
995+
return try valueLookup[map(from: value)].map({ .string($0.name) }) ?? .null
996996
}
997997

998998
public func parseValue(value: Map) throws -> Map {

Sources/GraphQL/Type/Scalars.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ public let GraphQLInt = try! GraphQLScalarType(
33
description:
44
"The `Int` scalar type represents non-fractional signed whole numeric " +
55
"values. Int can represent values between -(2^31) and 2^31 - 1.",
6-
serialize: { $0 } ,
6+
serialize: { try map(from: $0) } ,
77
parseValue: { try .int($0.intValue(converting: true)) },
88
parseLiteral: { ast in
99
if let ast = ast as? IntValue, let int = Int(ast.value) {
@@ -20,7 +20,7 @@ public let GraphQLFloat = try! GraphQLScalarType(
2020
"The `Float` scalar type represents signed double-precision fractional " +
2121
"values as specified by " +
2222
"[IEEE 754](http://en.wikipedia.org/wiki/IEEE_floating_point). ",
23-
serialize: { $0 } ,
23+
serialize: { try map(from: $0) } ,
2424
parseValue: { try .double($0.doubleValue(converting: true)) },
2525
parseLiteral: { ast in
2626
if let ast = ast as? FloatValue, let double = Double(ast.value) {
@@ -41,7 +41,7 @@ public let GraphQLString = try! GraphQLScalarType(
4141
"The `String` scalar type represents textual data, represented as UTF-8 " +
4242
"character sequences. The String type is most often used by GraphQL to " +
4343
"represent free-form human-readable text.",
44-
serialize: { $0 } ,
44+
serialize: { try map(from: $0) } ,
4545
parseValue: { try .string($0.stringValue(converting: true)) },
4646
parseLiteral: { ast in
4747
if let ast = ast as? StringValue {
@@ -55,7 +55,7 @@ public let GraphQLString = try! GraphQLScalarType(
5555
public let GraphQLBoolean = try! GraphQLScalarType(
5656
name: "Boolean",
5757
description: "The `Boolean` scalar type represents `true` or `false`.",
58-
serialize: { $0 } ,
58+
serialize: { try map(from: $0) } ,
5959
parseValue: { try .bool($0.boolValue(converting: true)) },
6060
parseLiteral: { ast in
6161
if let ast = ast as? BooleanValue {
@@ -74,7 +74,7 @@ public let GraphQLID = try! GraphQLScalarType(
7474
"response as a String; however, it is not intended to be human-readable. " +
7575
"When expected as an input type, any string (such as `\"4\"`) or integer " +
7676
"(such as `4`) input value will be accepted as an ID.",
77-
serialize: { $0 },
77+
serialize: { try map(from: $0) },
7878
parseValue: { try .string($0.stringValue(converting: true)) },
7979
parseLiteral: { ast in
8080
if let ast = ast as? StringValue {

0 commit comments

Comments
 (0)