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

Commit 42ab7b1

Browse files
committed
WIP: Preliminary patch for update/delete mutations
1 parent 3e18e52 commit 42ab7b1

File tree

2 files changed

+73
-16
lines changed

2 files changed

+73
-16
lines changed

graphql/accessor_general.lua

+36-3
Original file line numberDiff line numberDiff line change
@@ -1486,10 +1486,43 @@ function accessor_general.new(opts, funcs)
14861486
extra_args = function(self, collection_name)
14871487
local collection = self.collections[collection_name]
14881488
local schema_name = collection.schema_name
1489-
local schema = table.copy(self.schemas[schema_name])
1490-
schema.name = collection_name .. '_insert'
1489+
1490+
local schema_insert = table.copy(self.schemas[schema_name])
1491+
schema_insert.name = collection_name .. '_insert'
1492+
1493+
-- XXX: opts.multi
1494+
local schema_update = {
1495+
name = collection_name .. '_update',
1496+
type = 'record',
1497+
fields = {},
1498+
}
1499+
for _, field in ipairs(self.schemas[schema_name].fields) do
1500+
local field = table.copy(field)
1501+
field.type = avro_helpers.make_avro_type_nullable(
1502+
field.type)
1503+
table.insert(schema_update.fields, field)
1504+
end
1505+
1506+
-- XXX: opts.multi
1507+
local schema_delete = 'boolean'
1508+
14911509
return {
1492-
{name = 'insert', type = schema}
1510+
{name = 'insert', type = schema_insert},
1511+
{name = 'update', type = schema_update},
1512+
{name = 'delete', type = schema_delete},
1513+
}, {
1514+
insert = {
1515+
add_to_mutations_only = true,
1516+
add_to_top_fields_only = true,
1517+
},
1518+
update = {
1519+
add_to_mutations_only = true,
1520+
add_to_top_fields_only = false,
1521+
},
1522+
delete = {
1523+
add_to_mutations_only = true,
1524+
add_to_top_fields_only = false,
1525+
},
14931526
}
14941527
end,
14951528
}

graphql/tarantool_graphql.lua

+37-13
Original file line numberDiff line numberDiff line change
@@ -1248,16 +1248,26 @@ local function create_root_collection(state)
12481248
root_types[what] = nullable(gql_type(state, root_schema,
12491249
root_collection, nil))
12501250

1251-
end
1252-
1253-
-- add extra arguments to top-level fields (collections)
1254-
for collection_name, field in pairs(root_types['Mutation'].fields) do
1255-
-- Prevent exposing an argument into the query schema subtree (it is
1256-
-- needed because we use a booking table for arguments).
1257-
field.arguments = table.copy(field.arguments)
1258-
1259-
for name, arg in pairs(state.extra_arguments[collection_name]) do
1260-
field.arguments[name] = arg
1251+
-- add extra arguments to top-level fields (collections)
1252+
for collection_name, field in pairs(root_types[what].fields) do
1253+
-- Prevent exposing an argument inserted, say, into the mutation schema
1254+
-- subtree to the query subtree (it is needed because we use a booking
1255+
-- table for arguments).
1256+
field.arguments = table.copy(field.arguments)
1257+
1258+
local extra_args = state.extra_arguments[collection_name]
1259+
local extra_args_meta = state.extra_arguments_meta[collection_name]
1260+
1261+
for arg_name, arg in pairs(extra_args) do
1262+
local meta = extra_args_meta[arg_name]
1263+
check(meta, 'meta', 'table')
1264+
-- note: we handle add_to_top_fields_only == false case in
1265+
-- add_connection_arguments
1266+
if meta.add_to_top_fields_only and what == 'Mutation' or
1267+
not meta.add_to_mutations_only then
1268+
field.arguments[arg_name] = arg
1269+
end
1270+
end
12611271
end
12621272
end
12631273

@@ -1343,6 +1353,8 @@ local function add_connection_arguments(state)
13431353
}
13441354
end
13451355
end)
1356+
1357+
-- XXX: handle add_to_top_fields_only == false case of extra_arguments
13461358
end
13471359

13481360
local function parse_cfg(cfg)
@@ -1362,6 +1374,7 @@ local function parse_cfg(cfg)
13621374
-- argument. We capture extra_arguments[collection_name] into the resolve
13631375
-- function and sure it exists and will not be changed.
13641376
state.extra_arguments = utils.gen_booking_table({})
1377+
state.extra_arguments_meta = {}
13651378

13661379
-- map from avro-schema names to graphql types
13671380
state.definitions = {}
@@ -1415,12 +1428,16 @@ local function parse_cfg(cfg)
14151428
{skip_compound = true})
14161429
local list_args = convert_record_fields_to_args(
14171430
accessor:list_args(collection_name))
1418-
local extra_args = convert_record_fields_to_args(
1419-
accessor:extra_args(collection_name), {dont_skip = true})
1431+
local extra_args_avro, extra_args_meta = accessor:extra_args(
1432+
collection_name)
1433+
check(extra_args_meta, 'extra_args_meta', 'table')
1434+
local extra_args = convert_record_fields_to_args(extra_args_avro,
1435+
{dont_skip = true})
14201436

14211437
state.object_arguments[collection_name] = object_args
14221438
state.list_arguments[collection_name] = list_args
14231439
state.extra_arguments[collection_name] = extra_args
1440+
state.extra_arguments_meta[collection_name] = extra_args_meta
14241441
end
14251442

14261443
add_connection_arguments(state)
@@ -1683,7 +1700,14 @@ end
16831700
--- }
16841701
--- end,
16851702
--- extra_args = function(self, collection_name)
1686-
--- return ...
1703+
--- ...
1704+
--- local args_meta = {
1705+
--- arg_name = {
1706+
--- add_to_mutations_only = true / false,
1707+
--- add_to_top_fields_only = true / false,
1708+
--- }
1709+
--- }
1710+
--- return schemas_list, args_meta
16871711
--- end
16881712
--- }
16891713
--- }),

0 commit comments

Comments
 (0)