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

Commit 603857f

Browse files
committed
Allow nulls in avro schema, test nullable indexes
Fixes #43.
1 parent 3b55445 commit 603857f

10 files changed

+873
-23
lines changed

graphql/tarantool_graphql.lua

+36-18
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,30 @@ local tarantool_graphql = {}
2323
local gql_type
2424

2525
local function avro_type(avro_schema)
26-
if type(avro_schema) == 'table' and avro_schema.type == 'record' then
27-
return 'record'
28-
elseif type(avro_schema) == 'table' and utils.is_array(avro_schema) then
29-
return 'enum'
30-
elseif type(avro_schema) == 'string' and avro_schema == 'int' then
31-
return 'int'
32-
elseif type(avro_schema) == 'string' and avro_schema == 'long' then
33-
return 'long'
34-
elseif type(avro_schema) == 'string' and avro_schema == 'string' then
35-
return 'string'
36-
else
37-
error('unrecognized avro-schema type: ' .. json.encode(avro_schema))
26+
if type(avro_schema) == 'table' then
27+
if avro_schema.type == 'record' then
28+
return 'record'
29+
elseif avro_schema.type == 'record*' then
30+
return 'record*'
31+
elseif utils.is_array(avro_schema) then
32+
return 'union'
33+
end
34+
elseif type(avro_schema) == 'string' then
35+
if avro_schema == 'int' then
36+
return 'int'
37+
elseif avro_schema == 'int*' then
38+
return 'int*'
39+
elseif avro_schema == 'long' then
40+
return 'long'
41+
elseif avro_schema == 'long*' then
42+
return 'long*'
43+
elseif avro_schema == 'string' then
44+
return 'string'
45+
elseif avro_schema == 'string*' then
46+
return 'string*'
47+
end
3848
end
49+
error('unrecognized avro-schema type: ' .. json.encode(avro_schema))
3950
end
4051

4152
-- XXX: recursive skip several NonNull's?
@@ -74,10 +85,16 @@ local function convert_scalar_type(avro_schema, opts)
7485
local avro_t = avro_type(avro_schema)
7586
if avro_t == 'int' then
7687
return types.int.nonNull
88+
elseif avro_t == 'int*' then
89+
return types.int
7790
elseif avro_t == 'long' then
7891
return types_long.nonNull
92+
elseif avro_t == 'long*' then
93+
return types_long
7994
elseif avro_t == 'string' then
8095
return types.string.nonNull
96+
elseif avro_t == 'string*' then
97+
return types.string
8198
end
8299
if raise then
83100
error('unrecognized avro-schema scalar type: ' ..
@@ -201,7 +218,9 @@ gql_type = function(state, avro_schema, collection, collection_name)
201218
assert(accessor.list_args ~= nil,
202219
'state.accessor.list_args must not be nil')
203220

204-
if avro_type(avro_schema) == 'record' then
221+
local avro_t = avro_type(avro_schema)
222+
223+
if avro_t == 'record' or avro_t == 'record*' then
205224
assert(type(avro_schema.name) == 'string',
206225
('avro_schema.name must be a string, got %s (avro_schema %s)')
207226
:format(type(avro_schema.name), json.encode(avro_schema)))
@@ -299,15 +318,14 @@ gql_type = function(state, avro_schema, collection, collection_name)
299318
}
300319
end
301320

302-
local res = types.nonNull(types.object({
321+
local res = types.object({
303322
name = collection ~= nil and collection.name or avro_schema.name,
304323
description = 'generated from avro-schema for ' ..
305324
avro_schema.name,
306325
fields = fields,
307-
}))
308-
309-
return res
310-
elseif avro_type(avro_schema) == 'enum' then
326+
})
327+
return avro_t == 'record' and types.nonNull(res) or res
328+
elseif avro_t == 'enum' then
311329
error('enums not implemented yet') -- XXX
312330
else
313331
local res = convert_scalar_type(avro_schema, {raise = false})

test/local/init_fail.test.lua

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ end
2222
-- -----------------------------------------------
2323

2424
-- init box and data schema
25+
box.cfg{background = false}
2526
testdata.init_spaces()
2627

2728
-- upload test data

test/local/space_compound_index.test.lua

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ end
2424
-- -----------------------------------------------
2525

2626
-- init box and data schema
27+
box.cfg{background = false}
2728
testdata.init_spaces()
2829

2930
-- upload test data

test/local/space_compound_index_testdata.lua

+7-4
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ function space_compound_index_testdata.init_spaces()
124124
local O_USER_STR_FN = 3
125125
local O_USER_NUM_FN = 4
126126

