Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 428e8ec

Browse files
authored
Merge pull request #192 from tarantool/gh-183-fix-schema-without-strings
Fix case when all InputObject fields are filtered
2 parents 10ffd0a + 4637dde commit 428e8ec

File tree

5 files changed

+248
-47
lines changed

5 files changed

+248
-47
lines changed

graphql/gen_arguments.lua

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ local gen_arguments = {}
2121
--- key (and, then, offset) type
2222
---
2323
--- @treturn table `offset_type` is a record in case of compound (multi-part)
24-
--- primary key
24+
--- primary key (is not nil)
2525
local function get_primary_key_type(db_schema, collection_name)
2626
-- get name of field of primary key
2727
local _, index_meta = db_schema_helpers.get_primary_index_meta(
@@ -47,10 +47,12 @@ local function get_primary_key_type(db_schema, collection_name)
4747
assert(type(field_type) == 'string',
4848
'field type must be a string, got ' ..
4949
type(field_type))
50-
offset_fields[#offset_fields + 1] = {
51-
name = field_name,
52-
type = field_type,
53-
}
50+
if field_type ~= nil then
51+
offset_fields[#offset_fields + 1] = {
52+
name = field_name,
53+
type = field_type,
54+
}
55+
end
5456
end
5557

5658
local offset_type
@@ -79,7 +81,8 @@ end
7981
---
8082
--- @tparam[opt] function skip_cond
8183
---
82-
--- @return transformed avro-schema or nil (when mathed by skip_cond)
84+
--- @return transformed avro-schema or nil (when the type or all its fields are
85+
--- matched by skip_cond)
8386
local function recursive_nullable(e_schema, skip_cond)
8487
local avro_t = avro_helpers.avro_type(e_schema)
8588

@@ -103,6 +106,7 @@ local function recursive_nullable(e_schema, skip_cond)
103106
end
104107
end
105108

109+
if #res.fields == 0 then return nil end
106110
return res
107111
elseif avro_t == 'union' or
108112
avro_t == 'array' or avro_t == 'array*' or
@@ -121,7 +125,7 @@ end
121125
---
122126
--- @param e_schema (table or string) avro-schema with expanded references
123127
---
124-
--- @return transformed avro-schema
128+
--- @return transformed avro-schema or nil (empty fields case)
125129
local function recursive_replace_default_with_nullable(e_schema)
126130
local avro_t = avro_helpers.avro_type(e_schema)
127131

@@ -142,6 +146,7 @@ local function recursive_replace_default_with_nullable(e_schema)
142146
table.insert(res.fields, field)
143147
end
144148

149+
if #res.fields == 0 then return nil end
145150
return res
146151
elseif avro_t == 'union' then
147152
local res = {}
@@ -152,14 +157,17 @@ local function recursive_replace_default_with_nullable(e_schema)
152157
table.insert(res, new_child_type)
153158
end
154159

160+
if #res == 0 then return nil end
155161
return res
156162
elseif avro_t == 'array' or avro_t == 'array*' then
157163
local res = table.copy(e_schema)
158164
res.items = recursive_replace_default_with_nullable(e_schema.items)
165+
if res.items == nil then return nil end
159166
return res
160167
elseif avro_t == 'map' or avro_t == 'map*' then
161168
local res = table.copy(e_schema)
162169
res.values = recursive_replace_default_with_nullable(e_schema.values)
170+
if res.values == nil then return nil end
163171
return res
164172
end
165173

@@ -223,6 +231,7 @@ local function get_pcre_argument_type(db_schema, collection_name)
223231
return is_non_string_scalar or is_non_record_compound
224232

225233
end)
234+
if res == nil then return nil end
226235
res.name = collection_name .. '_pcre'
227236
return res
228237
end
@@ -268,10 +277,14 @@ local function get_update_argument_type(db_schema, collection_name)
268277
if not is_field_part_of_primary_key then
269278
local field = table.copy(field)
270279
field.type = recursive_nullable(field.type)
271-
table.insert(schema_update.fields, field)
280+
if field.type ~= nil then
281+
table.insert(schema_update.fields, field)
282+
end
272283
end
273284
end
274285

