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

Commit 417c83e

Browse files
committed
do small refactor; add comments
1 parent 589778a commit 417c83e

File tree

1 file changed

+55
-16
lines changed

1 file changed

+55
-16
lines changed

graphql/tarantool_graphql.lua

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ local function avro_type(avro_schema)
3030
return 'record*'
3131
elseif utils.is_array(avro_schema) then
3232
return 'union'
33+
elseif avro_schema.type == 'array' then
34+
return 'array'
35+
elseif avro_schema.type == 'array*' then
36+
return 'array*'
37+
elseif avro_schema.type == 'map' then
38+
return 'map'
39+
elseif avro_schema.type == 'map*' then
40+
return 'map*'
3341
end
3442
elseif type(avro_schema) == 'string' then
3543
if avro_schema == 'int' then
@@ -105,6 +113,8 @@ end
105113

106114
--- Non-recursive version of the @{gql_type} function that returns
107115
--- InputObject instead of Object.
116+
--- An error will be raised in case of fields that are not scalar types
117+
--- as there are no sense in non scalar arguments
108118
local function gql_argument_type(state, avro_schema)
109119
assert(type(state) == 'table',
110120
'state must be a table, got ' .. type(state))
@@ -115,17 +125,23 @@ local function gql_argument_type(state, avro_schema)
115125
assert(type(avro_schema.name) == 'string',
116126
('avro_schema.name must be a string, got %s (avro_schema %s)')
117127
:format(type(avro_schema.name), json.encode(avro_schema)))
128+
118129
assert(type(avro_schema.fields) == 'table',
119130
('avro_schema.fields must be a table, got %s (avro_schema %s)')
120131
:format(type(avro_schema.fields), json.encode(avro_schema)))
121132

133+
---@tfixme iteration over null table
134+
--- maybe avro.scheme was meant?
122135
local fields = {}
123-
for _, field in ipairs(fields) do
136+
for _, field in ipairs(avro_schema.fields) do
137+
124138
assert(type(field.name) == 'string',
125139
('field.name must be a string, got %s (schema %s)')
126140
:format(type(field.name), json.encode(field)))
141+
127142
local gql_field_type = convert_scalar_type(
128143
field.type, {raise = true})
144+
129145
fields[field.name] = {
130146
name = field.name,
131147
kind = types.nonNull(gql_field_type),
@@ -140,6 +156,8 @@ local function gql_argument_type(state, avro_schema)
140156
}))
141157

142158
return res
159+
--- elseif avro_type(avro_schema) == 'array' then
160+
143161
else
144162
local res = convert_scalar_type(avro_schema, {raise = false})
145163
if res == nil then
@@ -149,31 +167,38 @@ local function gql_argument_type(state, avro_schema)
149167
end
150168
end
151169

170+
171+
--- Returns table of record's arguments
172+
--- all arguments are nullable
152173
local function convert_record_fields_to_args(state, fields)
153174
local args = {}
154175
for _, field in ipairs(fields) do
176+
155177
assert(type(field.name) == 'string',
156178
('field.name must be a string, got %s (schema %s)')
157179
:format(type(field.name), json.encode(field)))
180+
158181
local gql_class = gql_argument_type(state, field.type)
159182
args[field.name] = nullable(gql_class)
160183
end
161184
return args
162185
end
163186

