11import 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}
0 commit comments