From b92feeb5381c6d41defdf278a57462053b2a784e Mon Sep 17 00:00:00 2001 From: Yaroslav Shumakov Date: Sat, 16 Apr 2022 17:18:16 +0300 Subject: [PATCH 1/3] Fix coerse scalar list variables --- graphql/util.lua | 5 +++++ test/integration/graphql_test.lua | 28 ++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/graphql/util.lua b/graphql/util.lua index 5347d0b..67735f9 100644 --- a/graphql/util.lua +++ b/graphql/util.lua @@ -115,6 +115,11 @@ local function coerceValue(node, schemaType, variables, opts) node.name.value, variables[node.name.value], schemaType.name )) end + elseif type(value) == 'table' and schemaType.__type == 'List' and + type(schemaType.ofType) == 'table' and schemaType.ofType.parseValue ~= nil then + for k, v in ipairs(value) do + value[k] = schemaType.ofType.parseValue(v) + end end return value end diff --git a/test/integration/graphql_test.lua b/test/integration/graphql_test.lua index 6462450..e02a007 100644 --- a/test/integration/graphql_test.lua +++ b/test/integration/graphql_test.lua @@ -562,8 +562,8 @@ function g.test_custom_type_scalar_variables() if args.field == nil then return nil end - assert(type(args.field) == 'table', "Field is not a table! ") - assert(args.field.test ~= nil, "No field 'test' in object!") + t.assert_type(args.field, 'table', "Field is not a table! ") + t.assert_not_equals(args.field.test, nil, "No field 'test' in object!") return args.field end }, @@ -576,6 +576,20 @@ function g.test_custom_type_scalar_variables() return args.fields[1] end }, + ['test_json_type_list'] = { + arguments = { + array = types.list(json_type), + }, + kind = types.list(json_type), + resolve = function(_, args) + if args.array == nil then + return nil + end + t.assert_type(args.array[1], 'table', "Array element is not a table! ") + t.assert_not_equals(args.array[1].test, nil, "No field 'test' in array element!") + return args.array + end + }, ['test_custom_type_scalar_inputObject'] = { kind = types.string, arguments = { @@ -617,6 +631,16 @@ function g.test_custom_type_scalar_variables() variables = {field = box.NULL}, }), {test_json_type = 'null'}) + t.assert_equals(check_request([[ + query($array: [Json]) { + test_json_type_list( + array: $array + ) + } + ]], query_schema, nil, nil, { + variables = {array = {json.encode({test = 123})}}, + }), {test_json_type_list = {'{"test":123}'}}) + t.assert_equals(check_request([[ query { test_json_type( From eae104cad3e59c43c3cdd1327bf2ccbbcfb7cf1d Mon Sep 17 00:00:00 2001 From: Georgy Moiseev Date: Fri, 19 Aug 2022 11:00:56 +0300 Subject: [PATCH 2/3] Fix coerse scalar in input object variables --- graphql/util.lua | 18 +++++++++++++---- test/integration/graphql_test.lua | 33 ++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/graphql/util.lua b/graphql/util.lua index 67735f9..7d6901f 100644 --- a/graphql/util.lua +++ b/graphql/util.lua @@ -115,10 +115,20 @@ local function coerceValue(node, schemaType, variables, opts) node.name.value, variables[node.name.value], schemaType.name )) end - elseif type(value) == 'table' and schemaType.__type == 'List' and - type(schemaType.ofType) == 'table' and schemaType.ofType.parseValue ~= nil then - for k, v in ipairs(value) do - value[k] = schemaType.ofType.parseValue(v) + elseif type(value) == 'table' then + if schemaType.__type == 'List' and type(schemaType.ofType) == 'table' and schemaType.ofType.parseValue ~= nil then + for k, v in ipairs(value) do + value[k] = schemaType.ofType.parseValue(v) + end + end + + if schemaType.__type == 'InputObject' then + for k, v in pairs(value) do + if schemaType.fields[k] ~= nil and type(schemaType.fields[k].kind) == 'table' and + schemaType.fields[k].kind.parseValue ~= nil then + value[k] = schemaType.fields[k].kind.parseValue(v) + end + end end end return value diff --git a/test/integration/graphql_test.lua b/test/integration/graphql_test.lua index e02a007..1dc81e4 100644 --- a/test/integration/graphql_test.lua +++ b/test/integration/graphql_test.lua @@ -608,7 +608,28 @@ function g.test_custom_type_scalar_variables() resolve = function(_, args) return args.object.nested_object.field end - } + }, + ['test_json_type_inputObject'] = { + kind = json_type, + arguments = { + object = types.inputObject({ + name = 'ComplexJsonInputObject', + fields = { + nested_object = types.inputObject({ + name = 'ComplexJsonNestedInputObject', + fields = { + field = json_type, + } + }), + } + }), + }, + resolve = function(_, args) + t.assert_type(args.object.nested_object.field, 'table', "Object element is not a table! ") + t.assert_not_equals(args.object.nested_object.field.test, nil, "No field 'test' in object element!") + return args.object.nested_object.field + end + }, } t.assert_equals(check_request([[ @@ -779,6 +800,16 @@ function g.test_custom_type_scalar_variables() ]], query_schema, nil, nil, {variables = {fields = {'echo'}}}) end ) + + t.assert_equals(check_request([[ + query($nested_object: ComplexJsonNestedInputObject!) { + test_json_type_inputObject( + object: { nested_object: $nested_object } + ) + } + ]], query_schema, nil, nil, { + variables = {nested_object = { field = json.encode({test = 123})}}, + }), {test_json_type_inputObject = '{"test":123}'}) end function g.test_output_type_mismatch_error() From e6a5e4a4bfe314ea80088da3867c7d77629a7c37 Mon Sep 17 00:00:00 2001 From: Georgy Moiseev Date: Fri, 19 Aug 2022 12:02:01 +0300 Subject: [PATCH 3/3] Fix test variable name --- test/integration/graphql_test.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/graphql_test.lua b/test/integration/graphql_test.lua index 1dc81e4..765027e 100644 --- a/test/integration/graphql_test.lua +++ b/test/integration/graphql_test.lua @@ -797,7 +797,7 @@ function g.test_custom_type_scalar_variables() object: { nested_object: { field: $field } } ) } - ]], query_schema, nil, nil, {variables = {fields = {'echo'}}}) + ]], query_schema, nil, nil, {variables = {field = {'echo'}}}) end )