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

Commit 9d6cf22

Browse files
committed
wip
1 parent 30c64ab commit 9d6cf22

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

test/local/avro_union.test.lua

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
box.cfg{background = false}
10+
local json = require('json')
11+
local yaml = require('yaml')
12+
local graphql = require('graphql')
13+
local utils = require('graphql.utils')
14+
15+
local schemas = json.decode([[{
16+
"user_collection": {
17+
"name": "user_collection",
18+
"type": "record",
19+
"fields": [
20+
{ "name": "user_id", "type": "string" },
21+
{ "name": "name", "type": "string" },
22+
{ "name": "stuff", "type": [
23+
"null",
24+
"string",
25+
{"name": "balances",
26+
"type": {"type": "array", "items": "string" }
27+
},
28+
{ "type": "map", "values": "string" }
29+
]
30+
}
31+
]
32+
}
33+
}]])
34+
35+
local collections = json.decode([[{
36+
"user_collection": {
37+
"schema_name": "user_collection",
38+
"connections": []
39+
}
40+
}]])
41+
42+
local service_fields = {
43+
user_collection = {
44+
{name = 'expires_on', type = 'long', default = 0}
45+
}
46+
}
47+
48+
local indexes = {
49+
user_collection = {
50+
user_id_index = {
51+
service_fields = {},
52+
fields = {'user_id'},
53+
index_type = 'tree',
54+
unique = true,
55+
primary = true,
56+
}
57+
}
58+
}
59+
60+
local USER_ID = 2
61+
62+
box.schema.create_space('user_collection')
63+
box.space.user_collection:create_index('user_id_index',
64+
{type = 'tree', unique = true, parts = { USER_ID, 'string' }}
65+
)
66+
67+
box.space.user_collection:replace(
68+
{1827767717, 'user_id_1', 'Ivan', 'Stuffy string'})
69+
70+
box.space.user_collection:replace(
71+
{1827767717, 'user_id_2', 'Bob', {'Union stuff str1', 'Union stuff str2'}})
72+
73+
box.space.user_collection:replace(
74+
{1827767717, 'user_id_3', 'Alice'})
75+
76+
local accessor = graphql.accessor_space.new({
77+
schemas = schemas,
78+
collections = collections,
79+
service_fields = service_fields,
80+
indexes = indexes,
81+
})
82+
83+
local gql_wrapper = graphql.new({
84+
schemas = schemas,
85+
collections = collections,
86+
accessor = accessor,
87+
})
88+
89+
--- note that inside mapBox there is an auto-generated name for
90+
--- avro-schema no-named map (it is necessary as we can not
91+
--- access no-named fields in GQL)
92+
local query_1 = [[
93+
query user_collection {
94+
user_collection {
95+
user_id
96+
name
97+
stuff {
98+
... on stringBox {
99+
string
100+
}
101+
... on balancesBox {
102+
balances
103+
}
104+
... on mapBox {
105+
map
106+
}
107+
}
108+
}
109+
}
110+
]]
111+
112+
utils.show_trace(function()
113+
114+
--- extract GQL schema from gql_wrapper.state.nullable_collection_types
115+
--- and compare them with expected GQL schema
116+
local variables_1 = { }
117+
local gql_query_1 = gql_wrapper:compile(query_1)
118+
local result = gql_query_1:execute(variables_1)
119+
--- Validate result with initial avro-schema
120+
print(('RESULT\n%s'):format(yaml.encode(result)))
121+
end)
122+
123+
box.space.user_collection:drop()

0 commit comments

Comments
 (0)