127-
box.cfg{background = false}
128127
box.once('test_space_init_spaces', function()
129128
-- users
130129
box.schema.create_space('user_collection')
@@ -160,12 +159,16 @@ function space_compound_index_testdata.fill_test_data()
160159
j % 5 == 0 and 'e' or
161160
nil
162161
assert(s ~= nil, 's must not be nil')
162+
local user_str = 'user_str_' .. s
163+
local user_num = i
163164
box.space.user_collection:replace(
164-
{'user_str_' .. s, i, 'first name ' .. s, 'last name ' .. s})
165+
{user_str, user_num, 'first name ' .. s, 'last name ' .. s})
165166
for k = 1, 10 do
167+
local order_str = 'order_str_' .. s .. '_' .. tostring(k)
168+
local order_num = i * 100 + k
166169
box.space.order_collection:replace(
167-
{'order_str_' .. s .. '_' .. tostring(k), i * 100 + k,
168-
'user_str_' .. s, i, 'description ' .. s})
170+
{order_str, order_num, user_str, user_num,
171+
'description ' .. s})
169172
end
170173
end
171174
end
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
RESULT: ok: false; err: Syntax error near line 2
2+
RESULT
3+
---
4+
bar:
5+
- id_or_null_1: '1'
6+
id_or_null_3: '1'
7+
id_or_null_2: '1'
8+
id: '1'
9+
- id_or_null_1: '10'
10+
id_or_null_3: '10'
11+
id_or_null_2: '10'
12+
id: '10'
13+
- id_or_null_1: '100'
14+
id_or_null_3: '100'
15+
id_or_null_2: '100'
16+
id: '100'
17+
- id_or_null_3: '101'
18+
id_or_null_2: '101'
19+
id: '101'
20+
- id_or_null_3: '102'
21+
id_or_null_2: '102'
22+
id: '102'
23+
- id_or_null_1: '103'
24+
id_or_null_3: '103'
25+
id: '103'
26+
- id_or_null_1: '104'
27+
id_or_null_3: '104'
28+
id: '104'
29+
- id_or_null_1: '105'
30+
id_or_null_2: '105'
31+
id: '105'
32+
- id_or_null_1: '106'
33+
id_or_null_2: '106'
34+
id: '106'
35+
- id_or_null_3: '107'
36+
id: '107'
37+
- id_or_null_3: '108'
38+
id: '108'
39+
- id_or_null_2: '109'
40+
id: '109'
41+
- id_or_null_1: '11'
42+
id_or_null_3: '11'
43+
id_or_null_2: '11'
44+
id: '11'
45+
- id_or_null_2: '110'
46+
id: '110'
47+
- id_or_null_1: '111'
48+
id: '111'
49+
- id_or_null_1: '112'
50+
id: '112'
51+
- id: '113'
52+
- id: '114'
53+
- id_or_null_1: '12'
54+
id_or_null_3: '12'
55+
id_or_null_2: '12'
56+
id: '12'
57+
- id_or_null_1: '13'
58+
id_or_null_3: '13'
59+
id_or_null_2: '13'
60+
id: '13'
61+
...
62+
63+
RESULT
64+
---
65+
bar:
66+
- id_or_null_1: '42'
67+
id_or_null_3: '42'
68+
id_or_null_2: '42'
69+
id: '42'
70+
...
71+
72+
RESULT
73+
---
74+
bar:
75+
- id_or_null_1: '42'
76+
id_or_null_3: '42'
77+
id_or_null_2: '42'
78+
id: '42'
79+
...
80+
81+
RESULT
82+
---
83+
foo:
84+
- bar_partial_unique:
85+
- id: '42'
86+
bar_partial_non_unique:
87+
- id: '42'
88+
id: '42'
89+
...
90+
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env tarantool
2+
3+
-- ----------------------------------------------------------
4+
-- Motivation: https://github.com/tarantool/graphql/issues/43
5+
-- ----------------------------------------------------------
6+
7+
local fio = require('fio')
8+
9+
-- require in-repo version of graphql/ sources despite current working directory
10+
package.path = fio.abspath(debug.getinfo(1).source:match("@?(.*/)")
11+
:gsub('/./', '/'):gsub('/+$', '')) .. '/../../?.lua' .. ';' ..
12+
package.path
13+
14+
local graphql = require('graphql')
15+
local testdata = require('test.testdata.nullable_index_testdata')
16+
17+
-- init box, upload test data and acquire metadata
18+
-- -----------------------------------------------
19+
20+
-- init box and data schema
21+
box.cfg{background = false}
22+
testdata.init_spaces()
23+
24+
-- upload test data
25+
testdata.fill_test_data()
26+
27+
-- acquire metadata
28+
local metadata = testdata.get_test_metadata()
29+
local schemas = metadata.schemas
30+
local collections = metadata.collections
31+
local service_fields = metadata.service_fields
32+
local indexes = metadata.indexes
33+
34+
-- build accessor and graphql schemas
35+
-- ----------------------------------
36+
37+
local accessor = graphql.accessor_space.new({
38+
schemas = schemas,
39+
collections = collections,
40+
service_fields = service_fields,
41+
indexes = indexes,
42+
})
43+
44+
local gql_wrapper = graphql.new({
45+
schemas = schemas,
46+
collections = collections,
47+
accessor = accessor,
48+
})
49+
50+
testdata.run_queries(gql_wrapper)
51+
52+
-- clean up
53+
-- --------
54+
55+
testdata.drop_spaces()
56+
57+
os.exit()

0 commit comments

Comments
 (0)