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

Commit 1fc559b

Browse files
committed
Feature: convert graphql query to avro schema
Extend query object: add `avro_schema` method, which produces Avro schema which can be used to verify or flatten any `query_exequte` result. Closes #7
1 parent 88792a6 commit 1fc559b

8 files changed

+766
-1
lines changed

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ default:
33

44
.PHONY: lint
55
lint:
6-
luacheck graphql/*.lua test/local/*.lua test/testdata/*.lua \
6+
luacheck graphql/*.lua \
7+
test/local/*.lua \
8+
test/testdata/*.lua \
79
test/common/*.test.lua test/common/lua/*.lua \
10+
test/extra/*.test.lua \
811
--no-redefined --no-unused-args
912

1013
.PHONY: test

graphql/query_to_avro.lua

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
--- Module for convertion GraphQL query to Avro schema.
2+
---
3+
--- Random notes:
4+
---
5+
--- * The best way to use this module is to just call `avro_schema` methon on
6+
--- compiled query object.
7+
local path = "graphql.core"
8+
local introspection = require(path .. '.introspection')
9+
local query_util = require(path .. '.query_util')
10+
11+
-- module functions
12+
local query_to_avro = {}
13+
14+
local gql_scalar_to_avro_index = {
15+
String = "string",
16+
Int = "int",
17+
Long = "long",
18+
-- GraphQL Float is double precision according to graphql.org.
19+
-- More info http://graphql.org/learn/schema/#scalar-types
20+
Float = "double",
21+
Boolean = "boolean"
22+
}
23+
local function gql_scalar_to_avro(fieldType)
24+
assert(fieldType.__type == "Scalar", "GraphQL scalar field expected")
25+
assert(fieldType.name ~= "Map", "Map type is not supported")
26+
local result = gql_scalar_to_avro_index[fieldType.name]
27+
assert(result ~= nil, "Unexpected scalar type: " .. fieldType.name)
28+
return result
29+
end
30+
31+
-- The function converts avro type to nullable.
32+
-- In current tarantool/avro-schema implementation we simply add '*'
33+
-- to the end of type name.
34+
-- The function do not copy the resulting type but changes it in place.
35+
--
36+
-- @tparam table avro schema node to be converted to nullable
37+
--
38+
-- @tresult table schema node; basically it is the passed schema node,
39+
-- however in nullable type implementation through unions it can be different
40+
-- node
41+
local function make_avro_type_nullable(avro)
42+
assert(avro.type ~= nil, "Avro `type` field is necessary")
43+
local type_type = type(avro.type)
44+
if type_type == "string" then
45+
avro.type = avro.type .. '*'
46+
return avro
47+
end
48+
if type_type == "table" then
49+
avro.type = make_avro_type_nullable(avro.type)
50+
return avro
51+
end
52+
assert(false, "Avro type should be a string or table")
53+
end
54+
55+
local object_to_avro
56+
57+
local function complete_field_to_avro(fieldType, result, subSelections, context,
58+
NonNull)
59+
local fieldTypeName = fieldType.__type
60+
if fieldTypeName == 'NonNull' then
61+
-- In case the field is NonNull, the real type is in ofType attibute.
62+
fieldType = fieldType.ofType
63+
fieldTypeName = fieldType.__type
64+
elseif NonNull ~= true then
65+
-- Call complete_field second time and make result nullable.
66+
result = complete_field_to_avro(fieldType, result, subSelections,
67+
context, true)
68+
result = make_avro_type_nullable(result)
69+
return result
70+
end
71+
72+
73+
if fieldTypeName == 'List' then
74+
local innerType = fieldType.ofType
75+
-- Steal type from virtual object.
76+
-- This is necessary because in case of arrays type should be
77+
-- "completed" into results `items` field, but in case of 1:1 relations
78+
-- it should be completed into `type` field.
79+
local items = complete_field_to_avro(innerType, {}, subSelections,
80+
context).type
81+
result.type = {
82+
type = "array",
83+
items = items
84+
}
85+
return result
86+
end
87+
88+
if fieldTypeName == 'Scalar' then
89+
result.type = gql_scalar_to_avro(fieldType)
90+
return result
91+
end
92+
93+
if fieldTypeName == 'Object' then
94+
result.type = object_to_avro(fieldType, subSelections, context)
95+
return result
96+
elseif fieldTypeName == 'Interface' or fieldTypeName == 'Union' then
97+
error('Interfases and Utions are not supported yet')
98+
end
99+
error(string.format('Unknown type "%s"', fieldTypeName))
100+
end
101+
102+
--- The function converts a single Object field to avro format
103+
local function field_to_avro(object_type, fields, context)
104+
local firstField = fields[1]
105+
assert(#fields == 1, "The aliases are not considered yet")
106+
local fieldName = firstField.name.value
107+
local fieldType = introspection.fieldMap[fieldName] or
108+
object_type.fields[fieldName]
109+
assert(fieldType ~= nil)
110+
local subSelections = query_util.mergeSelectionSets(fields)
111+
local result = {}
112+
result.name = fieldName
113+
result = complete_field_to_avro(fieldType.kind, result, subSelections,
114+
context)
115+
return result
116+
end
117+
118+
--- Convert GraphQL object to avro record.
119+
---
120+
--- @tparam table object_type GraphQL type object to be converted to Avro schema
121+
---
122+
--- @tparam table selections GraphQL representations of fields which should be
123+
--- in the output of the query
124+
---
125+
--- @tparam table context additional information for Avro schema generation; one
126+
--- of the fields is `namespace_parts` -- table of names of records from the
127+
--- root to the current object
128+
---
129+
--- @treturn table corresponding Avro schema
130+
object_to_avro = function(object_type, selections, context, nullable)
131+
local groupedFieldSet = query_util.collectFields(object_type, selections,
132+
{}, {}, context)
133+
local result = {
134+
type = 'record',
135+
name = object_type.name,
136+
fields = {}
137+
}
138+
if #context.namespace_parts ~= 0 then
139+
result.namespace = table.concat(context.namespace_parts, ".")
140+
end
141+
table.insert(context.namespace_parts, result.name)
142+
for _, fields in pairs(groupedFieldSet) do
143+
local avro_field = field_to_avro(object_type, fields, context)
144+
table.insert(result.fields, avro_field)
145+
end
146+
context.namespace_parts[#context.namespace_parts] = nil
147+
return result
148+
end
149+
150+
--- Create an Avro schema for a given query.
151+
---
152+
--- @tparam table query object which avro schema should be created for
153+
---
154+
--- @treturn table `avro_schema` avro schema for any `query:execute()` result.
155+
function query_to_avro.convert(query)
156+
local state = query.state
157+
local context = query_util.buildContext(state.schema, query.ast, {}, {},
158+
query.operation_name)
159+
-- The variable is necessary to avoid fullname interferention.
160+
-- Each nested Avro record creates it's namespace.
161+
context.namespace_parts = {}
162+
local rootType = state.schema[context.operation.operation]
163+
local selections = context.operation.selectionSet.selections
164+
return object_to_avro(rootType, selections, context, false)
165+
end
166+
167+
return query_to_avro

graphql/tarantool_graphql.lua

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ local schema = require('graphql.core.schema')
1414
local types = require('graphql.core.types')
1515
local validate = require('graphql.core.validate')
1616
local execute = require('graphql.core.execute')
17+
local query_to_avro = require('graphql.query_to_avro')
1718

1819
local utils = require('graphql.utils')
1920

@@ -595,6 +596,7 @@ local function gql_compile(state, query)
595596
local gql_query = setmetatable(qstate, {
596597
__index = {
597598
execute = gql_execute,
599+
avro_schema = query_to_avro.convert
598600
}
599601
})
600602
return gql_query

test/extra/suite.ini

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[default]
2+
core = app
3+
description = tests on features which are not related to specific executor
4+
lua_libs = ../common/lua/test_data_user_order.lua ../testdata/array_and_map_testdata.lua \
5+
../testdata/nullable_index_testdata.lua

test/extra/to_avro_arrays.test.lua

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/usr/bin/env tarantool
2+
local fio = require('fio')
3+
local yaml = require('yaml')
4+
local avro = require('avro_schema')
5+
local testdata = require('array_and_map_testdata')
6+
local test = require('tap').test('to avro schema')
7+
-- require in-repo version of graphql/ sources despite current working directory
8+
package.path = fio.abspath(debug.getinfo(1).source:match("@?(.*/)")
9+
:gsub('/./', '/'):gsub('/+$', '')) .. '/../../?.lua' .. ';' ..
10+
package.path
11+
12+
local graphql = require('graphql')
13+
14+
box.cfg{wal_mode="none"}
15+
test:plan(4)
16+
17+
testdata.init_spaces()
18+
testdata.fill_test_data()
19+
local meta = testdata.get_test_metadata()
20+
21+
local accessor = graphql.accessor_space.new({
22+
schemas = meta.schemas,
23+
collections = meta.collections,
24+
service_fields = meta.service_fields,
25+
indexes = meta.indexes,
26+
})
27+
28+
local gql_wrapper = graphql.new({
29+
schemas = meta.schemas,
30+
collections = meta.collections,
31+
accessor = accessor,
32+
})
33+
34+
-- We do not select `customer_balances` and `favorite_holidays` because thay are
35+
-- is of `Map` type, which is not supported.
36+
local query = [[
37+
query user_holidays($user_id: String) {
38+
user_collection(user_id: $user_id) {
39+
user_id
40+
favorite_food
41+
user_balances {
42+
value
43+
}
44+
}
45+
}
46+
]]
47+
local expected_avro_schema = [[
48+
type: record
49+
name: Query
50+
fields:
51+
- name: user_collection
52+
type:
53+
type: array
54+
items:
55+
type: record
56+
fields:
57+
- name: user_id
58+
type: string
59+
- name: user_balances
60+
type:
61+
type: array
62+
items:
63+
type: record
64+
fields:
65+
- name: value
66+
type: int
67+
name: balance
68+
namespace: Query.user_collection
69+
- name: favorite_food
70+
type:
71+
type: array
72+
items: string
73+
name: user_collection
74+
namespace: Query
75+
]]
76+
expected_avro_schema = yaml.decode(expected_avro_schema)
77+
local gql_query = gql_wrapper:compile(query)
78+
local variables = {
79+
user_id = 'user_id_1',
80+
}
81+
82+
local avros = gql_query:avro_schema()
83+
84+
test:is_deeply(avros, expected_avro_schema, "generated avro schema")
85+
local result_expected = [[
86+
user_collection:
87+
- user_id: user_id_1
88+
user_balances:
89+
- value: 33
90+
- value: 44
91+
favorite_food:
92+
- meat
93+
- potato
94+
]]
95+
result_expected = yaml.decode(result_expected)
96+
local result = gql_query:execute(variables)
97+
test:is_deeply(result, result_expected, 'graphql qury exec result')
98+
local ok, ash, r, fs, _
99+
ok, ash = avro.create(avros)
100+
assert(ok)
101+
ok, _ = avro.validate(ash, result)
102+
assert(ok)
103+
test:is(ok, true, 'gql result validation by avro')
104+
ok, fs = avro.compile(ash)
105+
assert(ok)
106+
ok, r = fs.flatten(result)
107+
assert(ok)
108+
ok, r = fs.unflatten(r)
109+
assert(ok)
110+
test:is_deeply(r, result_expected, 'res = unflatten(flatten(res))')
111+
112+
os.exit(test:check() == true and 0 or 1)

0 commit comments

Comments
 (0)