Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Sources/GraphQL/Utilities/ValueFromAST.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func valueFromAST(
variables: [String: Map] = [:]
) throws -> Map {
if let nonNull = type as? GraphQLNonNull {
guard !(valueAST is NullValue) else {
throw GraphQLError(message: "Null value provided to non-nullable type")
}

// Note: we're not checking that the result of valueFromAST is non-null.
// We're assuming that this query has been validated and the value used
// here is of the correct type.
Expand All @@ -32,6 +36,10 @@ func valueFromAST(
return try valueFromAST(valueAST: valueAST, type: nonNullType, variables: variables)
}

guard !(valueAST is NullValue) else {
return .null
}

if let variable = valueAST as? Variable {
let variableName = variable.name.value

Expand Down
19 changes: 19 additions & 0 deletions Tests/GraphQLTests/UtilitiesTests/BuildASTSchemaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1186,4 +1186,23 @@ import Testing
// 'mutation' operation.
#expect(schema.mutationType == nil)
}

@Test func supportsNullLiterals() throws {
let sdl = """
input MyInput {
nullLiteral: String!
}

type Query {
field(in: MyInput = null): String
}
"""
try #expect(cycleSDL(sdl: sdl) == sdl)

let schema = try buildSchema(source: sdl)

let rootFields = try #require(schema.getType(name: "Query") as? GraphQLObjectType)
.getFields()
#expect(rootFields["field"]?.args[0].defaultValue == .null)
}
}