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

Commit ac9f0ed

Browse files
committed
readability refactor; clean up comments;
move nullable function to utils
1 parent f0fb9f4 commit ac9f0ed

File tree

1 file changed

+20
-36
lines changed

1 file changed

+20
-36
lines changed

graphql/tarantool_graphql.lua

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ local execute = require('graphql.core.execute')
1717

1818
local utils = require('graphql.utils')
1919

20+
--local nullable = utils.nullable
21+
2022
local tarantool_graphql = {}
2123

2224
-- forward declarations
2325
local gql_type
2426

25-
--- Returns type of the top element in the avro schema
2627
local function avro_type(avro_schema)
2728
if type(avro_schema) == 'table' then
2829
if avro_schema.type == 'record' then
@@ -58,16 +59,7 @@ local function avro_type(avro_schema)
5859
error('unrecognized avro-schema type: ' .. json.encode(avro_schema))
5960
end
6061

61-
-- XXX: recursive skip several NonNull's?
62-
local function nullable(gql_class)
63-
assert(type(gql_class) == 'table', 'gql_class must be a table, got ' ..
64-
type(gql_class))
65-
66-
if gql_class.__type ~= 'NonNull' then return gql_class end
67-
68-
assert(gql_class.ofType ~= nil, 'gql_class.ofType must not be nil')
69-
return gql_class.ofType
70-
end
62+
local nullable = utils.nullable
7163

7264
local types_long = types.scalar({
7365
name = 'Long',
@@ -123,7 +115,7 @@ end
123115
--- Non-recursive version of the @{gql_type} function that returns
124116
--- InputObject instead of Object.
125117
--- An error will be raised in case of fields that are not scalar types
126-
--- as there are no sense in non scalar arguments
118+
--- as there are no sense in non scalar arguments.
127119
local function gql_argument_type(state, avro_schema)
128120
assert(type(state) == 'table',
129121
'state must be a table, got ' .. type(state))
@@ -163,8 +155,6 @@ local function gql_argument_type(state, avro_schema)
163155
}))
164156

165157
return res
166-
--- elseif avro_type(avro_schema) == 'array' then
167-
168158
else
169159
local res = convert_scalar_type(avro_schema, {raise = false})
170160
if res == nil then
@@ -175,17 +165,13 @@ local function gql_argument_type(state, avro_schema)
175165
end
176166

177167

178-
--- Returns table of record's arguments
179-
--- all arguments are nullable
168+
--- Recursively convert each field of an avro-schema to a graphql type and
169+
--- corresponding argument for an upper graphql type.
180170
---
181171
--- @tparam table state
182172
--- @tparam table fields
183-
--- @tparam table opts include is_for_args flag to specify
184-
--- case when the function is used to collect arguments
185-
local function convert_record_fields_to_args(state, fields, opts)
173+
local function convert_record_fields_to_args(state, fields)
186174
local args = {}
187-
local is_for_args = opts and opts.is_for_args or false
188-
189175
for _, field in ipairs(fields) do
190176

191177
assert(type(field.name) == 'string',
@@ -197,9 +183,8 @@ local function convert_record_fields_to_args(state, fields, opts)
197183
-- arrays (gql lists) and maps can't be arguments
198184
-- so these kinds are to be skipped
199185

200-
---@todo consider case when gql_class is wrapper nonNull around List
201-
--- or Map
202-
if not(is_for_args and (gql_class == 'List' or gql_class == 'Map')) then
186+
if (nullable(gql_class) ~= 'List'
187+
and nullable(gql_class) ~= 'Map') then
203188
args[field.name] = nullable(gql_class)
204189
end
205190
end
@@ -217,7 +202,8 @@ end
217202
local function convert_record_fields(state, fields, opts)
218203
local res = {}
219204
local object_args = {}
220-
local is_for_args = opts and opts.is_for_args or false
205+
local opts = opts or {}
206+
local is_for_args = opts.is_for_args or false
221207

222208
for _, field in ipairs(fields) do
223209
assert(type(field.name) == 'string',
@@ -231,19 +217,15 @@ local function convert_record_fields(state, fields, opts)
231217

232218

233219
-- arrays (gql lists) and maps can't be arguments
234-
-- so these kinds are to be skipped
235-
236-
---@todo consider case when gql_class is wrapper nonNull around List
237-
--- or Map
238-
if not (is_for_args and (res[field.name].kind == 'List'
239-
or res[field.name].kind == 'Map')) then
220+
if not is_for_args or (nullable(res[field.name].kind) ~= 'List'
221+
and nullable(res[field.name].kind) ~= 'Map') then
240222
object_args[field.name] = nullable(res[field.name].kind)
241223
end
242224
end
243225
return res, object_args
244226
end
245227

246-
--- The function recursively converts passed avro-schema to a graphql type (kind)
228+
--- The function recursively converts passed avro-schema to a graphql type (kind).
247229
---
248230
--- @tparam table state for read state.accessor and previously filled
249231
--- state.types (state.types are gql types)
@@ -518,7 +500,7 @@ local function parse_cfg(cfg)
518500
end
519501

520502
--- The function checks if given query has an appropriate type 'query'
521-
--- (mutations are not supported yet)
503+
--- (mutations are not supported yet).
522504
local function assert_gql_query_ast(func_name, ast)
523505
assert(#ast.definitions == 1,
524506
func_name .. ': expected an one query')
@@ -530,7 +512,8 @@ local function assert_gql_query_ast(func_name, ast)
530512
type(operation_name))
531513
end
532514

533-
--- The function just makes some assertions and then call graphql-lua execute
515+
--- The function just makes some reasonable assertions on input
516+
--- and then call graphql-lua execute.
534517
local function gql_execute(qstate, variables)
535518
assert(qstate.state)
536519
local state = qstate.state
@@ -549,7 +532,8 @@ local function gql_execute(qstate, variables)
549532
end
550533

551534
--- The function parses a raw query string, validate the resulting query
552-
--- and return it ready for execution
535+
--- and make it ready for execution.
536+
---
553537
--- @tparam table state current state of graphql-lib, including
554538
--- schemas, collections and accessor
555539
--- @tparam string query raw query string
@@ -649,4 +633,4 @@ function tarantool_graphql.new(cfg)
649633
})
650634
end
651635

652-
return tarantool_graphql
636+
return tarantool_graphql

0 commit comments

Comments
 (0)