Module: GraphQL::Stitching

Defined in:
lib/graphql/stitching.rb,
lib/graphql/stitching/util.rb,
lib/graphql/stitching/shaper.rb,
lib/graphql/stitching/gateway.rb,
lib/graphql/stitching/planner.rb,
lib/graphql/stitching/request.rb,
lib/graphql/stitching/version.rb,
lib/graphql/stitching/composer.rb,
lib/graphql/stitching/executor.rb,
lib/graphql/stitching/supergraph.rb,
lib/graphql/stitching/remote_client.rb,
lib/graphql/stitching/planner_operation.rb,
lib/graphql/stitching/composer/base_validator.rb,
lib/graphql/stitching/composer/validate_boundaries.rb,
lib/graphql/stitching/composer/validate_interfaces.rb

Defined Under Namespace

Classes: Composer, Executor, Gateway, Planner, PlannerOperation, RemoteClient, Request, Shaper, StitchingError, Supergraph, Util

Constant Summary collapse

EMPTY_OBJECT =
{}.freeze
VERSION =
"0.2.3"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.stitch_directiveObject



13
14
15
# File 'lib/graphql/stitching.rb', line 13

def stitch_directive
  @stitch_directive ||= "stitch"
end

Class Method Details

.schema_from_definition(sdl, stitch_directives:) ⇒ Object



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

def schema_from_definition(sdl, stitch_directives:)
  ast = GraphQL.parse(sdl)

  if stitch_directives&.any?
    directive_definition = ast.definitions.find do |d|
      d.is_a?(GraphQL::Language::Nodes::DirectiveDefinition) && d.name == stitch_directive
    end

    if !directive_definition
      directive_sdl = "directive @#{stitch_directive}(key: String!) repeatable on FIELD_DEFINITION"
      directive_definition = GraphQL.parse(directive_sdl).definitions.first
      ast.send(:merge!, { definitions: [directive_definition, *ast.definitions] })
    end
  end

  stitch_directives.each do |config|
    config[:type_name] ||= "Query"

    type_node = ast.definitions.find do |d|
      d.is_a?(GraphQL::Language::Nodes::ObjectTypeDefinition) && d.name == config[:type_name]
    end

    raise StitchingError, "invalid type name `#{config[:type_name]}`." unless type_node

    field_node = type_node.fields.find do |f|
      f.name == config[:field_name]
    end

    raise StitchingError, "invalid field name `#{config[:field_name]}`." unless field_node

    field_node.send(:merge!, {
      directives: [
        *field_node.directives,
        GraphQL::Language::Nodes::Directive.new(
          arguments: [GraphQL::Language::Nodes::Argument.new(name: "key", value: config[:key])],
          name: stitch_directive,
        )
      ]
    })
  end

  if GraphQL::Schema::BuildFromDefinition.method(:from_document).parameters.first.last == :document
    # GraphQL v1.13.x
    GraphQL::Schema::BuildFromDefinition.from_document(ast, default_resolve: nil)
  else
    # GraphQL v2
    GraphQL::Schema::BuildFromDefinition.from_document(GraphQL::Schema, ast, default_resolve: nil)
  end
end

.stitching_directive_namesObject



19
20
21
# File 'lib/graphql/stitching.rb', line 19

def stitching_directive_names
  [stitch_directive]
end