Skip to content

Commit 9f63204

Browse files
Cleanup and documentation
1 parent 2dd8a86 commit 9f63204

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

Sources/GraphQL/Utilities/ValueFromAST.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ func valueFromAST(valueAST: Value, type: GraphQLInputType, variables: [String: M
6868
return try .dictionary(fields.keys.reduce(OrderedDictionary<String, Map>()) { obj, fieldName in
6969
var obj = obj
7070
let field = fields[fieldName]!
71-
let fieldAST = fieldASTs[fieldName]
72-
if let fieldAST = fieldAST {
71+
if let fieldAST = fieldASTs[fieldName] {
7372
let fieldValue = try valueFromAST(
7473
valueAST: fieldAST.value,
7574
type: field.type,
7675
variables: variables
7776
)
7877
obj[fieldName] = fieldValue
7978
} else {
79+
// If AST doesn't contain field, it is undefined
8080
if let defaultValue = field.defaultValue {
8181
obj[fieldName] = defaultValue
8282
} else {
@@ -92,7 +92,6 @@ func valueFromAST(valueAST: Value, type: GraphQLInputType, variables: [String: M
9292
throw GraphQLError(message: "Must be leaf type")
9393
}
9494

95-
let parsed = try type.parseLiteral(valueAST: valueAST)
96-
97-
return parsed
95+
// If we've made it this far, it should be a literal
96+
return try type.parseLiteral(valueAST: valueAST)
9897
}

Tests/GraphQLTests/InputTests/InputTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import NIO
55

66
class InputTests : XCTestCase {
77

8+
// Test that input objects parse as expected from non-null literals
89
func testInputParsing() throws {
910
struct Echo : Codable {
1011
let field1: String?
@@ -110,6 +111,7 @@ class InputTests : XCTestCase {
110111
)
111112
}
112113

114+
// Test that inputs parse as expected when null literals are present
113115
func testInputParsingDefinedNull() throws {
114116
struct Echo : Codable {
115117
let field1: String?
@@ -215,6 +217,7 @@ class InputTests : XCTestCase {
215217
)
216218
}
217219

220+
// Test that input objects parse as expected when there are missing fields with no default
218221
func testInputParsingUndefined() throws {
219222
struct Echo : Codable {
220223
let field1: String?
@@ -319,6 +322,7 @@ class InputTests : XCTestCase {
319322
)
320323
}
321324

325+
// Test that input objects parse as expected when there are missing fields with defaults
322326
func testInputParsingUndefinedWithDefault() throws {
323327
struct Echo : Codable {
324328
let field1: String?

0 commit comments

Comments
 (0)