Class: GraphQL::Stitching::Composer

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/stitching/composer.rb

Defined Under Namespace

Classes: BaseValidator, ComposerError, ValidateBoundaries, ValidateInterfaces, ValidationError

Constant Summary collapse

DEFAULT_VALUE_MERGER =
->(values_by_location, _info) { values_by_location.values.find { !_1.nil? } }
VALIDATORS =
[
  "ValidateInterfaces",
  "ValidateBoundaries",
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_name: "Query", mutation_name: "Mutation", description_merger: nil, deprecation_merger: nil, directive_kwarg_merger: nil) ⇒ Composer

Returns a new instance of Composer.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/graphql/stitching/composer.rb', line 18

def initialize(
  query_name: "Query",
  mutation_name: "Mutation",
  description_merger: nil,
  deprecation_merger: nil,
  directive_kwarg_merger: nil
)
  @query_name = query_name
  @mutation_name = mutation_name
  @description_merger = description_merger || DEFAULT_VALUE_MERGER
  @deprecation_merger = deprecation_merger || DEFAULT_VALUE_MERGER
  @directive_kwarg_merger = directive_kwarg_merger || DEFAULT_VALUE_MERGER
end

Instance Attribute Details

#mutation_nameObject (readonly)

Returns the value of attribute mutation_name.



9
10
11
# File 'lib/graphql/stitching/composer.rb', line 9

def mutation_name
  @mutation_name
end

#query_nameObject (readonly)

Returns the value of attribute query_name.



9
10
11
# File 'lib/graphql/stitching/composer.rb', line 9

def query_name
  @query_name
end

#schema_directivesObject (readonly)

Returns the value of attribute schema_directives.



9
10
11
# File 'lib/graphql/stitching/composer.rb', line 9

def schema_directives
  @schema_directives
end

#subschema_types_by_name_and_locationObject (readonly)

Returns the value of attribute subschema_types_by_name_and_location.



9
10
11
# File 'lib/graphql/stitching/composer.rb', line 9

def subschema_types_by_name_and_location
  @subschema_types_by_name_and_location
end

Instance Method Details

#build_directive(directive_name, directives_by_location) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/graphql/stitching/composer.rb', line 169

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).uniq)
    builder.build_merged_arguments(directive_name, directives_by_location, self)
  end
end

#build_enum_type(type_name, types_by_location, enum_usage) ⇒ Object



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
# File 'lib/graphql/stitching/composer.rb', line 194

def build_enum_type(type_name, types_by_location, enum_usage)
  builder = self

  # "value" => "location" => enum_value
  enum_values_by_value_location = types_by_location.each_with_object({}) do |(location, type_candidate), memo|
    type_candidate.enum_values.each do |enum_value_candidate|
      memo[enum_value_candidate.value] ||= {}
      memo[enum_value_candidate.value][location] ||= {}
      memo[enum_value_candidate.value][location] = enum_value_candidate
    end
  end

  # intersect input enum types
  if enum_usage.fetch(type_name, []).include?(:write)
    enum_values_by_value_location.reject! do |value, enum_values_by_location|
      types_by_location.keys.length != enum_values_by_location.keys.length
    end
  end

  Class.new(GraphQL::Schema::Enum) 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_value_location.each do |value, enum_values_by_location|
      enum_value = value(value,
        value: value,
        description: builder.merge_descriptions(type_name, enum_values_by_location, enum_value: value),
        deprecation_reason: builder.merge_deprecations(type_name, enum_values_by_location, enum_value: value),
      )

      builder.build_merged_directives(type_name, enum_values_by_location, enum_value, enum_value: value)
    end
  end
end

#build_enum_usage_map(schemas) ⇒ Object



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
# File 'lib/graphql/stitching/composer.rb', line 517

def build_enum_usage_map(schemas)
  reads = []
  writes = []

  schemas.each do |schema|
    schema.types.values.each do |type|
      next if Supergraph::INTROSPECTION_TYPES.include?(type.graphql_name)

      if type.kind.object? || type.kind.interface?
        type.fields.values.each do |field|
          field_type = field.type.unwrap
          reads << field_type.graphql_name if field_type.kind.enum?

          field.arguments.values.each 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.values.each do |argument|
          argument_type = argument.type.unwrap
          writes << argument_type.graphql_name if argument_type.kind.enum?
        end
      end
    end
  end

  usage = reads.uniq.each_with_object({}) do |enum_name, memo|
    memo[enum_name] ||= []
    memo[enum_name] << :read
  end
  writes.uniq.each_with_object(usage) do |enum_name, memo|
    memo[enum_name] ||= []
    memo[enum_name] << :write
  end
