diff --git a/Sources/GraphQL/Utilities/ValueFromAST.swift b/Sources/GraphQL/Utilities/ValueFromAST.swift index 88c5d8a0..114aae21 100644 --- a/Sources/GraphQL/Utilities/ValueFromAST.swift +++ b/Sources/GraphQL/Utilities/ValueFromAST.swift @@ -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. @@ -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 diff --git a/Tests/GraphQLTests/UtilitiesTests/BuildASTSchemaTests.swift b/Tests/GraphQLTests/UtilitiesTests/BuildASTSchemaTests.swift index 47e6d483..c8375c34 100644 --- a/Tests/GraphQLTests/UtilitiesTests/BuildASTSchemaTests.swift +++ b/Tests/GraphQLTests/UtilitiesTests/BuildASTSchemaTests.swift @@ -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) + } }