@@ -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,41 +50,43 @@ 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]
70
+ let field = fields [ fieldName] !
72
71
let fieldAST = fieldASTs [ fieldName]
73
- guard fieldAST != nil else {
74
- obj [ fieldName] = . undefined
75
- return obj
76
- }
77
-
78
- var fieldValue = try valueFromAST (
79
- valueAST: fieldAST? . value,
80
- type: field!. type,
81
- variables: variables
82
- )
72
+ if let fieldAST = fieldAST {
73
+ let fieldValue = try valueFromAST (
74
+ valueAST: fieldAST. value,
75
+ type: field. type,
76
+ variables: variables
77
+ )
83
78
84
- if fieldValue == . null {
85
- fieldValue = field. flatMap ( { $0. defaultValue. map ( { . string( $0) } ) } )
79
+ if fieldValue == . null {
80
+ if let defaultValue = field. defaultValue {
81
+ obj [ fieldName] = . string( defaultValue)
82
+ } else {
83
+ obj [ fieldName] = . null
84
+ }
85
+ } else {
86
+ obj [ fieldName] = fieldValue
87
+ }
86
88
} else {
87
- obj [ fieldName] = fieldValue
89
+ obj [ fieldName] = . undefined
88
90
}
89
91
90
92
return obj
@@ -97,9 +99,5 @@ func valueFromAST(valueAST: Value?, type: GraphQLInputType, variables: [String:
97
99
98
100
let parsed = try type. parseLiteral ( valueAST: valueAST)
99
101
100
- guard parsed != . null else {
101
- return nil
102
- }
103
-
104
102
return parsed
105
103
}
0 commit comments