Class: GraphQL::Stitching::Gateway

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

Defined Under Namespace

Classes: GatewayError

Constant Summary collapse

EMPTY_CONTEXT =
{}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locations: nil, supergraph: nil) ⇒ Gateway

Returns a new instance of Gateway.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/graphql/stitching/gateway.rb', line 14

def initialize(locations: nil, supergraph: nil)
  @supergraph = if locations && supergraph
    raise GatewayError, "Cannot provide both locations and a supergraph."
  elsif supergraph && !supergraph.is_a?(Supergraph)
    raise GatewayError, "Provided supergraph must be a GraphQL::Stitching::Supergraph instance."
  elsif supergraph
    supergraph
  elsif locations
    build_supergraph_from_locations_config(locations)
  else
    raise GatewayError, "No locations or supergraph provided."
  end
end

Instance Attribute Details

#supergraphObject (readonly)

Returns the value of attribute supergraph.



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

def supergraph
  @supergraph
end

Instance Method Details

#execute(query:, variables: nil, operation_name: nil, context: EMPTY_CONTEXT, validate: true) ⇒ Object



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

def execute(query:, variables: nil, operation_name: nil, context: EMPTY_CONTEXT, validate: true)
  document = GraphQL::Stitching::Document.new(query, operation_name: operation_name)

  if validate
    validation_errors = @supergraph.schema.validate(document.ast)
    return error_result(validation_errors) if validation_errors.any?
  end

  begin
    plan = fetch_plan(document, context) do
      GraphQL::Stitching::Planner.new(
        supergraph: @supergraph,
        document: document,
      ).perform.to_h
    end

    GraphQL::Stitching::Executor.new(
      supergraph: @supergraph,
      plan: plan,
      variables: variables || {},
    ).perform(document)
  rescue StandardError => e
    custom_message = @on_error.call(e, context) if @on_error
    error_result([{ "message" => custom_message || "An unexpected error occured." }])
  end
end

#on_cache_read(&block) ⇒ Object

Raises:



55
56
57
58
# File 'lib/graphql/stitching/gateway.rb', line 55

def on_cache_read(&block)
  raise GatewayError, "A cache read block is required." unless block_given?
  @on_cache_read = block
end

#on_cache_write(&block) ⇒ Object

Raises:



60
61
62
63
# File 'lib/graphql/stitching/gateway.rb', line 60

def on_cache_write(&block)
  raise GatewayError, "A cache write block is required." unless block_given?
  @on_cache_write = block
end

#on_error(&block) ⇒ Object

Raises:



65
66
67
68
# File 'lib/graphql/stitching/gateway.rb', line 65

def on_error(&block)
  raise GatewayError, "An error handler block is required." unless block_given?
  @on_error = block
end