Skip to content

Commit dbf92a5

Browse files
Parsed Maps include null/undefined values
1 parent 10f35a9 commit dbf92a5

File tree

3 files changed

+45
-43
lines changed

3 files changed

+45
-43
lines changed

Sources/GraphQL/Execution/Values.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,20 @@ func getArgumentValues(argDefs: [GraphQLArgumentDefinition], argASTs: [Argument]
3535
return try argDefs.reduce([:]) { result, argDef in
3636
var result = result
3737
let name = argDef.name
38-
let valueAST = argASTMap[name]?.value
38+
let argAST = argASTMap[name]
39+
40+
if let argAST = argAST {
41+
let valueAST = argAST.value
3942

40-
let value = try valueFromAST(
41-
valueAST: valueAST,
42-
type: argDef.type,
43-
variables: variableValues
44-
) ?? argDef.defaultValue
43+
let value = try valueFromAST(
44+
valueAST: valueAST,
45+
type: argDef.type,
46+
variables: variableValues
47+
)
4548

46-
if let value = value {
4749
result[name] = value
50+
} else {
51+
result[name] = .null
4852
}
4953

5054
return result
@@ -75,7 +79,7 @@ func getVariableValue(schema: GraphQLSchema, definitionAST: VariableDefinition,
7579
if errors.isEmpty {
7680
if input == .null {
7781
if let defaultValue = definitionAST.defaultValue {
78-
return try valueFromAST(valueAST: defaultValue, type: inputType)!
82+
return try valueFromAST(valueAST: defaultValue, type: inputType)
7983
}
8084
else if !(inputType is GraphQLNonNull) {
8185
return .null

Sources/GraphQL/Map/Map.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -915,12 +915,12 @@ extension Map {
915915
if debug {
916916
indentLevel += 1
917917
}
918+
919+
let filtered = dictionary.filter({ item in
920+
!item.value.isUndefined
921+
})
918922

919-
for (key, value) in dictionary.sorted(by: {$0.0 < $1.0}) {
920-
guard !value.isUndefined else {
921-
continue // Do not serialize undefined values
922-
}
923-
923+
for (key, value) in filtered.sorted(by: {$0.0 < $1.0}) {
924924
if debug {
925925
string += "\n"
926926
string += indent()
@@ -929,7 +929,7 @@ extension Map {
929929
string += escape(key) + ":" + serialize(map: value)
930930
}
931931

932-
if index != dictionary.count - 1 {
932+
if index != filtered.count - 1 {
933933
if debug {
934934
string += ", "
935935
} else {

Sources/GraphQL/Utilities/ValueFromAST.swift

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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,41 +50,43 @@ 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]
70+
let field = fields[fieldName]!
7271
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+
)
8378

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+
}
8688
} else {
87-
obj[fieldName] = fieldValue
89+
obj[fieldName] = .undefined
8890
}
8991

9092
return obj
@@ -97,9 +99,5 @@ func valueFromAST(valueAST: Value?, type: GraphQLInputType, variables: [String:
9799

98100
let parsed = try type.parseLiteral(valueAST: valueAST)
99101

100-
guard parsed != .null else {
101-
return nil
102-
}
103-
104102
return parsed
105103
}

0 commit comments

Comments
 (0)