@@ -17,12 +17,13 @@ local execute = require('graphql.core.execute')
17
17
18
18
local utils = require (' graphql.utils' )
19
19
20
+ -- local nullable = utils.nullable
21
+
20
22
local tarantool_graphql = {}
21
23
22
24
-- forward declarations
23
25
local gql_type
24
26
25
- --- Returns type of the top element in the avro schema
26
27
local function avro_type (avro_schema )
27
28
if type (avro_schema ) == ' table' then
28
29
if avro_schema .type == ' record' then
@@ -58,16 +59,7 @@ local function avro_type(avro_schema)
58
59
error (' unrecognized avro-schema type: ' .. json .encode (avro_schema ))
59
60
end
60
61
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
71
63
72
64
local types_long = types .scalar ({
73
65
name = ' Long' ,
123
115
--- Non-recursive version of the @{gql_type} function that returns
124
116
--- InputObject instead of Object.
125
117
--- 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.
127
119
local function gql_argument_type (state , avro_schema )
128
120
assert (type (state ) == ' table' ,
129
121
' state must be a table, got ' .. type (state ))
@@ -163,8 +155,6 @@ local function gql_argument_type(state, avro_schema)
163
155
}))
164
156
165
157
return res
166
- --- elseif avro_type(avro_schema) == 'array' then
167
-
168
158
else
169
159
local res = convert_scalar_type (avro_schema , {raise = false })
170
160
if res == nil then
@@ -175,17 +165,13 @@ local function gql_argument_type(state, avro_schema)
175
165
end
176
166
177
167
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.
180
170
---
181
171
--- @tparam table state
182
172
--- @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 )
186
174
local args = {}
187
- local is_for_args = opts and opts .is_for_args or false
188
-
189
175
for _ , field in ipairs (fields ) do
190
176
191
177
assert (type (field .name ) == ' string' ,
@@ -197,9 +183,8 @@ local function convert_record_fields_to_args(state, fields, opts)
197
183
-- arrays (gql lists) and maps can't be arguments
198
184
-- so these kinds are to be skipped
199
185
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
203
188
args [field .name ] = nullable (gql_class )
204
189
end
205
190
end
217
202
local function convert_record_fields (state , fields , opts )
218
203
local res = {}
219
204
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
221
207
222
208
for _ , field in ipairs (fields ) do
223
209
assert (type (field .name ) == ' string' ,
@@ -231,19 +217,15 @@ local function convert_record_fields(state, fields, opts)
231
217
232
218
233
219
-- 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
240
222
object_args [field .name ] = nullable (res [field .name ].kind )
241
223
end
242
224
end
243
225
return res , object_args
244
226
end
245
227
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).
247
229
---
248
230
--- @tparam table state for read state.accessor and previously filled
249
231
--- state.types (state.types are gql types)
@@ -518,7 +500,7 @@ local function parse_cfg(cfg)
518
500
end
519
501
520
502
--- The function checks if given query has an appropriate type 'query'
521
- --- (mutations are not supported yet)
503
+ --- (mutations are not supported yet).
522
504
local function assert_gql_query_ast (func_name , ast )
523
505
assert (# ast .definitions == 1 ,
524
506
func_name .. ' : expected an one query' )
@@ -530,7 +512,8 @@ local function assert_gql_query_ast(func_name, ast)
530
512
type (operation_name ))
531
513
end
532
514
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.
534
517
local function gql_execute (qstate , variables )
535
518
assert (qstate .state )
536
519
local state = qstate .state
@@ -549,7 +532,8 @@ local function gql_execute(qstate, variables)
549
532
end
550
533
551
534
--- 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
+ ---
553
537
--- @tparam table state current state of graphql-lib, including
554
538
--- schemas, collections and accessor
555
539
--- @tparam string query raw query string
@@ -649,4 +633,4 @@ function tarantool_graphql.new(cfg)
649
633
})
650
634
end
651
635
652
- return tarantool_graphql
636
+ return tarantool_graphql
0 commit comments