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.

[View source]

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 ... }"

[View source]

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/graphql/stitching/executor/root_source.rb', line 46

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

[View source]

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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.request.supergraph.execute_at_location(op.location, query_document, query_variables, @executor.request)
  @executor.query_count += 1

  if result["data"]
    if op.path.any?
      # Nested root scopes must expand their pathed origin set
      origin_set = op.path.reduce([@executor.data]) do |set, ns|
        set.flat_map { |obj| obj && obj[ns] }.tap(&:compact!)
      end

      origin_set.each { _1.merge!(result["data"]) }
    else
      # Actual root scopes merge directly into results data
      @executor.data.merge!(result["data"])
    end
  end

  if result["errors"]&.any?
    @executor.errors.concat(format_errors!(result["errors"], op.path))
  end

  ops.map(&:step)
end

#format_errors!(errors, path) ⇒ Object

Format response errors without a document location (because it won't match the request doc), and prepend any insertion path for the scope into error paths.

[View source]

69
70
71
72
73
74
75
# File 'lib/graphql/stitching/executor/root_source.rb', line 69

def format_errors!(errors, path)
  errors.each do |err|
    err.delete("locations")
    err["path"].unshift(*path) if err["path"] && path.any?
  end
  errors
end