From 009b3a2c96f097521339b7e341c52a42941536bf Mon Sep 17 00:00:00 2001 From: Georgy Moiseev Date: Wed, 1 Dec 2021 12:24:39 +0300 Subject: [PATCH 1/2] Fix describing type with table in schema Before this patch, describing argument type in schema with a table resulted in "No value provided" error on query execution. Based on PR #22 by @no1seman --- graphql/execute.lua | 7 +++++++ test/integration/graphql_test.lua | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/graphql/execute.lua b/graphql/execute.lua index a2b9d56..2193341 100644 --- a/graphql/execute.lua +++ b/graphql/execute.lua @@ -260,6 +260,13 @@ local function getFieldEntry(objectType, object, fields, context) local arguments = util.map(fieldType.arguments or {}, function(argument, name) local supplied = argumentMap[name] and argumentMap[name].value + + -- This line of code provides support to using + -- `arg = { kind = type, description = desc }` + -- type declaration in query input arguments + -- instead of `arg = type` one. + if argument.kind then argument = argument.kind end + return util.coerceValue(supplied, argument, context.variables, { strict_non_null = true, defaultValues = defaultValues, diff --git a/test/integration/graphql_test.lua b/test/integration/graphql_test.lua index faff008..b76c699 100644 --- a/test/integration/graphql_test.lua +++ b/test/integration/graphql_test.lua @@ -1588,3 +1588,28 @@ function g.test_descriptions() local input_object_arg_described = util.find_by_name(test_input_object.inputFields, 'input_object_arg_described') t.assert_equals(input_object_arg_described.description, 'input object argument') end + +function g.test_schema_input_arg_described_with_kind() + local function callback(_, args) + return args[1].value + end + + local query_schema = { + ['test'] = { + kind = types.string.nonNull, + arguments = { + arg = { + kind = types.string.nonNull, + }, + }, + resolve = callback, + } + } + + local query = [[ + { test(arg: "A") } + ]] + + local _, errors = check_request(query, query_schema, {}) + t.assert_equals(errors, nil) +end From 489d8be6fbb9b1a74ee5f945b1f2e2971e849c53 Mon Sep 17 00:00:00 2001 From: Georgy Moiseev Date: Wed, 1 Dec 2021 15:17:01 +0300 Subject: [PATCH 2/2] Fix variable validation for type as table Before this patch, describing argument type in schema with a table resulted in "unsupported Lua type 'function'" error on variable type validation. Based on PR #22 by @no1seman --- graphql/rules.lua | 7 +++++++ test/integration/graphql_test.lua | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/graphql/rules.lua b/graphql/rules.lua index dee1808..bb37bb2 100644 --- a/graphql/rules.lua +++ b/graphql/rules.lua @@ -517,6 +517,13 @@ local function isVariableTypesValid(argument, argumentType, context, variableType = types.nonNull(variableType) end + -- This line of code provides support to using + -- `arg = { kind = type, description = desc }` + -- type declaration in query input arguments + -- instead of `arg = type` one when passing + -- argument with a variable. + if argumentType.kind ~= nil then argumentType = argumentType.kind end + if not isTypeSubTypeOf(variableType, argumentType, context) then return false, ('Variable "%s" type mismatch: the variable type "%s" ' .. 'is not compatible with the argument type "%s"'):format(variableName, diff --git a/test/integration/graphql_test.lua b/test/integration/graphql_test.lua index b76c699..500a41b 100644 --- a/test/integration/graphql_test.lua +++ b/test/integration/graphql_test.lua @@ -1613,3 +1613,29 @@ function g.test_schema_input_arg_described_with_kind() local _, errors = check_request(query, query_schema, {}) t.assert_equals(errors, nil) end + +function g.test_schema_input_arg_described_with_kind_variable_pass() + local function callback(_, args) + return args[1].value + end + + local query_schema = { + ['test'] = { + kind = types.string.nonNull, + arguments = { + arg = { + kind = types.string.nonNull, + }, + }, + resolve = callback, + } + } + + local query = [[ + query ($arg: String!) { test(arg: $arg) } + ]] + local variables = { arg = 'B' } + + local _, errors = check_request(query, query_schema, nil, nil, { variables = variables }) + t.assert_equals(errors, nil) +end