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

Commit 312b47c

Browse files
committed
Add creation of avro-schema from query with directives
Close #85
1 parent 77921fc commit 312b47c

File tree

2 files changed

+577
-0
lines changed

2 files changed

+577
-0
lines changed

graphql/query_to_avro.lua

+40
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,46 @@ local function field_to_avro(object_type, fields, context)
200200

201201
local fieldTypeAvro = gql_type_to_avro(fieldType.kind, subSelections,
202202
context)
203+
-- Currently we support only 'include' and 'skip' directives. Both of them
204+
-- affect resulting avro-schema the same way: field with directive becomes
205+
-- nullable, if it's already not. Nullable field does not change.
206+
--
207+
-- If it is a 1:N connection then it's 'array' field becomes 'array*'.
208+
-- If it is avro-schema union, then 'null' will be added to the union
209+
-- types. If there are more then one directive on a field then all works
210+
-- the same way, like it is only one directive. (But we still check all
211+
-- directives to be 'include' or 'skip').
212+
if firstField.directives ~= nil then
213+
for _, d in ipairs(firstField.directives) do
214+
check(d.name, "directive.name", "table")
215+
check(d.arguments, "directive.arguments", "table")
216+
check(d.kind, "directive.kind", "string")
217+
assert(d.kind == "directive")
218+
check(d.name.value, "directive.name.value", "string")
219+
assert(d.name.value == "include" or d.name.value == "skip",
220+
"Only 'include' and 'skip' directives are supported for now")
221+
end
222+
if type(fieldTypeAvro) == "string" then
223+
if string.sub(fieldTypeAvro, -1) ~= '*' then
224+
fieldTypeAvro = fieldTypeAvro .. '*'
225+
end
226+
end
227+
if type(fieldTypeAvro) == "table" then
228+
if utils.is_array(fieldTypeAvro) then
229+
-- Union case.
230+
if not utils.value_in("null", fieldTypeAvro) then
231+
table.insert(fieldTypeAvro, "null")
232+
end
233+
else
234+
-- Record case.
235+
check(fieldTypeAvro.type, "fieldTypeAvro.type", "string")
236+
if string.sub(fieldTypeAvro.type, -1) ~= '*' then
237+
fieldTypeAvro.type = fieldTypeAvro.type .. '*'
238+
end
239+
end
240+
end
241+
end
242+
203243
return {
204244
name = convert_schema_helpers.base_name(fieldName),
205245
type = fieldTypeAvro,

0 commit comments

Comments
 (0)