-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcomposer.rb
More file actions
784 lines (651 loc) · 31.9 KB
/
composer.rb
File metadata and controls
784 lines (651 loc) · 31.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
# frozen_string_literal: true
require_relative "composer/base_validator"
require_relative "composer/validate_interfaces"
require_relative "composer/validate_type_resolvers"
require_relative "composer/type_resolver_config"
require_relative "composer/authorization"
module GraphQL
module Stitching
# Composer receives many individual `GraphQL::Schema` instances
# representing various graph locations and merges them into one
# combined Supergraph that is validated for integrity.
class Composer
include Authorization
# @api private
NO_DEFAULT_VALUE = begin
t = Class.new(GraphQL::Schema::Object) do
field(:f, String) { _1.argument(:a, String) }
end
t.get_field("f").get_argument("a").default_value
end
# @api private
COMPOSITION_VALIDATORS = [
ValidateInterfaces,
ValidateTypeResolvers,
].freeze
# @return [String] name of the Query type in the composed schema.
attr_reader :query_name
# @return [String] name of the Mutation type in the composed schema.
attr_reader :mutation_name
# @return [String] name of the Subscription type in the composed schema.
attr_reader :subscription_name
# @api private
attr_reader :subgraph_types_by_name_and_location
# @api private
attr_reader :schema_directives
def initialize(
query_name: "Query",
mutation_name: "Mutation",
subscription_name: "Subscription",
visibility_profiles: [],
root_entrypoints: nil,
formatter: nil
)
@query_name = query_name
@mutation_name = mutation_name
@subscription_name = subscription_name
@root_entrypoints = root_entrypoints || {}
@formatter = formatter || Formatter::Default
@field_map = {}
@resolver_map = {}
@resolver_configs = {}
@mapped_type_names = {}
@visibility_profiles = Set.new(visibility_profiles)
@authorizations_by_type_and_field = {}
@subgraph_directives_by_name_and_location = nil
@subgraph_types_by_name_and_location = nil
@schema_directives = nil
end
def perform(locations_input)
if @subgraph_types_by_name_and_location
raise CompositionError, "Composer may only perform once per instance."
end
schemas, executables = prepare_locations_input(locations_input)
directives_to_omit = [
GraphQL::Stitching.stitch_directive,
GraphQL::Stitching.authorization_directive,
Directives::SupergraphKey.graphql_name,
Directives::SupergraphResolver.graphql_name,
Directives::SupergraphSource.graphql_name,
]
# "directive_name" => "location" => subgraph_directive
@subgraph_directives_by_name_and_location = schemas.each_with_object({}) do |(location, schema), memo|
(schema.directives.keys - schema.default_directives.keys - directives_to_omit).each do |directive_name|
memo[directive_name] ||= {}
memo[directive_name][location] = schema.directives[directive_name]
end
end
# "directive_name" => merged_directive
@schema_directives = @subgraph_directives_by_name_and_location.each_with_object({}) do |(directive_name, directives_by_location), memo|
memo[directive_name] = build_directive(directive_name, directives_by_location)
end
@schema_directives.merge!(GraphQL::Schema.default_directives)
# "Typename" => "location" => subgraph_type
@subgraph_types_by_name_and_location = schemas.each_with_object({}) do |(location, schema), memo|
raise CompositionError, "Location keys must be strings" unless location.is_a?(String)
schema.types.each do |type_name, subgraph_type|
next if subgraph_type.introspection?
if type_name == @query_name && subgraph_type != schema.query
raise CompositionError, "Query name \"#{@query_name}\" is used by non-query type in #{location} schema."
elsif type_name == @mutation_name && subgraph_type != schema.mutation
raise CompositionError, "Mutation name \"#{@mutation_name}\" is used by non-mutation type in #{location} schema."
elsif type_name == @subscription_name && subgraph_type != schema.subscription
raise CompositionError, "Subscription name \"#{@subscription_name}\" is used by non-subscription type in #{location} schema."
end
type_name = @query_name if subgraph_type == schema.query
type_name = @mutation_name if subgraph_type == schema.mutation
type_name = @subscription_name if subgraph_type == schema.subscription
@mapped_type_names[subgraph_type.graphql_name] = type_name if subgraph_type.graphql_name != type_name
memo[type_name] ||= {}
memo[type_name][location] = subgraph_type
end
end
enum_usage = build_enum_usage_map(schemas.values)
# "Typename" => merged_type
schema_types = @subgraph_types_by_name_and_location.each_with_object({}) do |(type_name, types_by_location), memo|
kinds = types_by_location.values.map { _1.kind.name }.tap(&:uniq!)
if kinds.length > 1
raise CompositionError, "Cannot merge different kinds for `#{type_name}`. Found: #{kinds.join(", ")}."
end
extract_resolvers(type_name, types_by_location) if type_name == @query_name
memo[type_name] = case kinds.first
when "SCALAR"
build_scalar_type(type_name, types_by_location)
when "ENUM"
build_enum_type(type_name, types_by_location, enum_usage)
when "OBJECT"
build_object_type(type_name, types_by_location)
when "INTERFACE"
build_interface_type(type_name, types_by_location)
when "UNION"
build_union_type(type_name, types_by_location)
when "INPUT_OBJECT"
build_input_object_type(type_name, types_by_location)
else
raise CompositionError, "Unexpected kind encountered for `#{type_name}`. Found: #{kinds.first}."
end
end
builder = self
schema = Class.new(GraphQL::Schema) do
object_types = schema_types.values.select { |t| t.respond_to?(:kind) && t.kind.object? }
add_type_and_traverse(schema_types.values, root: false)
orphan_types(object_types)
query schema_types[builder.query_name]
mutation schema_types[builder.mutation_name]
subscription schema_types[builder.subscription_name]
directives builder.schema_directives.values
object_types.each do |t|
t.interfaces.each { _1.orphan_types(t) }
end
own_orphan_types.clear
end
select_root_field_locations(schema)
expand_abstract_resolvers(schema, schemas)
apply_supergraph_directives(schema, @resolver_map, @field_map)
apply_authorization_directives(schema)
if (visibility_def = schema.directives[GraphQL::Stitching.visibility_directive])
visibility_def.get_argument("profiles").default_value(@visibility_profiles.to_a.sort)
end
supergraph = Supergraph.from_definition(schema, executables: executables)
COMPOSITION_VALIDATORS.each do |validator_class|
validator_class.new.perform(supergraph, self)
end
supergraph
end
# @!scope class
# @!visibility private
def prepare_locations_input(locations_input)
schemas = {}
executables = {}
locations_input.each do |location, input|
schema = input[:schema]
if schema.nil?
raise CompositionError, "A schema is required for `#{location}` location."
elsif !(schema.is_a?(Class) && schema <= GraphQL::Schema)
raise CompositionError, "The schema for `#{location}` location must be a GraphQL::Schema class."
end
@resolver_configs.merge!(TypeResolverConfig.extract_directive_assignments(schema, location, input[:stitch]))
@resolver_configs.merge!(TypeResolverConfig.extract_federation_entities(schema, location))
if schema.directives[GraphQL::Stitching.authorization_directive]
SubgraphAuthorization.new(schema).reverse_merge!(@authorizations_by_type_and_field)
end
schemas[location.to_s] = schema
executables[location.to_s] = input[:executable] || schema
end
return schemas, executables
end
# @!scope class
# @!visibility private
def build_directive(directive_name, directives_by_location)
builder = self
Class.new(GraphQL::Schema::Directive) do
graphql_name(directive_name)
description(builder.merge_descriptions(directive_name, directives_by_location))
repeatable(directives_by_location.values.any?(&:repeatable?))
locations(*directives_by_location.values.flat_map(&:locations).tap(&:uniq!))
builder.build_merged_arguments(directive_name, directives_by_location, self, directive_name: directive_name)
end
end
# @!scope class
# @!visibility private
def build_scalar_type(type_name, types_by_location)
built_in_type = GraphQL::Schema::BUILT_IN_TYPES[type_name]
return built_in_type if built_in_type
builder = self
Class.new(GraphQL::Stitching::Supergraph::ScalarType) do
graphql_name(type_name)
description(builder.merge_descriptions(type_name, types_by_location))
builder.build_merged_directives(type_name, types_by_location, self)
end
end
# @!scope class
# @!visibility private
def build_enum_type(type_name, types_by_location, enum_usage)
builder = self
# "value" => "location" => enum_value
enum_values_by_name_location = types_by_location.each_with_object({}) do |(location, subgraph_type), memo|
subgraph_type.enum_values.each do |subgraph_enum_value|
memo[subgraph_enum_value.graphql_name] ||= {}
memo[subgraph_enum_value.graphql_name][location] = subgraph_enum_value
end
end
# intersect input enum types
if enum_usage.fetch(type_name, EMPTY_ARRAY).include?(:write)
enum_values_by_name_location.reject! do |value, enum_values_by_location|
types_by_location.keys.length != enum_values_by_location.keys.length
end
end
Class.new(GraphQL::Stitching::Supergraph::EnumType) do
graphql_name(type_name)
description(builder.merge_descriptions(type_name, types_by_location))
builder.build_merged_directives(type_name, types_by_location, self)
enum_values_by_name_location.each do |value_name, enum_values_by_location|
enum_value = value(value_name,
value: value_name,
description: builder.merge_descriptions(type_name, enum_values_by_location, enum_value: value_name),
deprecation_reason: builder.merge_deprecations(type_name, enum_values_by_location, enum_value: value_name),
)
builder.build_merged_directives(type_name, enum_values_by_location, enum_value, enum_value: value_name)
end
end
end
# @!scope class
# @!visibility private
def build_object_type(type_name, types_by_location)
builder = self
Class.new(GraphQL::Stitching::Supergraph::ObjectType) do
graphql_name(type_name)
description(builder.merge_descriptions(type_name, types_by_location))
interface_names = types_by_location.values.flat_map { _1.interfaces.map(&:graphql_name) }
interface_names.tap(&:uniq!).each do |interface_name|
implements(builder.build_type_binding(interface_name))
end
builder.build_merged_fields(type_name, types_by_location, self)
builder.build_merged_directives(type_name, types_by_location, self)
end
end
# @!scope class
# @!visibility private
def build_interface_type(type_name, types_by_location)
builder = self
Module.new do
include GraphQL::Stitching::Supergraph::InterfaceType
graphql_name(type_name)
description(builder.merge_descriptions(type_name, types_by_location))
interface_names = types_by_location.values.flat_map { _1.interfaces.map(&:graphql_name) }
interface_names.tap(&:uniq!).each do |interface_name|
implements(builder.build_type_binding(interface_name))
end
builder.build_merged_fields(type_name, types_by_location, self)
builder.build_merged_directives(type_name, types_by_location, self)
end
end
# @!scope class
# @!visibility private
def build_union_type(type_name, types_by_location)
builder = self
Class.new(GraphQL::Stitching::Supergraph::UnionType) do
graphql_name(type_name)
description(builder.merge_descriptions(type_name, types_by_location))
possible_names = types_by_location.values.flat_map { _1.possible_types.map(&:graphql_name) }.tap(&:uniq!)
possible_types(*possible_names.map { builder.build_type_binding(_1) })
builder.build_merged_directives(type_name, types_by_location, self)
end
end
# @!scope class
# @!visibility private
def build_input_object_type(type_name, types_by_location)
builder = self
Class.new(GraphQL::Stitching::Supergraph::InputObjectType) do
graphql_name(type_name)
description(builder.merge_descriptions(type_name, types_by_location))
builder.build_merged_arguments(type_name, types_by_location, self)
builder.build_merged_directives(type_name, types_by_location, self)
end
end
# @!scope class
# @!visibility private
def build_type_binding(type_name)
GraphQL::Schema::LateBoundType.new(@mapped_type_names.fetch(type_name, type_name))
end
# @!scope class
# @!visibility private
def build_merged_fields(type_name, types_by_location, owner)
# "field_name" => "location" => field
fields_by_name_location = types_by_location.each_with_object({}) do |(location, subgraph_type), memo|
@field_map[type_name] ||= {}
subgraph_type.fields.each do |field_name, subgraph_field|
@field_map[type_name][subgraph_field.name] ||= []
@field_map[type_name][subgraph_field.name] << location
memo[field_name] ||= {}
memo[field_name][location] = subgraph_field
end
end
fields_by_name_location.each do |field_name, fields_by_location|
value_types = fields_by_location.values.map(&:type)
type = merge_value_types(type_name, value_types, field_name: field_name)
schema_field = owner.field(
field_name,
description: merge_descriptions(type_name, fields_by_location, field_name: field_name),
deprecation_reason: merge_deprecations(type_name, fields_by_location, field_name: field_name),
type: Util.unwrap_non_null(type),
null: !type.non_null?,
connection: false,
camelize: false,
)
build_merged_arguments(type_name, fields_by_location, schema_field, field_name: field_name)
build_merged_directives(type_name, fields_by_location, schema_field, field_name: field_name)
end
end
# @!scope class
# @!visibility private
def build_merged_arguments(type_name, members_by_location, owner, field_name: nil, directive_name: nil)
# "argument_name" => "location" => argument
args_by_name_location = members_by_location.each_with_object({}) do |(location, subgraph_member), memo|
subgraph_member.arguments.each do |argument_name, argument|
memo[argument_name] ||= {}
memo[argument_name][location] = argument
end
end
args_by_name_location.each do |argument_name, arguments_by_location|
value_types = arguments_by_location.values.map(&:type)
if arguments_by_location.length != members_by_location.length
if value_types.any?(&:non_null?)
path = [type_name, field_name, argument_name].compact.join(".")
raise CompositionError, "Required argument `#{path}` must be defined in all locations." # ...or hidden?
end
next
end
kwargs = {}
default_values_by_location = arguments_by_location.each_with_object({}) do |(location, argument), memo|
next if argument.default_value == NO_DEFAULT_VALUE
memo[location] = argument.default_value
end
unless default_values_by_location.empty?
kwargs[:default_value] = @formatter.merge_default_values(default_values_by_location, Formatter::Info.new(
type_name: type_name,
field_name: field_name,
argument_name: argument_name,
directive_name: directive_name,
))
end
type = merge_value_types(type_name, value_types, argument_name: argument_name, field_name: field_name)
schema_argument = owner.argument(
argument_name,
description: merge_descriptions(type_name, arguments_by_location, argument_name: argument_name, field_name: field_name),
deprecation_reason: merge_deprecations(type_name, arguments_by_location, argument_name: argument_name, field_name: field_name),
type: Util.unwrap_non_null(type),
required: type.non_null?,
camelize: false,
**kwargs,
)
build_merged_directives(type_name, arguments_by_location, schema_argument, field_name: field_name, argument_name: argument_name)
end
end
# @!scope class
# @!visibility private
def build_merged_directives(type_name, members_by_location, owner, field_name: nil, argument_name: nil, enum_value: nil)
directives_by_name_location = members_by_location.each_with_object({}) do |(location, subgraph_member), memo|
subgraph_member.directives.each do |directive|
memo[directive.graphql_name] ||= {}
memo[directive.graphql_name][location] = directive
end
end
directives_by_name_location.each do |directive_name, directives_by_location|
kwarg_formatter = @formatter
directive_class = @schema_directives[directive_name]
next unless directive_class
# handled by deprecation_reason merger...
next if directive_class.graphql_name == "deprecated"
kwarg_values_by_name_location = directives_by_location.each_with_object({}) do |(location, directive), memo|
directive.arguments.keyword_arguments.each do |key, value|
key = key.to_s
memo[key] ||= {}
memo[key][location] = value
end
end
if directive_class.graphql_name == GraphQL::Stitching.visibility_directive
unless GraphQL::Stitching.supports_visibility?
raise CompositionError, "Using `@#{GraphQL::Stitching.visibility_directive}` directive " \
"for schema visibility controls requires GraphQL Ruby v#{GraphQL::Stitching::MIN_VISIBILITY_VERSION} or later."
end
if (profiles = kwarg_values_by_name_location["profiles"])
@visibility_profiles.merge(profiles.each_value.reduce(&:|))
kwarg_formatter = Formatter::Default
end
end
kwargs = kwarg_values_by_name_location.each_with_object({}) do |(kwarg_name, kwarg_values_by_location), memo|
memo[kwarg_name.to_sym] = kwarg_formatter.merge_kwargs(kwarg_values_by_location, Formatter::Info.new(
type_name: type_name,
field_name: field_name,
argument_name: argument_name,
enum_value: enum_value,
directive_name: directive_name,
kwarg_name: kwarg_name,
))
end
owner.directive(directive_class, **kwargs)
end
end
# @!scope class
# @!visibility private
def merge_value_types(type_name, subgraph_types, field_name: nil, argument_name: nil)
path = [type_name, field_name, argument_name].tap(&:compact!).join(".")
alt_structures = subgraph_types.map { Util.flatten_type_structure(_1) }
basis_structure = alt_structures.shift
alt_structures.each do |alt_structure|
if alt_structure.length != basis_structure.length
raise CompositionError, "Cannot compose mixed list structures at `#{path}`."
end
if alt_structure.last.name != basis_structure.last.name
raise CompositionError, "Cannot compose mixed types at `#{path}`."
end
end
type = GraphQL::Schema::BUILT_IN_TYPES.fetch(
basis_structure.last.name,
build_type_binding(basis_structure.last.name)
)
basis_structure.reverse!.each_with_index do |basis, index|
rev_index = basis_structure.length - index - 1
non_null = alt_structures.each_with_object([basis.non_null?]) { |s, m| m << s[rev_index].non_null? }
type = type.to_list_type if basis.list?
type = type.to_non_null_type if argument_name ? non_null.any? : non_null.all?
end
type
end
# @!scope class
# @!visibility private
def merge_descriptions(type_name, members_by_location, field_name: nil, argument_name: nil, enum_value: nil)
strings_by_location = members_by_location.each_with_object({}) { |(l, m), memo| memo[l] = m.description }
@formatter.merge_descriptions(strings_by_location, Formatter::Info.new(
type_name: type_name,
field_name: field_name,
field_scopes: field_name ? @authorizations_by_type_and_field.dig(type_name, field_name) : nil,
argument_name: argument_name,
enum_value: enum_value,
))
end
# @!scope class
# @!visibility private
def merge_deprecations(type_name, members_by_location, field_name: nil, argument_name: nil, enum_value: nil)
strings_by_location = members_by_location.each_with_object({}) { |(l, m), memo| memo[l] = m.deprecation_reason }
@formatter.merge_deprecations(strings_by_location, Formatter::Info.new(
type_name: type_name,
field_name: field_name,
argument_name: argument_name,
enum_value: enum_value,
))
end
# @!scope class
# @!visibility private
def extract_resolvers(type_name, types_by_location)
types_by_location.each do |location, subgraph_type|
subgraph_type.fields.each do |field_name, subgraph_field|
resolver_type = subgraph_field.type.unwrap
resolver_structure = Util.flatten_type_structure(subgraph_field.type)
resolver_configs = @resolver_configs.fetch("#{location}.#{field_name}", [])
subgraph_field.directives.each do |directive|
next unless directive.graphql_name == GraphQL::Stitching.stitch_directive
resolver_configs << TypeResolverConfig.from_kwargs(directive.arguments.keyword_arguments)
end
resolver_configs.each do |config|
resolver_type_name = if config.type_name
if !resolver_type.kind.abstract?
raise CompositionError, "Type resolver config may only specify a type name for abstract resolvers."
elsif !resolver_type.possible_types.find { _1.graphql_name == config.type_name }
raise CompositionError, "Type `#{config.type_name}` is not a possible return type for query `#{field_name}`."
end
config.type_name
else
resolver_type.graphql_name
end
key = TypeResolver.parse_key_with_types(
config.key,
@subgraph_types_by_name_and_location[resolver_type_name],
)
arguments_format = config.arguments || begin
argument = if subgraph_field.arguments.size == 1
subgraph_field.arguments.values.first
else
subgraph_field.arguments[key.primitive_name]
end
unless argument
raise CompositionError, "No resolver argument matched for `#{type_name}.#{field_name}`." \
"An argument mapping is required for unmatched names and composite keys."
end
"#{argument.graphql_name}: $.#{key.primitive_name}"
end
arguments = TypeResolver.parse_arguments_with_field(arguments_format, subgraph_field)
arguments.each { _1.verify_key(key) }
@resolver_map[resolver_type_name] ||= []
@resolver_map[resolver_type_name] << TypeResolver.new(
location: location,
type_name: resolver_type_name,
field: subgraph_field.name,
list: resolver_structure.first.list?,
key: key,
arguments: arguments,
)
end
end
end
end
# @!scope class
# @!visibility private
def select_root_field_locations(schema)
[schema.query, schema.mutation, schema.subscription].tap(&:compact!).each do |root_type|
root_type.fields.each do |root_field_name, root_field|
root_field_locations = @field_map[root_type.graphql_name][root_field_name]
next unless root_field_locations.length > 1
root_field_path = "#{root_type.graphql_name}.#{root_field_name}"
target_location = @root_entrypoints[root_field_path] || root_field_locations.last
unless root_field_locations.include?(target_location)
raise CompositionError, "Invalid `root_entrypoints` configuration: `#{root_field_path}` has no `#{target_location}` location."
end
root_field_locations.reject! { _1 == target_location }
root_field_locations.unshift(target_location)
end
end
end
# @!scope class
# @!visibility private
def expand_abstract_resolvers(composed_schema, schemas_by_location)
@resolver_map.keys.each do |type_name|
next unless composed_schema.get_type(type_name).kind.abstract?
@resolver_map[type_name].each do |resolver|
abstract_type = @subgraph_types_by_name_and_location[type_name][resolver.location]
expanded_types = Util.expand_abstract_type(schemas_by_location[resolver.location], abstract_type)
expanded_types.select { @subgraph_types_by_name_and_location[_1.graphql_name].length > 1 }.each do |impl_type|
@resolver_map[impl_type.graphql_name] ||= []
@resolver_map[impl_type.graphql_name].push(resolver)
end
end
end
end
# @!scope class
# @!visibility private
def build_enum_usage_map(schemas)
reads = []
writes = []
schemas.each do |schema|
schema.types.each_value do |type|
next if type.introspection?
if type.kind.object? || type.kind.interface?
type.fields.each_value do |field|
field_type = field.type.unwrap
reads << field_type.graphql_name if field_type.kind.enum?
field.arguments.each_value do |argument|
argument_type = argument.type.unwrap
writes << argument_type.graphql_name if argument_type.kind.enum?
end
end
elsif type.kind.input_object?
type.arguments.each_value do |argument|
argument_type = argument.type.unwrap
writes << argument_type.graphql_name if argument_type.kind.enum?
end
end
end
end
usage = reads.tap(&:uniq!).each_with_object({}) do |enum_name, memo|
memo[enum_name] ||= []
memo[enum_name] << :read
end
writes.tap(&:uniq!).each_with_object(usage) do |enum_name, memo|
memo[enum_name] ||= []
memo[enum_name] << :write
end
end
def apply_supergraph_directives(schema, resolvers_by_type_name, locations_by_type_and_field)
schema_directives = {}
schema.types.each do |type_name, type|
if resolvers_for_type = resolvers_by_type_name.dig(type_name)
# Apply key directives for each unique type/key/location
# (this allows keys to be composite selections and/or omitted from the supergraph schema)
keys_for_type = resolvers_for_type.each_with_object({}) do |resolver, memo|
memo[resolver.key.to_definition] ||= Set.new
memo[resolver.key.to_definition].merge(resolver.key.locations)
end
keys_for_type.each do |key, locations|
locations.each do |location|
schema_directives[Directives::SupergraphKey.graphql_name] ||= Directives::SupergraphKey
type.directive(Directives::SupergraphKey, key: key, location: location)
end
end
# Apply resolver directives for each unique query resolver
resolvers_for_type.each do |resolver|
params = {
location: resolver.location,
field: resolver.field,
list: resolver.list? || nil,
key: resolver.key.to_definition,
arguments: resolver.arguments.map(&:to_definition).join(", "),
argument_types: resolver.arguments.map(&:to_type_definition).join(", "),
type_name: (resolver.type_name if resolver.type_name != type_name),
}
schema_directives[Directives::SupergraphResolver.graphql_name] ||= Directives::SupergraphResolver
type.directive(Directives::SupergraphResolver, **params.tap(&:compact!))
end
end
next unless type.kind.fields? && !type.introspection?
type.fields.each do |field_name, field|
if field.owner != type
# make a local copy of fields inherited from an interface
# to assure that source attributions reflect the object, not the interface.
field = type.field(
field.graphql_name,
description: field.description,
deprecation_reason: field.deprecation_reason,
type: Util.unwrap_non_null(field.type),
null: !field.type.non_null?,
connection: false,
camelize: false,
)
end
locations_for_field = locations_by_type_and_field.dig(type_name, field_name)
next if locations_for_field.nil?
# Apply source directives to annotate the possible locations of each field
locations_for_field.each do |location|
schema_directives[Directives::SupergraphSource.graphql_name] ||= Directives::SupergraphSource
field.directive(Directives::SupergraphSource, location: location)
end
end
end
schema_directives.each_value { |directive_class| schema.directive(directive_class) }
end
def apply_authorization_directives(schema)
return if @authorizations_by_type_and_field.empty?
schema.types.each_value do |type|
authorizations_by_field = @authorizations_by_type_and_field[type.graphql_name]
next if authorizations_by_field.nil? || !type.kind.fields?
type.fields.each_value do |field|
scopes = authorizations_by_field[field.graphql_name]
next if scopes.nil?
field.directive(Directives::Authorization, scopes: scopes)
end
end
schema.directive(Directives::Authorization)
end
end
end
end