end

#build_input_object_type(type_name, types_by_location) ⇒ Object



278
279
280
281
282
283
284
285
286
287
# File 'lib/graphql/stitching/composer.rb', line 278

def build_input_object_type(type_name, types_by_location)
  builder = self

  Class.new(GraphQL::Schema::InputObject) 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

#build_interface_type(type_name, types_by_location) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/graphql/stitching/composer.rb', line 247

def build_interface_type(type_name, types_by_location)
  builder = self

  Module.new do
    include GraphQL::Schema::Interface
    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.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

#build_merged_arguments(type_name, members_by_location, owner, field_name: nil) ⇒ Object



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
# File 'lib/graphql/stitching/composer.rb', line 324

def build_merged_arguments(type_name, members_by_location, owner, field_name: nil)
  # "argument_name" => "location" => argument
  args_by_name_location = members_by_location.each_with_object({}) do |(location, member_candidate), memo|
    member_candidate.arguments.each do |argument_name, argument|
      memo[argument_name] ||= {}
      memo[argument_name][location] ||= {}
      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 ComposerError, "Required argument `#{path}` must be defined in all locations." # ...or hidden?
      end
      next
    end

    # Getting double args sometimes... why?
    return if owner.arguments.any? { _1.first == argument_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: merge_value_types(type_name, value_types, argument_name: argument_name, field_name: field_name),
      required: value_types.any?(&:non_null?),
      camelize: false,
    )

    build_merged_directives(type_name, arguments_by_location, schema_argument, field_name: field_name, argument_name: argument_name)
  end
end

#build_merged_directives(type_name, members_by_location, owner, field_name: nil, argument_name: nil, enum_value: nil) ⇒ Object



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
# File 'lib/graphql/stitching/composer.rb', line 361

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, member_candidate), memo|
    member_candidate.directives.each do |directive|
      memo[directive.graphql_name] ||= {}
      memo[directive.graphql_name][location] ||= {}
      memo[directive.graphql_name][location] = directive
    end
  end

  directives_by_name_location.each do |directive_name, directives_by_location|
    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
        next unless directive_class.arguments[key]

        memo[key] ||= {}
        memo[key][location] = value
      end
    end

    kwargs = kwarg_values_by_name_location.each_with_object({}) do |(kwarg_name, kwarg_values_by_location), memo|
      memo[kwarg_name.to_sym] = @directive_kwarg_merger.call(kwarg_values_by_location, {
        type_name: type_name,
        field_name: field_name,
        argument_name: argument_name,
        enum_value: enum_value,
        directive_name: directive_name,
        kwarg_name: kwarg_name,
      }.compact!)
    end

    owner.directive(directive_class, **kwargs)
  end
end

#build_merged_fields(type_name, types_by_location, owner) ⇒ Object



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
# File 'lib/graphql/stitching/composer.rb', line 293

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, type_candidate), memo|
    @field_map[type_name] ||= {}
    type_candidate.fields.each do |field_name, field_candidate|
      @field_map[type_name][field_candidate.name] ||= []
      @field_map[type_name][field_candidate.name] << location

      memo[field_name] ||= {}
      memo[field_name][location] ||= {}
      memo[field_name][location] = field_candidate
    end
  end

  fields_by_name_location.each do |field_name, fields_by_location|
    value_types = fields_by_location.values.map(&:type)

    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: merge_value_types(type_name, value_types, field_name: field_name),
      null: !value_types.all?(&:non_null?),
      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

#build_object_type(type_name, types_by_location) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/graphql/stitching/composer.rb', line 230

def build_object_type(type_name, types_by_location)
  builder = self

  Class.new(GraphQL::Schema::Object) 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.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

#build_scalar_type(type_name, types_by_location) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/graphql/stitching/composer.rb', line 181

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::Schema::Scalar) 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

#build_type_binding(type_name) ⇒ Object



289
290
291
# File 'lib/graphql/stitching/composer.rb', line 289

def build_type_binding(type_name)
  GraphQL::Schema::LateBoundType.new(@mapped_type_names.fetch(type_name, type_name))
end

#build_union_type(type_name, types_by_location) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/graphql/stitching/composer.rb', line 265

def build_union_type(type_name, types_by_location)
  builder = self

  Class.new(GraphQL::Schema::Union) 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) }.uniq
    possible_types(*possible_names.map { builder.build_type_binding(_1) })
    builder.build_merged_directives(type_name, types_by_location, self)
  end
end

#expand_abstract_boundaries(schema) ⇒ Object