286+
if schema_update.fields == nil then return nil end
287+
275288
return schema_update
276289
end
277290

@@ -303,6 +316,9 @@ function gen_arguments.object_args(db_schema, collection_name)
303316
and (avro_t ~= 'record' and avro_t ~= 'record*')
304317
return is_non_comparable_scalar or is_non_record_compound
305318
end)
319+
320+
if res == nil then return {} end
321+
306322
return res.fields
307323
end
308324

@@ -324,7 +340,9 @@ function gen_arguments.list_args(db_schema, collection_name)
324340
local pcre_field
325341
if rex ~= nil then
326342
local pcre_type = get_pcre_argument_type(db_schema, collection_name)
327-
pcre_field = {name = 'pcre', type = pcre_type}
343+
if pcre_type ~= nil then
344+
pcre_field = {name = 'pcre', type = pcre_type}
345+
end
328346
end
329347

330348
return {
@@ -374,30 +392,42 @@ function gen_arguments.extra_args(db_schema, collection_name, opts)
374392
local e_schema = db_schema.e_schemas[schema_name]
375393

376394
local schema_insert = recursive_replace_default_with_nullable(e_schema)
377-
schema_insert.name = collection_name .. '_insert'
378-
schema_insert.type = 'record*' -- make the record nullable
395+
if schema_insert ~= nil then
396+
schema_insert.name = collection_name .. '_insert'
397+
schema_insert.type = 'record*' -- make the record nullable
398+
end
379399

380400
local schema_update = get_update_argument_type(db_schema, collection_name)
381401
local schema_delete = 'boolean*'
382402

383-
return {
384-
{name = 'insert', type = schema_insert},
385-
{name = 'update', type = schema_update},
386-
{name = 'delete', type = schema_delete},
387-
}, {
388-
insert = {
403+
local args = {}
404+
local args_meta = {}
405+
406+
if schema_insert ~= nil then
407+
table.insert(args, {name = 'insert', type = schema_insert})
408+
args_meta.insert = {
389409
add_to_mutations_only = true,
390410
add_to_top_fields_only = true,
391-
},
392-
update = {
411+
}
412+
end
413+
414+
if schema_update ~= nil then
415+
table.insert(args, {name = 'update', type = schema_update})
416+
args_meta.update = {
393417
add_to_mutations_only = true,
394418
add_to_top_fields_only = false,
395-
},
396-
delete = {
419+
}
420+
end
421+
422+
if schema_delete ~= nil then
423+
table.insert(args, {name = 'delete', type = schema_delete})
424+
args_meta.delete = {
397425
add_to_mutations_only = true,
398426
add_to_top_fields_only = false,
399-
},
400-
}
427+
}
428+
end
429+
430+
return args, args_meta
401431
end
402432

403433
return gen_arguments
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env tarantool
2+
3+
local fio = require('fio')
4+
5+
-- require in-repo version of graphql/ sources despite current working directory
6+
package.path = fio.abspath(debug.getinfo(1).source:match("@?(.*/)")
7+
:gsub('/./', '/'):gsub('/+$', '')) .. '/../../?.lua' .. ';' .. package.path
8+
9+
local test_utils = require('test.test_utils')
10+
local testdata = require('test.testdata.no_eq_comparable_fields_testdata')
11+
12+
box.cfg({})
13+
14+
test_utils.run_testdata(testdata)
15+
16+
os.exit()

test/test_utils.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,20 @@ function test_utils.show_trace(func, ...)
163163
))
164164
end
165165

