From 92e7093f513031aae77256fe8b8bf1be0f2e7a22 Mon Sep 17 00:00:00 2001 From: Yaroslav Shumakov Date: Tue, 25 Jan 2022 21:29:41 +0300 Subject: [PATCH] Fix damaging schema Remove forcing defaultValue on directiveMap generation because it leads to adding redundant defaultValue field to Scalars itself into typeMap: { "typeMap": { "Int": { "defaultValue": 1, <- ERROR "parseLiteral": "function: 0x415a8fe8", "__type": "Scalar", "nonNull": {}, "serialize": "function: 0x415a8ed8", "isValueOfTheType": "function: 0x415a8e98", "name": "Int", "description": "..." } } } --- graphql/schema.lua | 1 - test/integration/graphql_test.lua | 67 +++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/graphql/schema.lua b/graphql/schema.lua index f2dc36b..b49e454 100644 --- a/graphql/schema.lua +++ b/graphql/schema.lua @@ -117,7 +117,6 @@ function schema:generateDirectiveMap() if argumentType == nil then error('Must supply type for argument "' .. name .. '" on "' .. directive.name .. '"') end - argumentType.defaultValue = argument.defaultValue self:generateTypeMap(argumentType) end end diff --git a/test/integration/graphql_test.lua b/test/integration/graphql_test.lua index 97e0723..6a0f88d 100644 --- a/test/integration/graphql_test.lua +++ b/test/integration/graphql_test.lua @@ -1804,3 +1804,70 @@ function g.test_skip_include_directives() t.assert_equals(errors, nil) t.assert_items_equals(data, {test = {uri = "uri1", uris = {uri = "uri2"}}}) end + +-- test simultaneous usage of mutation and directive default values +function g.test_mutation_and_directive_arguments_default_values() + local function callback(_, _) + return nil + end + + local mutation_schema = { + ['test_mutation'] = { + kind = types.string.nonNull, + arguments = { + object_arg = { + kind = types.inputObject({ + name = 'test_input_object', + fields = { + nested = { + kind = types.string, + defaultValue = 'default Value', + }, + }, + kind = types.string, + }), + }, + mutation_arg = { + kind = types.int, + defaultValue = 1, + }, + + }, + resolve = callback, + } + } + + local directives = { + types.directive({ + schema = schema, + name = 'timeout', + description = 'Request execute timeout', + arguments = { + seconds = { + kind = types.int, + description = 'Request timeout (in seconds). Default: 1 second', + defaultValue = 1, + }, + }, + onField = true, + }) + } + + local root = { + query = types.object({ + name = 'Query', + fields = {}, + }), + mutation = types.object({ + name = 'Mutation', + fields = mutation_schema or {}, + }), + directives = directives, + } + + local compiled_schema = schema.create(root, test_schema_name) + + -- test that schema.typeMap is not corrupted when both mutation and + -- directive default values used on the same argument type + t.assert_equals(compiled_schema.typeMap['Int'].defaultValue, nil) +end