|
| 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 | + assert(avro.type:endswith("*") == false, |
| 46 | + "Avro type should not be nullable already") |
| 47 | + avro.type = avro.type .. '*' |
| 48 | + return avro |
| 49 | + end |
| 50 | + if type_type == "table" then |
| 51 | + avro.type = make_avro_type_nullable(avro.type) |
| 52 | + return avro |
| 53 | + end |
| 54 | + error("Avro type should be a string or table, got :" .. type_type) |
| 55 | +end |
| 56 | + |
| 57 | +local object_to_avro |
| 58 | + |
| 59 | +local function complete_field_to_avro(fieldType, result, subSelections, context, |
| 60 | + NonNull) |
| 61 | + local fieldTypeName = fieldType.__type |
| 62 | + if fieldTypeName == 'NonNull' then |
| 63 | + -- In case the field is NonNull, the real type is in ofType attribute. |
| 64 | + fieldType = fieldType.ofType |
| 65 | + fieldTypeName = fieldType.__type |
| 66 | + elseif NonNull ~= true then |
| 67 | + -- Call complete_field second time and make result nullable. |
| 68 | + result = complete_field_to_avro(fieldType, result, subSelections, |
| 69 | + context, true) |
| 70 | + result = make_avro_type_nullable(result) |
| 71 | + return result |
| 72 | + end |
| 73 | + |
| 74 | + if fieldTypeName == 'List' then |
| 75 | + local innerType = fieldType.ofType |
| 76 | + -- Steal type from virtual object. |
| 77 | + -- This is necessary because in case of arrays type should be |
| 78 | + -- "completed" into results `items` field, but in other cases (Object, |
| 79 | + -- Scalar) it should be completed into `type` field. |
| 80 | + local items = complete_field_to_avro(innerType, {}, subSelections, |
| 81 | + context).type |
| 82 | + result.type = { |
| 83 | + type = "array", |
| 84 | + items = items |
| 85 | + } |
| 86 | + return result |
| 87 | + end |
| 88 | + |
| 89 | + if fieldTypeName == 'Scalar' then |
| 90 | + result.type = gql_scalar_to_avro(fieldType) |
| 91 | + return result |
| 92 | + end |
| 93 | + |
| 94 | + if fieldTypeName == 'Object' then |
| 95 | + result.type = object_to_avro(fieldType, subSelections, context) |
| 96 | + return result |
| 97 | + elseif fieldTypeName == 'Interface' or fieldTypeName == 'Union' then |
| 98 | + error('Interfaces and Unions are not supported yet') |
| 99 | + end |
| 100 | + error(string.format('Unknown type "%s"', fieldTypeName)) |
| 101 | +end |
| 102 | + |
| 103 | +--- The function converts a single Object field to avro format |
| 104 | +local function field_to_avro(object_type, fields, context) |
| 105 | + local firstField = fields[1] |
| 106 | + assert(#fields == 1, "The aliases are not considered yet") |
| 107 | + local fieldName = firstField.name.value |
| 108 | + local fieldType = introspection.fieldMap[fieldName] or |
| 109 | + object_type.fields[fieldName] |
| 110 | + assert(fieldType ~= nil) |
| 111 | + local subSelections = query_util.mergeSelectionSets(fields) |
| 112 | + local result = {} |
| 113 | + result.name = fieldName |
| 114 | + result = complete_field_to_avro(fieldType.kind, result, subSelections, |
| 115 | + context) |
| 116 | + return result |
| 117 | +end |
| 118 | + |
| 119 | +--- Convert GraphQL object to avro record. |
| 120 | +--- |
| 121 | +--- @tparam table object_type GraphQL type object to be converted to Avro schema |
| 122 | +--- |
| 123 | +--- @tparam table selections GraphQL representations of fields which should be |
| 124 | +--- in the output of the query |
| 125 | +--- |
| 126 | +--- @tparam table context additional information for Avro schema generation; one |
| 127 | +--- of the fields is `namespace_parts` -- table of names of records from the |
| 128 | +--- root to the current object |
| 129 | +--- |
| 130 | +--- @treturn table corresponding Avro schema |
| 131 | +object_to_avro = function(object_type, selections, context) |
| 132 | + local groupedFieldSet = query_util.collectFields(object_type, selections, |
| 133 | + {}, {}, context) |
| 134 | + local result = { |
| 135 | + type = 'record', |
| 136 | + name = object_type.name, |
| 137 | + fields = {} |
| 138 | + } |
| 139 | + if #context.namespace_parts ~= 0 then |
| 140 | + result.namespace = table.concat(context.namespace_parts, ".") |
| 141 | + end |
| 142 | + table.insert(context.namespace_parts, result.name) |
| 143 | + for _, fields in pairs(groupedFieldSet) do |
| 144 | + local avro_field = field_to_avro(object_type, fields, context) |
| 145 | + table.insert(result.fields, avro_field) |
| 146 | + end |
| 147 | + context.namespace_parts[#context.namespace_parts] = nil |
| 148 | + return result |
| 149 | +end |
| 150 | + |
| 151 | +--- Create an Avro schema for a given query. |
| 152 | +--- |
| 153 | +--- @tparam table query object which avro schema should be created for |
| 154 | +--- |
| 155 | +--- @treturn table `avro_schema` avro schema for any `query:execute()` result. |
| 156 | +function query_to_avro.convert(query) |
| 157 | + assert(type(query) == "table", |
| 158 | + 'query should be a table, got: ' .. type(table) |
| 159 | + .. '; hint: use ":" instead of "."') |
| 160 | + local state = query.state |
| 161 | + local context = query_util.buildContext(state.schema, query.ast, {}, {}, |
| 162 | + query.operation_name) |
| 163 | + -- The variable is necessary to avoid fullname interferention. |
| 164 | + -- Each nested Avro record creates it's namespace. |
| 165 | + context.namespace_parts = {} |
| 166 | + local rootType = state.schema[context.operation.operation] |
| 167 | + local selections = context.operation.selectionSet.selections |
| 168 | + return object_to_avro(rootType, selections, context) |
| 169 | +end |
| 170 | + |
| 171 | +return query_to_avro |
0 commit comments