166+
-- needed to compare a dump with floats/doubles, because, say,
167+
-- `tonumber(tostring(1/3)) == 1/3` is `false`
168+
function test_utils.deeply_number_tostring(t)
169+
if type(t) == 'table' then
170+
local res = {}
171+
for k, v in pairs(t) do
172+
res[k] = test_utils.deeply_number_tostring(v)
173+
end
174+
return res
175+
elseif type(t) == 'number' then
176+
return tostring(t)
177+
else
178+
return table.deepcopy(t)
179+
end
180+
end
181+
166182
return test_utils

test/testdata/common_testdata.lua

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,6 @@ local common_testdata = {}
1212
-- forward declaration
1313
local type_mismatch_cases
1414

15-
-- needed to compare a dump with floats/doubles, because, say,
16-
-- `tonumber(tostring(1/3)) == 1/3` is `false`
17-
local function deeply_number_tostring(t)
18-
if type(t) == 'table' then
19-
local res = {}
20-
for k, v in pairs(t) do
21-
res[k] = deeply_number_tostring(v)
22-
end
23-
return res
24-
elseif type(t) == 'number' then
25-
return tostring(t)
26-
else
27-
return table.deepcopy(t)
28-
end
29-
end
30-
3115
function common_testdata.get_test_metadata()
3216
local schemas = json.decode([[{
3317
"user": {
@@ -1004,8 +988,8 @@ function common_testdata.run_queries(gql_wrapper)
1004988
test_utils.show_trace(function()
1005989
local variables_6_1 = {limit = 10}
1006990
local result = gql_query_6:execute(variables_6_1)
1007-
local exp_result_6_1 = deeply_number_tostring(exp_result_6_1)
1008-
local result = deeply_number_tostring(result)
991+
local exp_result_6_1 = test_utils.deeply_number_tostring(exp_result_6_1)
992+
local result = test_utils.deeply_number_tostring(result)
1009993
test:is_deeply(result.data, exp_result_6_1, '6_1')
1010994
end)
1011995

@@ -1065,16 +1049,16 @@ function common_testdata.run_queries(gql_wrapper)
10651049
]]):strip())
10661050

10671051
test_utils.show_trace(function()
1068-
local exp_result_6_2 = deeply_number_tostring(exp_result_6_2)
1052+
local exp_result_6_2 = test_utils.deeply_number_tostring(exp_result_6_2)
10691053

10701054
local variables_6_2 = {limit = 10, in_stock = true}
10711055
local result = gql_query_6:execute(variables_6_2)
1072-
local result = deeply_number_tostring(result)
1056+
local result = test_utils.deeply_number_tostring(result)
10731057
test:is_deeply(result.data, exp_result_6_2, '6_2')
10741058

10751059
local variables_6_2 = {limit = 10}
10761060
local result = gql_query_6_i_true:execute(variables_6_2)
1077-
local result = deeply_number_tostring(result)
1061+
local result = test_utils.deeply_number_tostring(result)
10781062
test:is_deeply(result.data, exp_result_6_2, '6_2')
10791063
end)
10801064

@@ -1134,16 +1118,16 @@ function common_testdata.run_queries(gql_wrapper)
11341118
]]):strip())
11351119

11361120
test_utils.show_trace(function()
1137-
local exp_result_6_3 = deeply_number_tostring(exp_result_6_3)
1121+
local exp_result_6_3 = test_utils.deeply_number_tostring(exp_result_6_3)
11381122

11391123
local variables_6_3 = {limit = 10, in_stock = false}
11401124
local result = gql_query_6:execute(variables_6_3)
1141-
local result = deeply_number_tostring(result)
1125+
local result = test_utils.deeply_number_tostring(result)
11421126
test:is_deeply(result.data, exp_result_6_3, '6_3')
11431127

11441128
local variables_6_3 = {limit = 10}
11451129
local result = gql_query_6_i_false:execute(variables_6_3)
1146-
local result = deeply_number_tostring(result)
1130+
local result = test_utils.deeply_number_tostring(result)
11471131
test:is_deeply(result.data, exp_result_6_3, '6_3')
11481132
end)
11491133

0 commit comments

Comments
 (0)