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

Commit fd9cb90

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 689df3a commit fd9cb90

File tree

5 files changed

+406
-1
lines changed

5 files changed

+406
-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

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
-- use long types, to avoid overflow problems
15+
local gql_scalar_to_avro_index = {
16+
String = "string",
17+
Int = "long",
18+
Long = "long",
19+
Float = "double",
20+
Boolean = "boolean"
21+
}
22+
local function gql_scalar_to_avro(fieldType)
23+
assert(fieldType.__type == "Scalar", "GraphQL scalar field expected")
24+
local result = gql_scalar_to_avro_index[fieldType.name]
25+
assert(result ~= nil, "Unexpected scalar type: " .. fieldType.name)
26+
return result
27+
end
28+
29+
local object_to_avro
30+
31+
local function complete_field_to_avro(fieldType, result, subSelections, context)
32+
local fieldTypeName = fieldType.__type
33+
34+
if fieldTypeName == 'List' then
35+
local innerType = fieldType.ofType
36+
local items = complete_field_to_avro(innerType, {}, subSelections,
37+
context).type
38+
result.type = {
39+
type = "array",
40+
items = items
41+
}
42+
return result
43+
end
44+
45+
if fieldTypeName == 'Object' then
46+
result.type = object_to_avro(fieldType, subSelections, context)
47+
return result
48+
elseif fieldTypeName == 'Interface' or fieldTypeName == 'Union' then
49+
error('Interfases and Utions are not supported yet')
50+
end
51+
error(string.format('Unknown type "%s"', fieldTypeName))
52+
end
53+
54+
--- The function converts a single Object field to avro format
55+
local function field_to_avro(object_type, fields, context)
56+
local firstField = fields[1]
57+
local fieldName = firstField.name.value
58+
local fieldType = introspection.fieldMap[fieldName] or
59+
object_type.fields[fieldName]
60+
assert(fieldType ~= nil)
61+
local subSelections = query_util.mergeSelectionSets(fields)
62+
local innerType = fieldType.kind
63+
local fieldTypeName = innerType.__type
64+
local nullable = true
65+
-- In case the field is NonNull, the real type is in ofType attibute.
66+
if fieldTypeName == 'NonNull' then
67+
innerType = innerType.ofType
68+
nullable = false
69+
end
70+
local result = {}
71+
result.name = fieldName
72+
if fieldType.resolve ~= nil then
73+
-- not scalar
74+
return complete_field_to_avro(innerType, result, subSelections, context)
75+
end
76+
-- scalar type
77+
result.type = gql_scalar_to_avro(innerType)
78+
result.type = nullable and result.type .. "*" or result.type
79+
return result
80+
end
81+
82+
--- Convert GraphQL object to avro record.
83+
---
84+
--- @tparam table object_type GraphQL type object to be converted to Avro schema
85+
---
86+
--- @tparam table selections GraphQL representations of fields which should be
87+
--- in the output of the query
88+
---
89+
--- @tparam table context additional information for Avro schema generation; one
90+
--- of the fields is `namespace_parts` -- table of names of records from the
91+
--- root to the current object
92+
---
93+
--- @treturn table corresponding Avro schema
94+
object_to_avro = function(object_type, selections, context)
95+
local groupedFieldSet = query_util.collectFields(object_type, selections,
96+
{}, {}, context)
97+
local result = {
98+
type = 'record',
99+
name = object_type.name,
100+
fields = {}
101+
}
102+
if #context.namespace_parts ~= 0 then
103+
result.namespace = table.concat(context.namespace_parts, ".")
104+
end
105+
table.insert(context.namespace_parts, result.name)
106+
for _, fields in pairs(groupedFieldSet) do
107+
local avro_field = field_to_avro(object_type, fields, context)
108+
table.insert(result.fields, avro_field)
109+
end
110+
context.namespace_parts[#context.namespace_parts] = nil
111+
return result
112+
end
113+
114+
--- Create an Avro schema for a given query.
115+
---
116+
--- @tparam table query object which avro schema should be created for
117+
---
118+
--- @treturn table `avro_schema` avro schema for any `query:execute()` result.
119+
function query_to_avro.convert(query)
120+
local state = query.state
121+
local context = query_util.buildContext(state.schema, query.ast, {}, {},
122+
query.operation_name)
123+
-- The variable is necessary to avoid fullname interferention.
124+
-- Each nested Avro record creates it's namespace.
125+
context.namespace_parts = {}
126+
local rootType = state.schema[context.operation.operation]
127+
local selections = context.operation.selectionSet.selections
128+
return object_to_avro(rootType, selections, context)
129+
end
130+
131+
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

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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

0 commit comments

Comments
 (0)