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

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

Instance Method Summary collapse

Constructor Details

#initialize(executor, location) ⇒ RootSource

Returns a new instance of RootSource.



6
7
8
9
# File 'lib/graphql/stitching/executor/root_source.rb', line 6

def initialize(executor, location)
  @executor = executor
  @location = location
end

Instance Method Details

#build_document(op, operation_name = nil, operation_directives = nil) ⇒ Object

Builds root source documents "query MyOperation_1($var:VarType) { rootSelections ... }"



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/graphql/stitching/executor/root_source.rb', line 34

def build_document(op, operation_name = nil, operation_directives = nil)
  doc = String.new
  doc << op.operation_type

  if operation_name
    doc << " #{operation_name}_#{op.step}"
  end

  if op.variables.any?
    variable_defs = op.variables.map { |k, v| "$#{k}:#{v}" }.join(",")
    doc << "(#{variable_defs})"
  end

  if operation_directives
    doc << " #{operation_directives} "
  end

  doc << op.selections
  doc
end

#fetch(ops) ⇒ Object



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

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

  query_document = build_document(
    op,
    @executor.request.operation_name,
    @executor.request.operation_directives,
  )
  query_variables = @executor.request.variables.slice(*op.variables.keys)
  result = @executor.supergraph.execute_at_location(op.location, query_document, query_variables, @executor.request)
  @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

  ops.map(&:step)
end