164-
--- Convert each field of an avro-schema to a graphql type and corresponding
165-
--- argument for an upper graphql type.
187+
--- Recursively convert each field of an avro-schema to a graphql type and
188+
--- corresponding argument for an upper graphql type.
166189
---
167190
--- @tparam table state for read state.accessor and previously filled
168191
--- state.types
169192
--- @tparam table fields fields part from an avro-schema
170193
local function convert_record_fields(state, fields)
171194
local res = {}
172195
local object_args = {}
196+
173197
for _, field in ipairs(fields) do
174198
assert(type(field.name) == 'string',
175199
('field.name must be a string, got %s (schema %s)')
176200
:format(type(field.name), json.encode(field)))
201+
177202
res[field.name] = {
178203
name = field.name,
179204
kind = gql_type(state, field.type),
@@ -186,7 +211,7 @@ end
186211
--- The function recursively converts passed avro-schema to a graphql type.
187212
---
188213
--- @tparam table state for read state.accessor and previously filled
189-
--- state.types
214+
--- state.types (state.types are gql types)
190215
--- @tparam table avro_schema input avro-schema
191216
--- @tparam[opt] table collection table with schema_name, connections fields
192217
--- described a collection (e.g. tarantool's spaces)
@@ -230,6 +255,7 @@ gql_type = function(state, avro_schema, collection, collection_name)
230255

231256
local fields, _ = convert_record_fields(state, avro_schema.fields)
232257

258+
-- if collection param is passed
233259
for _, c in ipairs((collection or {}).connections or {}) do
234260
assert(type(c.type) == 'string',
235261
'connection.type must be a string, got ' .. type(c.type))
@@ -333,9 +359,12 @@ gql_type = function(state, avro_schema, collection, collection_name)
333359
error('unrecognized avro-schema type: ' .. json.encode(avro_schema))
334360
end
335361
return res
362+
336363
end
364+
337365
end
338366

367+
339368
local function parse_cfg(cfg)
340369
local state = {}
341370
state.types = utils.gen_booking_table({})
@@ -356,32 +385,37 @@ local function parse_cfg(cfg)
356385

357386
local fields = {}
358387

359-
for name, collection in pairs(state.collections) do
360-
collection.name = name
388+
for collection_name, collection in pairs(state.collections) do
389+
collection.name = collection_name
361390
assert(collection.schema_name ~= nil,
362391
'collection.schema_name must not be nil')
392+
363393
local schema = cfg.schemas[collection.schema_name]
364394
assert(schema ~= nil, ('cfg.schemas[%s] must not be nil'):format(
365395
tostring(collection.schema_name)))
366396
assert(schema.name == nil or schema.name == collection.schema_name,
367397
('top-level schema name does not match the name in ' ..
368398
'the schema itself: "%s" vs "%s"'):format(collection.schema_name,
369399
schema.name))
370-
state.types[name] = gql_type(state, schema, collection, name)
371400

401+
-- recursively converts all avro types into gql types in the given schema
402+
state.types[collection_name] = gql_type(state, schema, collection, collection_name)
403+
404+
-- prepare arguments
372405
local _, object_args = convert_record_fields(state,
373406
schema.fields)
374407
local list_args = convert_record_fields_to_args(
375-
state, accessor:list_args(name))
408+
state, accessor:list_args(collection_name))
376409
local args = utils.merge_tables(object_args, list_args)
377-
state.object_arguments[name] = object_args
378-
state.list_arguments[name] = list_args
379-
state.all_arguments[name] = args
410+
411+
state.object_arguments[collection_name] = object_args
412+
state.list_arguments[collection_name] = list_args
413+
state.all_arguments[collection_name] = args
380414

381415
-- create entry points from collection names
382-
fields[name] = {
383-
kind = types.nonNull(types.list(state.types[name])),
384-
arguments = state.all_arguments[name],
416+
fields[collection_name] = {
417+
kind = types.nonNull(types.list(state.types[collection_name])),
418+
arguments = state.all_arguments[collection_name],
385419
resolve = function(rootValue, args_instance, info)
386420
local object_args_instance = {} -- passed to 'filter'
387421
local list_args_instance = {} -- passed to 'args'
@@ -397,7 +431,7 @@ local function parse_cfg(cfg)
397431
end
398432
end
399433
local from = nil
400-
return accessor:select(rootValue, name, from,
434+
return accessor:select(rootValue, collection_name, from,
401435
object_args_instance, list_args_instance)
402436
end,
403437
}
@@ -414,7 +448,7 @@ local function parse_cfg(cfg)
414448
return state
415449
end
416450

417-
--- The function checks if given query has an appropriate type
451+
--- The function checks if given query has an appropriate type 'query'
418452
--- (mutations are not supported yet)
419453
local function assert_gql_query_ast(func_name, ast)
420454
assert(#ast.definitions == 1,
@@ -427,6 +461,7 @@ local function assert_gql_query_ast(func_name, ast)
427461
type(operation_name))
428462
end
429463

464+
--- The function just makes some assertions and then call graphql-lua execute
430465
local function gql_execute(qstate, variables)
431466
assert(qstate.state)
432467
local state = qstate.state
@@ -446,6 +481,9 @@ end
446481

447482
--- The function parses a raw query string, validate the resulting query
448483
--- and return it ready for execution
484+
--- @tparam table state current state of graphql-lib, including
485+
--- schemas, collections and accessor
486+
--- @tparam string query raw query string
449487
local function gql_compile(state, query)
450488
assert(type(state) == 'table' and type(query) == 'string',
451489
'use :validate(...) instead of .validate(...)')
@@ -462,6 +500,7 @@ local function gql_compile(state, query)
462500
ast = ast,
463501
operation_name = operation_name,
464502
}
503+
465504
local gql_query = setmetatable(qstate, {
466505
__index = {
467506
execute = gql_execute,

0 commit comments

Comments
 (0)