Class: GraphQL::Stitching::Executor::RootSource

Inherits:
Dataloader::Source
  • Object
show all
Defined in:
lib/graphql/stitching/executor.rb

Instance Method Summary collapse

Constructor Details

#initialize(executor) ⇒ RootSource

Returns a new instance of RootSource.



10
11
12
# File 'lib/graphql/stitching/executor.rb', line 10

def initialize(executor)
  @executor = executor
end

Instance Method Details

#build_query(op) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/graphql/stitching/executor.rb', line 30

def build_query(op)
  if op["variables"].any?
    variable_defs = op["variables"].map { |k, v| "$#{k}:#{v}" }.join(",")
    "#{op["operation_type"]}(#{variable_defs})#{op["selections"]}"
  else
    "#{op["operation_type"]}#{op["selections"]}"
  end
end

#fetch(ops) ⇒ Object



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

def fetch(ops)
  op = ops.first # There should only ever be one per location at a time

  query_document = build_query(op)
  query_variables = @executor.variables.slice(*op["variables"].keys)
  result = @executor.supergraph.execute_at_location(op["location"], query_document, query_variables)
  @executor.query_count += 1

  @executor.data.merge!(result["data"]) if result["data"]
  if result["errors"]&.any?
    result["errors"].each { _1.delete("locations") }
    @executor.errors.concat(result["errors"])
  end
  op["key"]
end