@@ -17,18 +17,14 @@ import OrderedCollections
17
17
* | Enum Value | .string |
18
18
*
19
19
*/
20
- func valueFromAST( valueAST: Value ? , type: GraphQLInputType , variables: [ String : Map ] = [ : ] ) throws -> Map ? {
20
+ func valueFromAST( valueAST: Value , type: GraphQLInputType , variables: [ String : Map ] = [ : ] ) throws -> Map {
21
21
if let nonNullType = type as? GraphQLNonNull {
22
22
// Note: we're not checking that the result of valueFromAST is non-null.
23
23
// We're assuming that this query has been validated and the value used
24
24
// here is of the correct type.
25
25
return try valueFromAST ( valueAST: valueAST, type: nonNullType. ofType as! GraphQLInputType , variables: variables)
26
26
}
27
27
28
- guard let valueAST = valueAST else {
29
- return nil
30
- }
31
-
32
28
if let variable = valueAST as? Variable {
33
29
let variableName = variable. name. value
34
30
@@ -38,7 +34,11 @@ func valueFromAST(valueAST: Value?, type: GraphQLInputType, variables: [String:
38
34
// Note: we're not doing any checking that this variable is correct. We're
39
35
// assuming that this query has been validated and the variable usage here
40
36
// is of the correct type.
41
- return variables [ variableName]
37
+ if let variable = variables [ variableName] {
38
+ return variable
39
+ } else {
40
+ return . null
41
+ }
42
42
}
43
43
44
44
if let list = type as? GraphQLList {
@@ -50,36 +50,38 @@ func valueFromAST(valueAST: Value?, type: GraphQLInputType, variables: [String:
50
50
valueAST: $0,
51
51
type: itemType as! GraphQLInputType ,
52
52
variables: variables
53
- ) !
53
+ )
54
54
} ) )
55
55
}
56
56
57
- return try [ valueFromAST ( valueAST: valueAST, type: itemType as! GraphQLInputType , variables: variables) ! ]
57
+ return try [ valueFromAST ( valueAST: valueAST, type: itemType as! GraphQLInputType , variables: variables) ]
58
58
}
59
59
60
60
if let objectType = type as? GraphQLInputObjectType {
61
61
guard let objectValue = valueAST as? ObjectValue else {
62
- return nil
62
+ throw GraphQLError ( message : " Must be object type " )
63
63
}
64
64
65
65
let fields = objectType. fields
66
-
67
66
let fieldASTs = objectValue. fields. keyMap ( { $0. name. value } )
68
67
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
70
69
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
+ )
82
77
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
+ }
83
85
}
84
86
85
87
return obj
@@ -90,11 +92,6 @@ func valueFromAST(valueAST: Value?, type: GraphQLInputType, variables: [String:
90
92
throw GraphQLError ( message: " Must be leaf type " )
91
93
}
92
94
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)
100
97
}
0 commit comments