Skip to content

Commit 51bae92

Browse files
Removing force-casts and cleaning up funcs
1 parent 1740c8a commit 51bae92

File tree

2 files changed

+64
-63
lines changed

2 files changed

+64
-63
lines changed

Sources/GraphQL/Execution/Values.swift

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import OrderedCollections
23

34
/**
45
* Prepares an object map of variableValues of the correct type based on the
@@ -96,40 +97,40 @@ func getVariableValue(schema: GraphQLSchema, definitionAST: VariableDefinition,
9697
)
9798
}
9899

99-
return try coerceValue(type: inputType, value: input)
100+
return try coerceValue(value: input, type: inputType)
100101
}
101102

102103
/**
103104
* Given a type and any value, return a runtime value coerced to match the type.
104105
*/
105-
func coerceValue(type: GraphQLInputType, value: Map) throws -> Map {
106+
func coerceValue(value: Map, type: GraphQLInputType) throws -> Map {
106107
if let nonNull = type as? GraphQLNonNull {
107108
// Note: we're not checking that the result of coerceValue is non-null.
108109
// We only call this function after calling isValidValue.
109-
return try coerceValue(type: nonNull.ofType as! GraphQLInputType, value: value)
110+
guard let nonNullType = nonNull.ofType as? GraphQLInputType else {
111+
throw GraphQLError(message: "NonNull must wrap an input type")
112+
}
113+
return try coerceValue(value: value, type: nonNullType)
110114
}
111115

112-
guard value != .undefined else {
113-
return .undefined
114-
}
115-
guard value != .null else {
116-
return .null
116+
guard value != .undefined && value != .null else {
117+
return value
117118
}
118119

119120
if let list = type as? GraphQLList {
120-
let itemType = list.ofType
121+
guard let itemType = list.ofType as? GraphQLInputType else {
122+
throw GraphQLError(message: "Input list must wrap an input type")
123+
}
121124

122125
if case .array(let value) = value {
123-
var coercedValues: [Map] = []
124-
125-
for item in value {
126-
coercedValues.append(try coerceValue(type: itemType as! GraphQLInputType, value: item))
126+
let coercedValues = try value.map { item in
127+
try coerceValue(value: item, type: itemType)
127128
}
128-
129129
return .array(coercedValues)
130130
}
131-
132-
return .array([try coerceValue(type: itemType as! GraphQLInputType, value: value)])
131+
132+
// Convert solitary value into single-value array
133+
return .array([try coerceValue(value: value, type: itemType)])
133134
}
134135

135136
if let objectType = type as? GraphQLInputObjectType {
@@ -138,38 +139,29 @@ func coerceValue(type: GraphQLInputType, value: Map) throws -> Map {
138139
}
139140

140141
let fields = objectType.fields
141-
142-
return try .dictionary(fields.keys.reduce([:]) { obj, fieldName in
143-
var obj = obj
144-
let field = fields[fieldName]!
145-
if let fieldValueMap = value[fieldName] {
146-
let fieldValue = try coerceValue(
147-
type: field.type,
148-
value: fieldValueMap
142+
143+
var object = OrderedDictionary<String, Map>()
144+
for (fieldName, field) in fields {
145+
if let fieldValueMap = value[fieldName], fieldValueMap != .undefined {
146+
object[fieldName] = try coerceValue(
147+
value: fieldValueMap,
148+
type: field.type
149149
)
150-
obj[fieldName] = fieldValue
151150
} else {
152151
// If AST doesn't contain field, it is undefined
153152
if let defaultValue = field.defaultValue {
154-
obj[fieldName] = defaultValue
153+
object[fieldName] = defaultValue
155154
} else {
156-
obj[fieldName] = .undefined
155+
object[fieldName] = .undefined
157156
}
158157
}
159-
160-
return obj
161-
})
158+
}
159+
return .dictionary(object)
162160
}
163161

164-
guard let type = type as? GraphQLLeafType else {
165-
throw GraphQLError(message: "Must be input type")
162+
if let leafType = type as? GraphQLLeafType {
163+
return try leafType.parseValue(value: value)
166164
}
167165

168-
let parsed = try type.parseValue(value: value)
169-
170-
guard parsed != .null else {
171-
return nil
172-
}
173-
174-
return parsed
166+
throw GraphQLError(message: "Must be input type")
175167
}

Sources/GraphQL/Utilities/ValueFromAST.swift

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ import OrderedCollections
1818
*
1919
*/
2020
func valueFromAST(valueAST: Value, type: GraphQLInputType, variables: [String: Map] = [:]) throws -> Map {
21-
if let nonNullType = type as? GraphQLNonNull {
21+
if let nonNull = type as? GraphQLNonNull {
2222
// Note: we're not checking that the result of valueFromAST is non-null.
2323
// We're assuming that this query has been validated and the value used
2424
// here is of the correct type.
25-
return try valueFromAST(valueAST: valueAST, type: nonNullType.ofType as! GraphQLInputType, variables: variables)
25+
guard let nonNullType = nonNull.ofType as? GraphQLInputType else {
26+
throw GraphQLError(message: "NonNull must wrap an input type")
27+
}
28+
return try valueFromAST(valueAST: valueAST, type: nonNullType, variables: variables)
2629
}
2730

2831
if let variable = valueAST as? Variable {
@@ -42,19 +45,29 @@ func valueFromAST(valueAST: Value, type: GraphQLInputType, variables: [String: M
4245
}
4346

4447
if let list = type as? GraphQLList {
45-
let itemType = list.ofType
48+
guard let itemType = list.ofType as? GraphQLInputType else {
49+
throw GraphQLError(message: "Input list must wrap an input type")
50+
}
4651

4752
if let listValue = valueAST as? ListValue {
48-
return try .array(listValue.values.map({
53+
let values = try listValue.values.map { item in
4954
try valueFromAST(
50-
valueAST: $0,
51-
type: itemType as! GraphQLInputType,
55+
valueAST: item,
56+
type: itemType,
5257
variables: variables
5358
)
54-
}))
59+
}
60+
return .array(values)
5561
}
56-
57-
return try [valueFromAST(valueAST: valueAST, type: itemType as! GraphQLInputType, variables: variables)]
62+
63+
// Convert solitary value into single-value array
64+
return .array([
65+
try valueFromAST(
66+
valueAST: valueAST,
67+
type: itemType,
68+
variables: variables
69+
)
70+
])
5871
}
5972

6073
if let objectType = type as? GraphQLInputObjectType {
@@ -64,34 +77,30 @@ func valueFromAST(valueAST: Value, type: GraphQLInputType, variables: [String: M
6477

6578
let fields = objectType.fields
6679
let fieldASTs = objectValue.fields.keyMap({ $0.name.value })
67-
68-
return try .dictionary(fields.keys.reduce(OrderedDictionary<String, Map>()) { obj, fieldName in
69-
var obj = obj
70-
let field = fields[fieldName]!
80+
81+
var object = OrderedDictionary<String, Map>()
82+
for (fieldName, field) in fields {
7183
if let fieldAST = fieldASTs[fieldName] {
72-
let fieldValue = try valueFromAST(
84+
object[fieldName] = try valueFromAST(
7385
valueAST: fieldAST.value,
7486
type: field.type,
7587
variables: variables
7688
)
77-
obj[fieldName] = fieldValue
7889
} else {
7990
// If AST doesn't contain field, it is undefined
8091
if let defaultValue = field.defaultValue {
81-
obj[fieldName] = defaultValue
92+
object[fieldName] = defaultValue
8293
} else {
83-
obj[fieldName] = .undefined
94+
object[fieldName] = .undefined
8495
}
8596
}
86-
87-
return obj
88-
})
97+
}
98+
return .dictionary(object)
8999
}
90100

91-
guard let type = type as? GraphQLLeafType else {
92-
throw GraphQLError(message: "Must be leaf type")
101+
if let leafType = type as? GraphQLLeafType {
102+
return try leafType.parseLiteral(valueAST: valueAST)
93103
}
94104

95-
// If we've made it this far, it should be a scalar
96-
return try type.parseLiteral(valueAST: valueAST)
105+
throw GraphQLError(message: "Must be input type")
97106
}

0 commit comments

Comments
 (0)