Skip to content
Merged
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
3 changes: 3 additions & 0 deletions graphql/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,9 @@ types.boolean = types.scalar({
description = "The `Boolean` scalar type represents `true` or `false`.",
serialize = coerceBoolean,
parseLiteral = function(node)
if node.kind ~= 'boolean' then
error(('Could not coerce value "%s" with type "%s" to type boolean'):format(node.value, node.kind))
end
return coerceBoolean(node.value)
end,
isValueOfTheType = isBoolean,
Expand Down
29 changes: 29 additions & 0 deletions test/unit/graphql_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1028,3 +1028,32 @@ function g.test_types_for_different_schemas()
t.assert_error_msg_contains('Field "string_2" is not defined on type "Object"',
validate, schema_1, parse([[query { object_list { long_1 string_2 } }]]))
end

function g.test_boolean_coerce()
local query = types.object({
name = 'Query',
fields = {
test_boolean = {
kind = types.boolean.nonNull,
arguments = {
value = types.boolean,
non_null_value = types.boolean.nonNull,
}
},
}
})

local test_schema = schema.create({query = query})

validate(test_schema, parse([[ { test_boolean(value: true, non_null_value: true) } ]]))
validate(test_schema, parse([[ { test_boolean(value: false, non_null_value: false) } ]]))
validate(test_schema, parse([[ { test_boolean(value: null, non_null_value: true) } ]]))

-- Errors
t.assert_error_msg_contains('Could not coerce value "True" with type "enum" to type boolean',
validate, test_schema, parse([[ { test_boolean(value: True) } ]]))
t.assert_error_msg_contains('Could not coerce value "123" with type "int" to type boolean',
validate, test_schema, parse([[ { test_boolean(value: 123) } ]]))
t.assert_error_msg_contains('Could not coerce value "value" with type "string" to type boolean',
validate, test_schema, parse([[ { test_boolean(value: "value") } ]]))
end