@@ -64,62 +64,56 @@ func getVariableValue(schema: GraphQLSchema, definitionAST: VariableDefinition,
64
64
let type = typeFromAST ( schema: schema, inputTypeAST: definitionAST. type)
65
65
let variable = definitionAST. variable
66
66
67
- if type == nil || !isInputType ( type : type ) {
67
+ guard let inputType = type as? GraphQLInputType else {
68
68
throw GraphQLError (
69
69
message:
70
70
" Variable \" $ \( variable. name. value) \" expected value of type " +
71
71
" \" \( definitionAST. type) \" which cannot be used as an input type. " ,
72
72
nodes: [ definitionAST]
73
73
)
74
74
}
75
-
76
- let inputType = type as! GraphQLInputType
77
- let errors = try isValidValue ( value: input, type: inputType)
78
-
79
- if errors. isEmpty {
80
- if input == . null {
81
- if let defaultValue = definitionAST. defaultValue {
82
- return try valueFromAST ( valueAST: defaultValue, type: inputType)
83
- }
84
- else if !( inputType is GraphQLNonNull ) {
85
- return . null
75
+
76
+ if input == . undefined {
77
+ if let defaultValue = definitionAST. defaultValue {
78
+ return try valueFromAST ( valueAST: defaultValue, type: inputType)
79
+ } else {
80
+ if inputType is GraphQLNonNull {
81
+ throw GraphQLError ( message: " Non-nullable type \( inputType) must be provided. " )
82
+ } else {
83
+ return . undefined
86
84
}
87
85
}
88
-
89
- return try coerceValue ( type: inputType, value: input) !
90
86
}
91
-
92
- guard input != . null else {
87
+
88
+ let errors = try isValidValue ( value: input, type: inputType)
89
+ guard errors. isEmpty else {
90
+ let message = !errors. isEmpty ? " \n " + errors. joined ( separator: " \n " ) : " "
93
91
throw GraphQLError (
94
92
message:
95
- " Variable \" $ \( variable. name. value) \" of required type " +
96
- " \" \( definitionAST . type ) \" was not provided. " ,
93
+ " Variable \" $ \( variable. name. value) \" got invalid value " +
94
+ " \( input ) . \( message ) " , // TODO: "\(JSON.stringify(input)).\(message) ",
97
95
nodes: [ definitionAST]
98
96
)
99
97
}
100
-
101
- let message = !errors. isEmpty ? " \n " + errors. joined ( separator: " \n " ) : " "
102
-
103
- throw GraphQLError (
104
- message:
105
- " Variable \" $ \( variable. name. value) \" got invalid value " +
106
- " \( input) . \( message) " , // TODO: "\(JSON.stringify(input)).\(message)",
107
- nodes: [ definitionAST]
108
- )
98
+
99
+ return try coerceValue ( type: inputType, value: input)
109
100
}
110
101
111
102
/**
112
103
* Given a type and any value, return a runtime value coerced to match the type.
113
104
*/
114
- func coerceValue( type: GraphQLInputType , value: Map ) throws -> Map ? {
105
+ func coerceValue( type: GraphQLInputType , value: Map ) throws -> Map {
115
106
if let nonNull = type as? GraphQLNonNull {
116
107
// Note: we're not checking that the result of coerceValue is non-null.
117
108
// We only call this function after calling isValidValue.
118
- return try coerceValue ( type: nonNull. ofType as! GraphQLInputType , value: value) !
109
+ return try coerceValue ( type: nonNull. ofType as! GraphQLInputType , value: value)
110
+ }
111
+
112
+ guard value != . undefined else {
113
+ return . undefined
119
114
}
120
-
121
115
guard value != . null else {
122
- return nil
116
+ return . null
123
117
}
124
118
125
119
if let list = type as? GraphQLList {
@@ -129,35 +123,41 @@ func coerceValue(type: GraphQLInputType, value: Map) throws -> Map? {
129
123
var coercedValues : [ Map ] = [ ]
130
124
131
125
for item in value {
132
- coercedValues. append ( try coerceValue ( type: itemType as! GraphQLInputType , value: item) ! )
126
+ coercedValues. append ( try coerceValue ( type: itemType as! GraphQLInputType , value: item) )
133
127
}
134
128
135
129
return . array( coercedValues)
136
130
}
137
131
138
- return . array( [ try coerceValue ( type: itemType as! GraphQLInputType , value: value) ! ] )
132
+ return . array( [ try coerceValue ( type: itemType as! GraphQLInputType , value: value) ] )
139
133
}
140
134
141
- if let type = type as? GraphQLInputObjectType {
135
+ if let objectType = type as? GraphQLInputObjectType {
142
136
guard case . dictionary( let value) = value else {
143
- return nil
137
+ throw GraphQLError ( message : " Must be dictionary to extract to an input type " )
144
138
}
145
139
146
- let fields = type . fields
140
+ let fields = objectType . fields
147
141
148
142
return try . dictionary( fields. keys. reduce ( [ : ] ) { obj, fieldName in
149
- var objCopy = obj
150
- let field = fields [ fieldName]
151
-
152
- var fieldValue = try coerceValue ( type: field!. type, value: value [ fieldName] ?? . null)
153
-
154
- if fieldValue == . null {
155
- fieldValue = field. flatMap ( { $0. defaultValue } )
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
149
+ )
150
+ obj [ fieldName] = fieldValue
156
151
} else {
157
- objCopy [ fieldName] = fieldValue
152
+ // If AST doesn't contain field, it is undefined
153
+ if let defaultValue = field. defaultValue {
154
+ obj [ fieldName] = defaultValue
155
+ } else {
156
+ obj [ fieldName] = . undefined
157
+ }
158
158
}
159
-
160
- return objCopy
159
+
160
+ return obj
161
161
} )
162
162
}
163
163
0 commit comments