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

Commit 135db04

Browse files
committed
Add creation of avro-schema from query with directives
Close #85
1 parent ad92a4d commit 135db04

File tree

2 files changed

+594
-0
lines changed

2 files changed

+594
-0
lines changed

graphql/query_to_avro.lua

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

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

0 commit comments

Comments
 (0)