@@ -443,6 +443,45 @@ gql_type = function(state, avro_schema, collection, collection_name)
443
443
end
444
444
end
445
445
446
+ --- Create virtual root collection `Query`, which has connections to any
447
+ --- collection.
448
+ ---
449
+ --- Actually, each GQL query starts its execution from the `Query` collection.
450
+ --- That is why it shoult contain connections to any collection.
451
+ ---
452
+ --- @tparam table state dictionary which contains all information about the
453
+ --- schema, arguments, types...
454
+ local function create_root_collection (state )
455
+ local root_connections = {}
456
+ -- The fake connections have 1:N mechanics.
457
+ -- Create one connection for each collection.
458
+ for collection_name , collection in pairs (state .collections ) do
459
+ table.insert (root_connections , {
460
+ parts = {},
461
+ name = collection_name ,
462
+ destination_collection = collection_name ,
463
+ type = " 1:N"
464
+ })
465
+ end
466
+ local root_schema = {
467
+ type = " record" ,
468
+ name = " Query" ,
469
+ -- The fake root has no fields.
470
+ fields = {}
471
+ }
472
+ local root_collection = {
473
+ name = " Query" ,
474
+ connections = root_connections
475
+ }
476
+
477
+ -- `gql_type` is designed to create GQL type corresponding to a real schema
478
+ -- and connections. However it also works with the fake schema.
479
+ local root_type = gql_type (state , root_schema , root_collection , " Query" )
480
+ state .schema = schema .create ({
481
+ query = root_type
482
+ })
483
+ end
484
+
446
485
local function parse_cfg (cfg )
447
486
local state = {}
448
487
state .types = utils .gen_booking_table ({})
@@ -461,8 +500,11 @@ local function parse_cfg(cfg)
461
500
local collections = table .copy (cfg .collections )
462
501
state .collections = collections
463
502
464
- local fields = {}
465
-
503
+ -- Prepare types which represents:
504
+ -- - Avro schemas (collections)
505
+ -- - scalar field arguments (used to filter objects by value stored in it's
506
+ -- field)
507
+ -- - list arguments (offset, limit...)
466
508
for collection_name , collection in pairs (state .collections ) do
467
509
collection .name = collection_name
468
510
assert (collection .schema_name ~= nil ,
@@ -495,43 +537,9 @@ local function parse_cfg(cfg)
495
537
state .list_arguments [collection_name ] = list_args
496
538
state .all_arguments [collection_name ] = args
497
539
498
- -- create entry points from collection names
499
- fields [collection_name ] = {
500
- kind = types .nonNull (types .list (state .types [collection_name ])),
501
- arguments = state .all_arguments [collection_name ],
502
- resolve = function (rootValue , args_instance , info )
503
- local object_args_instance = {} -- passed to 'filter'
504
- local list_args_instance = {} -- passed to 'args'
505
- for k , v in pairs (args_instance ) do
506
- if list_args [k ] ~= nil then
507
- list_args_instance [k ] = v
508
- elseif state .object_arguments [k ] ~= nil then
509
- object_args_instance [k ] = v
510
- else
511
- error ((' cannot found "%s" field ("%s" value) ' ..
512
- ' within allowed fields' ):format (tostring (k ),
513
- tostring (v )))
514
- end
515
- end
516
- local from = nil
517
-
518
- local extra = {
519
- qcontext = info .qcontext
520
- }
521
- return accessor :select (rootValue , collection_name , from ,
522
- object_args_instance , list_args_instance , extra )
523
- end ,
524
- }
525
540
end
526
-
527
- local schema = schema .create ({
528
- query = types .object ({
529
- name = ' Query' ,
530
- fields = fields ,
531
- })
532
- })
533
- state .schema = schema
534
-
541
+ -- create fake root `Query` collection
542
+ create_root_collection (state )
535
543
return state
536
544
end
537
545
0 commit comments