504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/graphql/stitching/composer.rb', line 504

def expand_abstract_boundaries(schema)
  @boundary_map.keys.each do |type_name|
    boundary_type = schema.types[type_name]
    next unless boundary_type.kind.abstract?

    expanded_types = Util.expand_abstract_type(schema, boundary_type)
    expanded_types.select { @subschema_types_by_name_and_location[_1.graphql_name].length > 1 }.each do |expanded_type|
      @boundary_map[expanded_type.graphql_name] ||= []
      @boundary_map[expanded_type.graphql_name].push(*@boundary_map[type_name])
    end
  end
end

#extract_boundaries(type_name, types_by_location) ⇒ Object



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
# File 'lib/graphql/stitching/composer.rb', line 458

def extract_boundaries(type_name, types_by_location)
  types_by_location.each do |location, type_candidate|
    type_candidate.fields.each do |field_name, field_candidate|
      boundary_type_name = field_candidate.type.unwrap.graphql_name
      boundary_list = Util.get_list_structure(field_candidate.type)

      field_candidate.directives.each do |directive|
        next unless directive.graphql_name == GraphQL::Stitching.stitch_directive

        key = directive.arguments.keyword_arguments.fetch(:key)
        key_selections = GraphQL.parse("{ #{key} }").definitions[0].selections

        if key_selections.length != 1
          raise ComposerError, "Boundary key at #{type_name}.#{field_name} must specify exactly one key."
        end

        argument_name = key_selections[0].alias
        argument_name ||= if field_candidate.arguments.size == 1
          field_candidate.arguments.keys.first
        end

        argument = field_candidate.arguments[argument_name]
        unless argument
          # contextualize this... "boundaries with multiple args need mapping aliases."
          raise ComposerError, "Invalid boundary argument `#{argument_name}` for #{type_name}.#{field_name}."
        end

        argument_list = Util.get_list_structure(argument.type)
        if argument_list.length != boundary_list.length
          raise ComposerError, "Mismatched input/output for #{type_name}.#{field_name}.#{argument_name} boundary. Arguments must map directly to results."
        end

        @boundary_map[boundary_type_name] ||= []
        @boundary_map[boundary_type_name] << {
          "location" => location,
          "selection" => key_selections[0].name,
          "field" => field_candidate.name,
          "arg" => argument_name,
          "list" => boundary_list.any?,
          "type_name" => boundary_type_name,
        }
      end
    end
  end
end

#merge_deprecations(type_name, members_by_location, field_name: nil, argument_name: nil, enum_value: nil) ⇒ Object



448
449
450
451
452
453
454
455
456
# File 'lib/graphql/stitching/composer.rb', line 448

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 }
  @deprecation_merger.call(strings_by_location, {
    type_name: type_name,
    field_name: field_name,
    argument_name: argument_name,
    enum_value: enum_value,
  }.compact!)
end

#merge_descriptions(type_name, members_by_location, field_name: nil, argument_name: nil, enum_value: nil) ⇒ Object



438
439
440
441
442
443
444
445
446
# File 'lib/graphql/stitching/composer.rb', line 438

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 }
  @description_merger.call(strings_by_location, {
    type_name: type_name,
    field_name: field_name,
    argument_name: argument_name,
    enum_value: enum_value,
  }.compact!)
end

#merge_value_types(type_name, type_candidates, field_name: nil, argument_name: nil) ⇒ Object



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
# File 'lib/graphql/stitching/composer.rb', line 402

def merge_value_types(type_name, type_candidates, field_name: nil, argument_name: nil)
  path = [type_name, field_name, argument_name].compact.join(".")
  named_types = type_candidates.map { _1.unwrap.graphql_name }.uniq

  unless named_types.all? { _1 == named_types.first }
    raise ComposerError, "Cannot compose mixed types at `#{path}`. Found: #{named_types.join(", ")}."
  end

  type = GraphQL::Schema::BUILT_IN_TYPES.fetch(named_types.first, build_type_binding(named_types.first))
  list_structures = type_candidates.map { Util.get_list_structure(_1) }

  if list_structures.any?(&:any?)
    if list_structures.any? { _1.length != list_structures.first.length }
      raise ComposerError, "Cannot compose mixed list structures at `#{path}`."
    end

    list_structures.each(&:reverse!)
    list_structures.first.each_with_index do |current, index|
      # input arguments use strongest nullability, readonly fields use weakest
      non_null = list_structures.public_send(argument_name ? :any? : :all?) do |list_structure|
        list_structure[index].start_with?("non_null")
      end

      case current
      when "list", "non_null_list"
        type = type.to_list_type
        type = type.to_non_null_type if non_null
      when "element", "non_null_element"
        type = type.to_non_null_type if non_null
      end
    end
  end

  type
