@@ -17,18 +17,14 @@ import OrderedCollections
1717 * | Enum Value | .string |
1818 *
1919 */
20- func valueFromAST( valueAST: Value ? , type: GraphQLInputType , variables: [ String : Map ] = [ : ] ) throws -> Map ? {
20+ func valueFromAST( valueAST: Value , type: GraphQLInputType , variables: [ String : Map ] = [ : ] ) throws -> Map {
2121 if let nonNullType = 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.
2525 return try valueFromAST ( valueAST: valueAST, type: nonNullType. ofType as! GraphQLInputType , variables: variables)
2626 }
2727
28- guard let valueAST = valueAST else {
29- return nil
30- }
31-
3228 if let variable = valueAST as? Variable {
3329 let variableName = variable. name. value
3430
@@ -38,7 +34,11 @@ func valueFromAST(valueAST: Value?, type: GraphQLInputType, variables: [String:
3834 // Note: we're not doing any checking that this variable is correct. We're
3935 // assuming that this query has been validated and the variable usage here
4036 // is of the correct type.
41- return variables [ variableName]
37+ if let variable = variables [ variableName] {
38+ return variable
39+ } else {
40+ return . null
41+ }
4242 }
4343
4444 if let list = type as? GraphQLList {
@@ -50,36 +50,38 @@ func valueFromAST(valueAST: Value?, type: GraphQLInputType, variables: [String:
5050 valueAST: $0,
5151 type: itemType as! GraphQLInputType ,
5252 variables: variables
53- ) !
53+ )
5454 } ) )
5555 }
5656
57- return try [ valueFromAST ( valueAST: valueAST, type: itemType as! GraphQLInputType , variables: variables) ! ]
57+ return try [ valueFromAST ( valueAST: valueAST, type: itemType as! GraphQLInputType , variables: variables) ]
5858 }
5959
6060 if let objectType = type as? GraphQLInputObjectType {
6161 guard let objectValue = valueAST as? ObjectValue else {
62- return nil
62+ throw GraphQLError ( message : " Must be object type " )
6363 }
6464
6565 let fields = objectType. fields
66-
6766 let fieldASTs = objectValue. fields. keyMap ( { $0. name. value } )
6867
69- return try . dictionary( fields. keys. reduce ( [ : ] as OrderedDictionary < String , Map > ) { obj, fieldName in
68+ return try . dictionary( fields. keys. reduce ( OrderedDictionary < String , Map > ( ) ) { obj, fieldName in
7069 var obj = obj
71- let field = fields [ fieldName]
72- let fieldAST = fieldASTs [ fieldName]
73- var fieldValue = try valueFromAST (
74- valueAST: fieldAST? . value,
75- type: field!. type,
76- variables: variables
77- )
78-
79- if fieldValue == . null {
80- fieldValue = field. flatMap ( { $0. defaultValue. map ( { . string( $0) } ) } )
81- } else {
70+ let field = fields [ fieldName] !
71+ if let fieldAST = fieldASTs [ fieldName] {
72+ let fieldValue = try valueFromAST (
73+ valueAST: fieldAST. value,
74+ type: field. type,
75+ variables: variables
76+ )
8277 obj [ fieldName] = fieldValue
78+ } else {
79+ // If AST doesn't contain field, it is undefined
80+ if let defaultValue = field. defaultValue {
81+ obj [ fieldName] = defaultValue
82+ } else {
83+ obj [ fieldName] = . undefined
84+ }
8385 }
8486
8587 return obj
@@ -90,11 +92,6 @@ func valueFromAST(valueAST: Value?, type: GraphQLInputType, variables: [String:
9092 throw GraphQLError ( message: " Must be leaf type " )
9193 }
9294
93- let parsed = try type. parseLiteral ( valueAST: valueAST)
94-
95- guard parsed != . null else {
96- return nil
97- }
98-
99- return parsed
95+ // If we've made it this far, it should be a literal
96+ return try type. parseLiteral ( valueAST: valueAST)
10097}
0 commit comments