@@ -207,6 +207,46 @@ local function field_to_avro(object_type, fields, context)
207
207
208
208
local fieldTypeAvro = gql_type_to_avro (fieldType .kind , subSelections ,
209
209
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
+
210
250
return {
211
251
name = convert_schema_helpers .base_name (fieldName ),
212
252
type = fieldTypeAvro ,
0 commit comments