1
1
import Foundation
2
+ import OrderedCollections
2
3
3
4
/**
4
5
* Prepares an object map of variableValues of the correct type based on the
@@ -96,40 +97,40 @@ func getVariableValue(schema: GraphQLSchema, definitionAST: VariableDefinition,
96
97
)
97
98
}
98
99
99
- return try coerceValue ( type : inputType , value : input )
100
+ return try coerceValue ( value : input , type : inputType )
100
101
}
101
102
102
103
/**
103
104
* Given a type and any value, return a runtime value coerced to match the type.
104
105
*/
105
- func coerceValue( type : GraphQLInputType , value : Map ) throws -> Map {
106
+ func coerceValue( value : Map , type : GraphQLInputType ) throws -> Map {
106
107
if let nonNull = type as? GraphQLNonNull {
107
108
// Note: we're not checking that the result of coerceValue is non-null.
108
109
// 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)
110
114
}
111
115
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
117
118
}
118
119
119
120
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
+ }
121
124
122
125
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)
127
128
}
128
-
129
129
return . array( coercedValues)
130
130
}
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) ] )
133
134
}
134
135
135
136
if let objectType = type as? GraphQLInputObjectType {
@@ -138,38 +139,29 @@ func coerceValue(type: GraphQLInputType, value: Map) throws -> Map {
138
139
}
139
140
140
141
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
149
149
)
150
- obj [ fieldName] = fieldValue
151
150
} else {
152
151
// If AST doesn't contain field, it is undefined
153
152
if let defaultValue = field. defaultValue {
154
- obj [ fieldName] = defaultValue
153
+ object [ fieldName] = defaultValue
155
154
} else {
156
- obj [ fieldName] = . undefined
155
+ object [ fieldName] = . undefined
157
156
}
158
157
}
159
-
160
- return obj
161
- } )
158
+ }
159
+ return . dictionary( object)
162
160
}
163
161
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 )
166
164
}
167
165
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 " )
175
167
}
0 commit comments