Skip to content

Commit a2fc502

Browse files
committed
throw if it's impossible to coerce boolean value
Graphql spec states: > Input Coercion When expected as an input type, only boolean input values are accepted. All other input values must raise a request error indicating an incorrect type. (https://spec.graphql.org/draft/#sec-Boolean) So we can't coerce string, numeric and enum values. And here we faced main issue - because graphql parser considered "False" and "True" as enum values (correct boolean values is "true" and "false"). And if user passed "False" value to boolean argument it silently converted to true. This patch check that passed node type is boolean and raises if it's not so. Closes #14
1 parent 32c97f0 commit a2fc502

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

graphql/types.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,9 @@ types.boolean = types.scalar({
382382
description = "The `Boolean` scalar type represents `true` or `false`.",
383383
serialize = coerceBoolean,
384384
parseLiteral = function(node)
385+
if node.kind ~= 'boolean' then
386+
error(('Could not coerce value "%s" with type "%s" to type boolean'):format(node.value, node.kind))
387+
end
385388
return coerceBoolean(node.value)
386389
end,
387390
isValueOfTheType = isBoolean,

test/unit/graphql_test.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,3 +1028,32 @@ function g.test_types_for_different_schemas()
10281028
t.assert_error_msg_contains('Field "string_2" is not defined on type "Object"',
10291029
validate, schema_1, parse([[query { object_list { long_1 string_2 } }]]))
10301030
end
1031+
1032+
function g.test_boolean_coerce()
1033+
local query = types.object({
1034+
name = 'Query',
1035+
fields = {
1036+
test_boolean = {
1037+
kind = types.boolean.nonNull,
1038+
arguments = {
1039+
value = types.boolean,
1040+
non_null_value = types.boolean.nonNull,
1041+
}
1042+
},
1043+
}
1044+
})
1045+
1046+
local test_schema = schema.create({query = query})
1047+
1048+
validate(test_schema, parse([[ { test_boolean(value: true, non_null_value: true) } ]]))
1049+
validate(test_schema, parse([[ { test_boolean(value: false, non_null_value: false) } ]]))
1050+
validate(test_schema, parse([[ { test_boolean(value: null, non_null_value: true) } ]]))
1051+
1052+
-- Errors
1053+
t.assert_error_msg_contains('Could not coerce value "True" with type "enum" to type boolean',
1054+
validate, test_schema, parse([[ { test_boolean(value: True) } ]]))
1055+
t.assert_error_msg_contains('Could not coerce value "123" with type "int" to type boolean',
1056+
validate, test_schema, parse([[ { test_boolean(value: 123) } ]]))
1057+
t.assert_error_msg_contains('Could not coerce value "value" with type "string" to type boolean',
1058+
validate, test_schema, parse([[ { test_boolean(value: "value") } ]]))
1059+
end

0 commit comments

Comments
 (0)