end

#perform(locations_input) ⇒ Object



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
# File 'lib/graphql/stitching/composer.rb', line 32

def perform(locations_input)
  reset!
  schemas, executables = prepare_locations_input(locations_input)

  # "directive_name" => "location" => candidate_directive
  @subschema_directives_by_name_and_location = schemas.each_with_object({}) do |(location, schema), memo|
    (schema.directives.keys - schema.default_directives.keys - GraphQL::Stitching.stitching_directive_names).each do |directive_name|
      memo[directive_name] ||= {}
      memo[directive_name][location] = schema.directives[directive_name]
    end
  end

  # "Typename" => merged_directive
  @schema_directives = @subschema_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" => candidate_type
  @subschema_types_by_name_and_location = schemas.each_with_object({}) do |(location, schema), memo|
    raise ComposerError, "Location keys must be strings" unless location.is_a?(String)
    raise ComposerError, "The subscription operation is not supported." if schema.subscription

    schema.types.each do |type_name, type_candidate|
      next if Supergraph::INTROSPECTION_TYPES.include?(type_name)

      if type_name == @query_name && type_candidate != schema.query
        raise ComposerError, "Query name \"#{@query_name}\" is used by non-query type in #{location} schema."
      elsif type_name == @mutation_name && type_candidate != schema.mutation
        raise ComposerError, "Mutation name \"#{@mutation_name}\" is used by non-mutation type in #{location} schema."
      end

      type_name = @query_name if type_candidate == schema.query
      type_name = @mutation_name if type_candidate == schema.mutation
      @mapped_type_names[type_candidate.graphql_name] = type_name if type_candidate.graphql_name != type_name

      memo[type_name] ||= {}
      memo[type_name][location] = type_candidate
    end
  end

  enum_usage = build_enum_usage_map(schemas.values)

  # "Typename" => merged_type
  schema_types = @subschema_types_by_name_and_location.each_with_object({}) do |(type_name, types_by_location), memo|
    kinds = types_by_location.values.map { _1.kind.name }.uniq

    unless kinds.all? { _1 == kinds.first }
      raise ComposerError, "Cannot merge different kinds for `#{type_name}`. Found: #{kinds.join(", ")}."
    end

    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"
      extract_boundaries(type_name, types_by_location)
      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 ComposerError, "Unexpected kind encountered for `#{type_name}`. Found: #{kind}."
    end
  end

  builder = self
  schema = Class.new(GraphQL::Schema) do
    orphan_types schema_types.values
    query schema_types[builder.query_name]
    mutation schema_types[builder.mutation_name]
    directives builder.schema_directives.values

    own_orphan_types.clear
  end

  expand_abstract_boundaries(schema)

  supergraph = Supergraph.new(
    schema: schema,
    fields: @field_map,
    boundaries: @boundary_map,
    executables: executables,
  )

  VALIDATORS.each do |validator|
    klass = Object.const_get("GraphQL::Stitching::Composer::#{validator}")
    klass.new.perform(supergraph, self)
  end

  supergraph
end

#prepare_locations_input(locations_input) ⇒ Object



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
# File 'lib/graphql/stitching/composer.rb', line 130

def prepare_locations_input(locations_input)
  schemas = {}
  executables = {}

  locations_input.each do |location, input|
    schema = input[:schema]

    if schema.nil?
      raise ComposerError, "A schema is required for `#{location}` location."
    elsif !(schema.is_a?(Class) && schema <= GraphQL::Schema)
      raise ComposerError, "The schema for `#{location}` location must be a GraphQL::Schema class."
    end

    if input[:stitch]
      stitch_directive = Class.new(GraphQL::Schema::Directive) do
        graphql_name(GraphQL::Stitching.stitch_directive)
        locations :FIELD_DEFINITION
        argument :key, String
        repeatable true
      end

      input[:stitch].each do |dir|
        type = dir[:type_name] ? schema.types[dir[:type_name]] : schema.query
        raise ComposerError, "Invalid stitch directive type `#{dir[:type_name]}`" unless type

        field = type.fields[dir[:field_name]]
        raise ComposerError, "Invalid stitch directive field `#{dir[:field_name]}`" unless field

        field.directive(stitch_directive, **dir.slice(:key))
      end
    end

    schemas[location.to_s] = schema
    executables[location.to_s] = input[:executable] || schema
  end

  return schemas, executables
end