Class: GraphQL::Stitching::Executor::BoundarySource

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

Instance Method Summary collapse

Constructor Details

#initialize(executor, location) ⇒ BoundarySource

Returns a new instance of BoundarySource.

[View source]

41
42
43
44
# File 'lib/graphql/stitching/executor.rb', line 41

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

Instance Method Details

#build_query(origin_sets_by_operation) ⇒ Object

[View source]

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/graphql/stitching/executor.rb', line 77

def build_query(origin_sets_by_operation)
  variable_defs = {}
  query_fields = origin_sets_by_operation.map.with_index do |(op, origin_set), batch_index|
    variable_defs.merge!(op["variables"])
    boundary = op["boundary"]
    key_selection = "_STITCH_#{boundary["selection"]}"

    if boundary["list"]
      input = JSON.generate(origin_set.map { _1[key_selection] })
      "_#{batch_index}_result: #{boundary["field"]}(#{boundary["arg"]}:#{input}) #{op["selections"]}"
    else
      origin_set.map.with_index do |origin_obj, index|
        input = JSON.generate(origin_obj[key_selection])
        "_#{batch_index}_#{index}_result: #{boundary["field"]}(#{boundary["arg"]}:#{input}) #{op["selections"]}"
      end
    end
  end

  query_document = if variable_defs.any?
    query_variables = variable_defs.map { |k, v| "$#{k}:#{v}" }.join(",")
    "query(#{query_variables}){ #{query_fields.join(" ")} }"
  else
    "query{ #{query_fields.join(" ")} }"
  end

  return query_document, variable_defs.keys
end

#extract_errors!(origin_sets_by_operation, errors) ⇒ Object

[View source]

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/graphql/stitching/executor.rb', line 124

def extract_errors!(origin_sets_by_operation, errors)
  ops = origin_sets_by_operation.keys
  origin_sets = origin_sets_by_operation.values
  pathed_errors_by_op_index_and_object_id = {}

  errors_result = errors.each_with_object([]) do |err, memo|
    err.delete("locations")
    path = err["path"]

    if path && path.length > 0
      result_alias = /^_(\d+)(?:_(\d+))?_result$/.match(path.first.to_s)

      if result_alias
        path = err["path"] = path[1..-1]

        origin_obj = if result_alias[2]
          origin_sets.dig(result_alias[1].to_i, result_alias[2].to_i)
        elsif path[0].is_a?(Integer) || /\d+/.match?(path[0].to_s)
          origin_sets.dig(result_alias[1].to_i, path.shift.to_i)
        end

        if origin_obj
          by_op_index = pathed_errors_by_op_index_and_object_id[result_alias[1].to_i] ||= {}
          by_object_id = by_op_index[origin_obj.object_id] ||= []
          by_object_id << err
          next
        end
      end
    end

    memo << err
  end

  if pathed_errors_by_op_index_and_object_id.any?
    pathed_errors_by_op_index_and_object_id.each do |op_index, pathed_errors_by_object_id|
      repath_errors!(pathed_errors_by_object_id, ops.dig(op_index, "insertion_path"))
      errors_result.concat(pathed_errors_by_object_id.values)
    end
  end
  errors_result.flatten!
end

#fetch(ops) ⇒ Object

[View source]

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
72
73
74
75
# File 'lib/graphql/stitching/executor.rb', line 46

def fetch(ops)
  origin_sets_by_operation = ops.each_with_object({}) do |op, memo|
    origin_set = op["insertion_path"].reduce([@executor.data]) do |set, path_segment|
      mapped = set.flat_map { |obj| obj && obj[path_segment] }
      mapped.compact!
      mapped
    end

    if op["type_condition"]
      # operations planned around unused fragment conditions should not trigger requests
      origin_set.select! { _1["_STITCH_typename"] == op["type_condition"] }
    end

    memo[op] = origin_set if origin_set.any?
  end

  if origin_sets_by_operation.any?
    query_document, variable_names = build_query(origin_sets_by_operation)
    variables = @executor.variables.slice(*variable_names)
    raw_result = @executor.supergraph.execute_at_location(@location, query_document, variables)
    @executor.query_count += 1

    merge_results!(origin_sets_by_operation, raw_result.dig("data"))

    errors = raw_result.dig("errors")
    @executor.errors.concat(extract_errors!(origin_sets_by_operation, errors)) if errors&.any?
  end

  ops.map { origin_sets_by_operation[_1] ? _1["key"] : nil }
end

#merge_results!(origin_sets_by_operation, raw_result) ⇒ Object

[View source]

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/graphql/stitching/executor.rb', line 105

def merge_results!(origin_sets_by_operation, raw_result)
  return unless raw_result

  origin_sets_by_operation.each_with_index do |(op, origin_set), batch_index|
    results = if op.dig("boundary", "list")
      raw_result["_#{batch_index}_result"]
    else
      origin_set.map.with_index { |_, index| raw_result["_#{batch_index}_#{index}_result"] }
    end

    next unless results&.any?

    origin_set.each_with_index do |origin_obj, index|
      origin_obj.merge!(results[index]) if results[index]
    end
  end
end