Skip to content

Commit 8dab0dc

Browse files
committed
improve type safety
1 parent 3f63dd0 commit 8dab0dc

File tree

3 files changed

+82
-82
lines changed

3 files changed

+82
-82
lines changed

Sources/GraphQL/Language/AST.swift

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,10 @@ extension InputObjectTypeDefinition : Node {}
208208
extension TypeExtensionDefinition : Node {}
209209
extension DirectiveDefinition : Node {}
210210

211-
final class Name {
212-
let kind: Kind = .name
213-
let loc: Location?
214-
let value: String
211+
public final class Name {
212+
public let kind: Kind = .name
213+
public let loc: Location?
214+
public let value: String
215215

216216
init(loc: Location? = nil, value: String) {
217217
self.loc = loc
@@ -220,7 +220,7 @@ final class Name {
220220
}
221221

222222
extension Name : Equatable {
223-
static func == (lhs: Name, rhs: Name) -> Bool {
223+
public static func == (lhs: Name, rhs: Name) -> Bool {
224224
return lhs.value == rhs.value
225225
}
226226
}
@@ -772,10 +772,10 @@ public func == (lhs: Value, rhs: Value) -> Bool {
772772
return false
773773
}
774774

775-
final class IntValue {
776-
let kind: Kind = .intValue
777-
let loc: Location?
778-
let value: String
775+
public final class IntValue {
776+
public let kind: Kind = .intValue
777+
public let loc: Location?
778+
public let value: String
779779

780780
init(loc: Location? = nil, value: String) {
781781
self.loc = loc
@@ -784,15 +784,15 @@ final class IntValue {
784784
}
785785

786786
extension IntValue : Equatable {
787-
static func == (lhs: IntValue, rhs: IntValue) -> Bool {
787+
public static func == (lhs: IntValue, rhs: IntValue) -> Bool {
788788
return lhs.value == rhs.value
789789
}
790790
}
791791

792-
final class FloatValue {
793-
let kind: Kind = .floatValue
794-
let loc: Location?
795-
let value: String
792+
public final class FloatValue {
793+
public let kind: Kind = .floatValue
794+
public let loc: Location?
795+
public let value: String
796796

797797
init(loc: Location? = nil, value: String) {
798798
self.loc = loc
@@ -801,15 +801,15 @@ final class FloatValue {
801801
}
802802

803803
extension FloatValue : Equatable {
804-
static func == (lhs: FloatValue, rhs: FloatValue) -> Bool {
804+
public static func == (lhs: FloatValue, rhs: FloatValue) -> Bool {
805805
return lhs.value == rhs.value
806806
}
807807
}
808808

809-
final class StringValue {
810-
let kind: Kind = .stringValue
811-
let loc: Location?
812-
let value: String
809+
public final class StringValue {
810+
public let kind: Kind = .stringValue
811+
public let loc: Location?
812+
public let value: String
813813

814814
init(loc: Location? = nil, value: String) {
815815
self.loc = loc
@@ -818,15 +818,15 @@ final class StringValue {
818818
}
819819

820820
extension StringValue : Equatable {
821-
static func == (lhs: StringValue, rhs: StringValue) -> Bool {
821+
public static func == (lhs: StringValue, rhs: StringValue) -> Bool {
822822
return lhs.value == rhs.value
823823
}
824824
}
825825

826-
final class BooleanValue {
827-
let kind: Kind = .booleanValue
828-
let loc: Location?
829-
let value: Bool
826+
public final class BooleanValue {
827+
public let kind: Kind = .booleanValue
828+
public let loc: Location?
829+
public let value: Bool
830830

831831
init(loc: Location? = nil, value: Bool) {
832832
self.loc = loc
@@ -835,15 +835,15 @@ final class BooleanValue {
835835
}
836836

837837
extension BooleanValue : Equatable {
838-
static func == (lhs: BooleanValue, rhs: BooleanValue) -> Bool {
838+
public static func == (lhs: BooleanValue, rhs: BooleanValue) -> Bool {
839839
return lhs.value == rhs.value
840840
}
841841
}
842842

843-
final class EnumValue {
844-
let kind: Kind = .enumValue
845-
let loc: Location?
846-
let value: String
843+
public final class EnumValue {
844+
public let kind: Kind = .enumValue
845+
public let loc: Location?
846+
public let value: String
847847

848848
init(loc: Location? = nil, value: String) {
849849
self.loc = loc
@@ -852,15 +852,15 @@ final class EnumValue {
852852
}
853853

854854
extension EnumValue : Equatable {
855-
static func == (lhs: EnumValue, rhs: EnumValue) -> Bool {
855+
public static func == (lhs: EnumValue, rhs: EnumValue) -> Bool {
856856
return lhs.value == rhs.value
857857
}
858858
}
859859

860-
final class ListValue {
861-
let kind: Kind = .listValue
862-
let loc: Location?
863-
let values: [Value]
860+
public final class ListValue {
861+
public let kind: Kind = .listValue
862+
public let loc: Location?
863+
public let values: [Value]
864864

865865
init(loc: Location? = nil, values: [Value]) {
866866
self.loc = loc
@@ -869,7 +869,7 @@ final class ListValue {
869869
}
870870

871871
extension ListValue : Equatable {
872-
static func == (lhs: ListValue, rhs: ListValue) -> Bool {
872+
public static func == (lhs: ListValue, rhs: ListValue) -> Bool {
873873
guard lhs.values.count == rhs.values.count else {
874874
return false
875875
}
@@ -884,10 +884,10 @@ extension ListValue : Equatable {
884884
}
885885
}
886886

887-
final class ObjectValue {
888-
let kind: Kind = .objectValue
889-
let loc: Location?
890-
let fields: [ObjectField]
887+
public final class ObjectValue {
888+
public let kind: Kind = .objectValue
889+
public let loc: Location?
890+
public let fields: [ObjectField]
891891

892892
init(loc: Location? = nil, fields: [ObjectField]) {
893893
self.loc = loc
@@ -896,16 +896,16 @@ final class ObjectValue {
896896
}
897897

898898
extension ObjectValue : Equatable {
899-
static func == (lhs: ObjectValue, rhs: ObjectValue) -> Bool {
899+
public static func == (lhs: ObjectValue, rhs: ObjectValue) -> Bool {
900900
return lhs.fields == rhs.fields
901901
}
902902
}
903903

904-
final class ObjectField {
905-
let kind: Kind = .objectField
906-
let loc: Location?
907-
let name: Name
908-
let value: Value
904+
public final class ObjectField {
905+
public let kind: Kind = .objectField
906+
public let loc: Location?
907+
public let name: Name
908+
public let value: Value
909909

910910
init(loc: Location? = nil, name: Name, value: Value) {
911911
self.loc = loc
@@ -915,7 +915,7 @@ final class ObjectField {
915915
}
916916

917917
extension ObjectField : Equatable {
918-
static func == (lhs: ObjectField, rhs: ObjectField) -> Bool {
918+
public static func == (lhs: ObjectField, rhs: ObjectField) -> Bool {
919919
return lhs.name == rhs.name &&
920920
lhs.value == rhs.value
921921
}

Sources/GraphQL/Type/Definition.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ extension GraphQLNonNull : GraphQLOutputType {}
4949
*/
5050
public protocol GraphQLLeafType : GraphQLType, GraphQLNamedType {
5151
func serialize(value: Any) throws -> Map
52-
func parseValue(value: Any) throws -> Map
52+
func parseValue(value: Map) throws -> Map
5353
func parseLiteral(valueAST: Value) throws -> Map
5454
}
5555

@@ -161,14 +161,14 @@ extension GraphQLNonNull : GraphQLWrapperType {}
161161
public final class GraphQLScalarType {
162162
public let name: String
163163
let description: String?
164-
let serialize: (Any) throws -> Any
165-
let parseValue: ((Any) throws -> Any)?
166-
let parseLiteral: ((Value) throws -> Any)?
164+
let serialize: (Any) throws -> Map
165+
let parseValue: ((Map) throws -> Map)?
166+
let parseLiteral: ((Value) throws -> Map)?
167167

168168
public init(
169169
name: String,
170170
description: String? = nil,
171-
serialize: @escaping (Any) throws -> Any
171+
serialize: @escaping (Any) throws -> Map
172172
) throws {
173173
try assertValid(name: name)
174174
self.name = name
@@ -181,9 +181,9 @@ public final class GraphQLScalarType {
181181
public init(
182182
name: String,
183183
description: String? = nil,
184-
serialize: @escaping (Any) throws -> Any,
185-
parseValue: @escaping (Any) throws -> Any,
186-
parseLiteral: @escaping (Value) throws -> Any
184+
serialize: @escaping (Any) throws -> Map,
185+
parseValue: @escaping (Map) throws -> Map,
186+
parseLiteral: @escaping (Value) throws -> Map
187187
) throws {
188188
try assertValid(name: name)
189189
self.name = name
@@ -195,17 +195,17 @@ public final class GraphQLScalarType {
195195

196196
// Serializes an internal value to include in a response.
197197
public func serialize(value: Any) throws -> Map {
198-
return try GraphQL.map(from: self.serialize(value))
198+
return try self.serialize(value)
199199
}
200200

201201
// Parses an externally provided value to use as an input.
202-
public func parseValue(value: Any) throws -> Map {
203-
return try GraphQL.map(from: self.parseValue?(value))
202+
public func parseValue(value: Map) throws -> Map {
203+
return try self.parseValue?(value) ?? Map.null
204204
}
205205

206206
// Parses an externally provided literal value to use as an input.
207207
public func parseLiteral(valueAST: Value) throws -> Map {
208-
return try GraphQL.map(from: self.parseLiteral?(valueAST))
208+
return try self.parseLiteral?(valueAST) ?? Map.null
209209
}
210210
}
211211

@@ -837,8 +837,8 @@ public final class GraphQLEnumType {
837837
return try valueLookup[GraphQL.map(from: value)].map({ .string($0.name) }) ?? .null
838838
}
839839

840-
public func parseValue(value: Any) throws -> Map {
841-
if case .string(let value) = try GraphQL.map(from: value) {
840+
public func parseValue(value: Map) throws -> Map {
841+
if case .string(let value) = value {
842842
return nameLookup[value]?.value ?? .null
843843
}
844844

Sources/GraphQL/Type/Scalars.swift

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ 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: { try map(from: $0).asInt(converting: true) } ,
7-
parseValue: { try map(from: $0).asInt(converting: true) },
6+
serialize: { try map(from: $0) } ,
7+
parseValue: { try $0.asInt(converting: true).map },
88
parseLiteral: { ast in
99
if let ast = ast as? IntValue, let int = Int(ast.value) {
10-
return int
10+
return .int(int)
1111
}
1212

13-
return Map.null
13+
return .null
1414
}
1515
)
1616

@@ -20,18 +20,18 @@ 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: { try map(from: $0).asDouble(converting: true) } ,
24-
parseValue: { try map(from: $0).asDouble(converting: true) },
23+
serialize: { try map(from: $0) } ,
24+
parseValue: { try $0.asDouble(converting: true).map },
2525
parseLiteral: { ast in
2626
if let ast = ast as? FloatValue, let double = Double(ast.value) {
27-
return double
27+
return .double(double)
2828
}
2929

3030
if let ast = ast as? IntValue, let double = Double(ast.value) {
31-
return double
31+
return .double(double)
3232
}
3333

34-
return Map.null
34+
return .null
3535
}
3636
)
3737

@@ -41,28 +41,28 @@ 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: { try map(from: $0).asString(converting: true) } ,
45-
parseValue: { try map(from: $0).asString(converting: true) },
44+
serialize: { try map(from: $0) } ,
45+
parseValue: { try $0.asString(converting: true).map },
4646
parseLiteral: { ast in
4747
if let ast = ast as? StringValue {
48-
return ast.value
48+
return .string(ast.value)
4949
}
5050

51-
return Map.null
51+
return .null
5252
}
5353
)
5454

5555
public let GraphQLBoolean = try! GraphQLScalarType(
5656
name: "Boolean",
5757
description: "The `Boolean` scalar type represents `true` or `false`.",
58-
serialize: { try map(from: $0).asBool(converting: true) } ,
59-
parseValue: { try map(from: $0).asBool(converting: true) },
58+
serialize: { try map(from: $0) } ,
59+
parseValue: { try $0.asBool(converting: true).map },
6060
parseLiteral: { ast in
6161
if let ast = ast as? BooleanValue {
62-
return ast.value
62+
return .bool(ast.value)
6363
}
6464

65-
return Map.null
65+
return .null
6666
}
6767
)
6868

@@ -74,17 +74,17 @@ 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: { try map(from: $0).asString(converting: true) },
78-
parseValue: { try map(from: $0).asString(converting: true) },
77+
serialize: { try map(from: $0) },
78+
parseValue: { try $0.asString(converting: true).map },
7979
parseLiteral: { ast in
8080
if let ast = ast as? StringValue {
81-
return ast.value
81+
return .string(ast.value)
8282
}
8383

8484
if let ast = ast as? IntValue {
85-
return ast.value
85+
return .string(ast.value)
8686
}
8787

88-
return Map.null
88+
return .null
8989
}
9090
)

0 commit comments

Comments
 (0)