Skip to content

Commit 5bee791

Browse files
Merge pull request #173 from NeedleInAJayStack/fix/allow-null-ast-values
AST parsing supports null default values on input objects
2 parents 1cef9e2 + 9b30f3a commit 5bee791

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

Sources/GraphQL/Utilities/ValueFromAST.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ func valueFromAST(
2323
variables: [String: Map] = [:]
2424
) throws -> Map {
2525
if let nonNull = type as? GraphQLNonNull {
26+
guard !(valueAST is NullValue) else {
27+
throw GraphQLError(message: "Null value provided to non-nullable type")
28+
}
29+
2630
// Note: we're not checking that the result of valueFromAST is non-null.
2731
// We're assuming that this query has been validated and the value used
2832
// here is of the correct type.
@@ -32,6 +36,10 @@ func valueFromAST(
3236
return try valueFromAST(valueAST: valueAST, type: nonNullType, variables: variables)
3337
}
3438

39+
guard !(valueAST is NullValue) else {
40+
return .null
41+
}
42+
3543
if let variable = valueAST as? Variable {
3644
let variableName = variable.name.value
3745

Tests/GraphQLTests/UtilitiesTests/BuildASTSchemaTests.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,4 +1186,23 @@ import Testing
11861186
// 'mutation' operation.
11871187
#expect(schema.mutationType == nil)
11881188
}
1189+
1190+
@Test func supportsNullLiterals() throws {
1191+
let sdl = """
1192+
input MyInput {
1193+
nullLiteral: String!
1194+
}
1195+
1196+
type Query {
1197+
field(in: MyInput = null): String
1198+
}
1199+
"""
1200+
try #expect(cycleSDL(sdl: sdl) == sdl)
1201+
1202+
let schema = try buildSchema(source: sdl)
1203+
1204+
let rootFields = try #require(schema.getType(name: "Query") as? GraphQLObjectType)
1205+
.getFields()
1206+
#expect(rootFields["field"]?.args[0].defaultValue == .null)
1207+
}
11891208
}

0 commit comments

Comments
 (0)