Skip to content

Commit bab6895

Browse files
Fixes unincluded args to use default values
1 parent 1013374 commit bab6895

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

Sources/GraphQL/Execution/Execute.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ func shouldIncludeNode(exeContext: ExecutionContext, directives: [Directive] = [
594594
let skip = try getArgumentValues(
595595
argDefs: GraphQLSkipDirective.args,
596596
argASTs: skipAST.arguments,
597-
variableValues: exeContext.variableValues
597+
variables: exeContext.variableValues
598598
)
599599

600600
if skip["if"] == .bool(true) {
@@ -606,7 +606,7 @@ func shouldIncludeNode(exeContext: ExecutionContext, directives: [Directive] = [
606606
let include = try getArgumentValues(
607607
argDefs: GraphQLIncludeDirective.args,
608608
argASTs: includeAST.arguments,
609-
variableValues: exeContext.variableValues
609+
variables: exeContext.variableValues
610610
)
611611

612612
if include["if"] == .bool(false) {
@@ -685,7 +685,7 @@ public func resolveField(
685685
let args = try getArgumentValues(
686686
argDefs: fieldDef.args,
687687
argASTs: fieldAST.arguments,
688-
variableValues: exeContext.variableValues
688+
variables: exeContext.variableValues
689689
)
690690

691691
// The resolve func's optional third argument is a context value that

Sources/GraphQL/Execution/Values.swift

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,32 @@ func getVariableValues(schema: GraphQLSchema, definitionASTs: [VariableDefinitio
2626
* Prepares an object map of argument values given a list of argument
2727
* definitions and list of argument AST nodes.
2828
*/
29-
func getArgumentValues(argDefs: [GraphQLArgumentDefinition], argASTs: [Argument]?, variableValues: [String: Map] = [:]) throws -> Map {
29+
func getArgumentValues(argDefs: [GraphQLArgumentDefinition], argASTs: [Argument]?, variables: [String: Map] = [:]) throws -> Map {
3030
guard let argASTs = argASTs else {
3131
return [:]
3232
}
3333

3434
let argASTMap = argASTs.keyMap({ $0.name.value })
35-
36-
return try argDefs.reduce([:]) { result, argDef in
37-
var result = result
38-
let name = argDef.name
39-
let argAST = argASTMap[name]
40-
41-
if let argAST = argAST {
42-
let valueAST = argAST.value
43-
44-
let value = try valueFromAST(
45-
valueAST: valueAST,
35+
36+
var args = OrderedDictionary<String, Map>()
37+
for argDef in argDefs {
38+
let argName = argDef.name
39+
if let argAST = argASTMap[argName] {
40+
args[argName] = try valueFromAST(
41+
valueAST: argAST.value,
4642
type: argDef.type,
47-
variables: variableValues
43+
variables: variables
4844
)
49-
50-
result[name] = value
5145
} else {
52-
result[name] = .null
46+
// If AST doesn't contain field, it is undefined
47+
if let defaultValue = argDef.defaultValue {
48+
args[argName] = defaultValue
49+
} else {
50+
args[argName] = .undefined
51+
}
5352
}
54-
55-
return result
5653
}
54+
return .dictionary(args)
5755
}
5856

5957

Sources/GraphQL/Subscription/Subscribe.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func executeSubscription(
195195

196196
// Build a map of arguments from the field.arguments AST, using the
197197
// variables scope to fulfill any variable references.
198-
let args = try getArgumentValues(argDefs: fieldDef.args, argASTs: fieldNode.arguments, variableValues: context.variableValues)
198+
let args = try getArgumentValues(argDefs: fieldDef.args, argASTs: fieldNode.arguments, variables: context.variableValues)
199199

200200
// The resolve function's optional third argument is a context value that
201201
// is provided to every resolve function within an execution. It is commonly

0 commit comments

Comments
 (0)