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

Commit 31897d6

Browse files
committed
add simple test for array avro type usage
1 parent 24ec6c4 commit 31897d6

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

test/local/array_and_map.result

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
RESULT
2+
---
3+
user_collection:
4+
- user_id: def
5+
favorite_food:
6+
- meat
7+
- potato
8+
...
9+
10+
RESULT
11+
---
12+
organization_collection:
13+
- organization_id: def
14+
organization_events:
15+
holiday: nice holiday
16+
new_year: hey hou
17+
...
18+

test/local/array_and_map.test.lua

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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' .. ';' ..
8+
package.path
9+
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": {
17+
"name": "user",
18+
"type": "record",
19+
"fields": [
20+
{ "name": "user_id", "type": "string" },
21+
{ "name": "favorite_food", "type": {"type": "array", "items": "string"} }
22+
]
23+
},
24+
"organization": {
25+
"name": "organization",
26+
"type": "record",
27+
"fields": [
28+
{ "name": "organization_id", "type": "string" },
29+
{ "name": "organization_events", "type": {"type": "map", "values": "string"} }
30+
]
31+
}
32+
}]])
33+
34+
local collections = json.decode([[{
35+
"user_collection": {
36+
"schema_name": "user"
37+
},
38+
"organization_collection": {
39+
"schema_name": "organization"
40+
}
41+
}]])
42+
43+
local function access_function(parent, collection_name, filter, args)
44+
--[[
45+
print('DEBUG: collection_name: ' .. collection_name)
46+
print('DEBUG: filter: ' .. json.encode(filter))
47+
print('DEBUG: args: ' .. json.encode(args))
48+
print('DEBUG: --------')
49+
--]]
50+
local obj
51+
if collection_name == 'user_collection' then
52+
obj = {
53+
user_id = 'def',
54+
favorite_food = { 'meat', 'potato' },
55+
}
56+
elseif collection_name == 'organization_collection' then
57+
obj = {
58+
organization_id = 'def',
59+
organization_events = { holiday = 'nice holiday',
60+
new_year = 'hey hou' },
61+
}
62+
else
63+
error('NIY: ' .. collection_name)
64+
end
65+
if not utils.is_subtable(obj, filter) then
66+
return {}
67+
end
68+
return { obj }
69+
end
70+
71+
local accessor = setmetatable({}, {
72+
__index = {
73+
select = function(self, parent, collection_name, connection_name,
74+
filter, args)
75+
return access_function(parent, collection_name, filter, args)
76+
end,
77+
list_args = function(self, connection_type)
78+
if connection_type == '1:1' then
79+
return {}
80+
end
81+
return {
82+
{ name = 'limit', type = 'int' },
83+
{ name = 'offset', type = 'long' },
84+
-- {name = 'filter', type = ...},
85+
}
86+
end,
87+
}
88+
})
89+
90+
local gql_wrapper = graphql.new({
91+
-- class_name:class mapping
92+
schemas = schemas,
93+
-- collection_{schema_name=..., connections=...} mapping
94+
collections = collections,
95+
-- :select() and :list_args() provider
96+
accessor = accessor,
97+
})
98+
99+
local query_with_list = [[
100+
query userFavs($user_id: String) {
101+
user_collection(user_id: $user_id) {
102+
user_id
103+
favorite_food
104+
}
105+
}
106+
]]
107+
108+
109+
local query_with_map = [[
110+
query obtainOrganizationEvents($organization_id: String) {
111+
organization_collection(organization_id: $organization_id) {
112+
organization_id,
113+
organization_events
114+
}
115+
}
116+
]]
117+
118+
119+
utils.show_trace(function()
120+
local variables_2 = { user_id = 'def' }
121+
local gql_query_2 = gql_wrapper:compile(query_with_list)
122+
local result = gql_query_2:execute(variables_2)
123+
print(('RESULT\n%s'):format(yaml.encode(result)))
124+
end)
125+
126+
utils.show_trace(function()
127+
local variables_1 = {organization_id = 'def'}
128+
local gql_query_1 = gql_wrapper:compile(query_with_map)
129+
local result = gql_query_1:execute(variables_1)
130+
print(('RESULT\n%s'):format(yaml.encode(result)))
131+
end)

0 commit comments

Comments
 (0)