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

Commit 3385e5a

Browse files
committed
Add creation of avro-schema from query with
Maps Close #198
1 parent a344822 commit 3385e5a

File tree

5 files changed

+245
-217
lines changed

5 files changed

+245
-217
lines changed

graphql/convert_schema/types.lua

+9-3
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,16 @@ function types.convert(state, avro_schema, opts)
448448
'got %s (avro_schema %s)'):format(type(avro_schema.values),
449449
json.encode(avro_schema)))
450450

451-
-- validate avro schema format inside 'values'
452-
types.convert(state, avro_schema.values, {context = context})
451+
local converted_values = types.convert(state, avro_schema.values,
452+
{context = context})
453+
454+
local true_values_type = converted_values
455+
while true_values_type.__type == 'NonNull' do
456+
true_values_type = true_values_type.ofType
457+
end
458+
local map_name = true_values_type.name .. "_Map"
453459

454-
local res = core_types.map
460+
local res = core_types.map({values = converted_values, name = map_name })
455461
return avro_t == 'map' and core_types.nonNull(res) or res
456462
elseif avro_t == 'union' then
457463
return union.convert(avro_schema, {

graphql/convert_schema/union.lua

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ local function box_type(type_to_box, box_field_name, opts)
3838
-- for the argument type to avoid 'Encountered multiple types' error. See
3939
-- also the comment in @{types.box_collection_type}.
4040
local box_name = (gql_true_type.name or gql_true_type.__type) .. '_box'
41+
if (gql_true_type.__type == "Scalar" and gql_true_type.subtype == 'Map') then
42+
box_name = 'Map_box'
43+
end
4144
if gen_argument then
4245
box_name = helpers.full_name(box_name, context)
4346
else

graphql/core/types.lua

+18-11
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,24 @@ function types.union(config)
191191
return instance
192192
end
193193

194-
types.map = types.scalar({
195-
name = 'Map',
196-
description = 'Map is a dictionary with string keys and values of ' ..
197-
'arbitrary but same among all values type',
198-
serialize = function(value) return value end,
199-
parseValue = function(value) return value end,
200-
parseLiteral = function(node)
201-
error('Literal parsing is implemented in util.coerceValue; ' ..
202-
'we should not go here')
203-
end,
204-
})
194+
function types.map(config)
195+
local instance = {
196+
__type = 'Scalar',
197+
subtype = 'Map',
198+
name = config.name,
199+
description = 'Map is a dictionary with string keys and values of ' ..
200+
'arbitrary but same among all values type',
201+
serialize = function(value) return value end,
202+
parseValue = function(value) return value end,
203+
parseLiteral = function(node)
204+
error('Literal parsing is implemented in util.coerceValue; ' ..
205+
'we should not go here')
206+
end,
207+
values = config.values,
208+
}
209+
210+
return instance
211+
end
205212

206213
function types.inputObject(config)
207214
assert(type(config.name) == 'string', 'type name must be provided as a string')

graphql/query_to_avro.lua

+13-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ local query_to_avro = {}
1616

1717
-- forward declaration
1818
local object_to_avro
19+
local map_to_avro
1920

2021
local gql_scalar_to_avro_index = {
2122
String = "string",
@@ -29,7 +30,9 @@ local gql_scalar_to_avro_index = {
2930

3031
local function gql_scalar_to_avro(fieldType)
3132
assert(fieldType.__type == "Scalar", "GraphQL scalar field expected")
32-
assert(fieldType.name ~= "Map", "Map type is not supported")
33+
if (fieldType.subtype == "Map") then
34+
return map_to_avro(fieldType)
35+
end
3336
local result = gql_scalar_to_avro_index[fieldType.name]
3437
assert(result ~= nil, "Unexpected scalar type: " .. fieldType.name)
3538
return result
@@ -85,6 +88,15 @@ local function gql_type_to_avro(fieldType, subSelections, context)
8588
return result
8689
end
8790

91+
--- The function converts a GraphQL Map type to avro-schema map type.
92+
map_to_avro = function(mapType)
93+
assert(mapType.values ~= nil, "GraphQL Map type must have 'values' field")
94+
return {
95+
type = "map",
96+
values = gql_type_to_avro(mapType.values),
97+
}
98+
end
99+
88100
--- The function converts a single Object field to avro format.
89101
local function field_to_avro(object_type, fields, context)
90102
local firstField = fields[1]

0 commit comments

Comments
 (0)