tarantool / graphql

GraphQL implementation in Lua
MIT License
15 stars 3 forks source link

parseValue doesn't compatible with list of scalars #13

Closed olegrok closed 2 years ago

olegrok commented 3 years ago

The main problem that our graphql coerseValue doesn't consider hierarchical structure of input object.

See also https://github.com/tarantool/cartridge/commit/fb4b3a360f8f6d6d34384d0ec8961c7dc4cadf7b

diff --git a/test/integration/graphql_test.lua b/test/integration/graphql_test.lua
index a4d11e8..0bf9704 100644
--- a/test/integration/graphql_test.lua
+++ b/test/integration/graphql_test.lua
@@ -558,11 +558,25 @@ 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
         },
+        ['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_list'] = {
             kind = types.string,
             arguments = {
@@ -623,6 +637,16 @@ function g.test_custom_type_scalar_variables()
         variables = {},
     }), {test_json_type = 'null'})

+    t.assert_equals(check_request([[
+        query($array: [Json]) {
+            test_json_type_list(
+                array: $array
+            )
+        }
+    ]], query_schema, {
+        variables = {array = {json.encode({test = 123})}},
+    }), {test_json_type_list = {'{"test":123}'}})
+
     t.assert_equals(check_request([[
         query($field: CustomString!) {
             test_custom_type_scalar(
Totktonada commented 2 years ago

It seems, now I should do something like the following to reproduce the problem:

diff --git a/graphql/schema.lua b/graphql/schema.lua
index cff57b4..e1f79b0 100644
--- a/graphql/schema.lua
+++ b/graphql/schema.lua
@@ -183,7 +183,9 @@ function schema:generateTypeMap(node)
       end

       -- HACK: resolve type names to real types
-      field.kind = types.resolve(field.kind, self.name)
+      if type(field.kind) == 'string' then
+        field.kind = types.resolve(field.kind, self.name)
+      end
       self:generateTypeMap(field.kind)
     end
   end

After this I see it:

$ luatest -v -p test_custom_type_scalar_variables
Started on Sat Apr  9 03:08:23 2022
    integration.test_custom_type_scalar_variables ... (0.001s) fail
...tarantool-meta/graphql/test/integration/graphql_test.lua:644: expected: {test_json_type_list = {"{\"test\":123}"}}
actual: {test_json_type_list = cdata<void *>: NULL}
=========================================================

Failed tests:
-------------

1) integration.test_custom_type_scalar_variables
...tarantool-meta/graphql/test/integration/graphql_test.lua:644: expected: {test_json_type_list = {"{\"test\":123}"}}
actual: {test_json_type_list = cdata<void *>: NULL}
stack traceback:
    ...tarantool-meta/graphql/test/integration/graphql_test.lua:644: in function 'integration.test_custom_type_scalar_variables'
    ...
    [C]: in function 'xpcall'

Ran 1 tests in 0.001 seconds, 0 succeeded, 1 failed, 93 not selected

=========================================================
Failed tests:

integration.test_custom_type_scalar_variables

(My module version is 0.1.4.)

no1seman commented 2 years ago

@Totktonada since @olegrok wrote a test check_request() implementaion changed and it fails because of wrong arguments