Class: GraphQL::Stitching::Executor::TypeResolverSource

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

Instance Method Summary collapse

Constructor Details

#initialize(executor, location) ⇒ TypeResolverSource

Returns a new instance of TypeResolverSource.



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

def initialize(executor, location)
  @executor = executor
  @location = location
  @variables = {}
end

Instance Method Details

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

Builds batched resolver queries "query MyOperation_2_3($var:VarType, $_0_key:[ID!]!, $_1_0_key:ID!, $_1_1_key:ID!, $_1_2_key:ID!) { _0_result: list(keys: $_0_key) { resolverSelections... } _1_0_result: item(key: $_1_0_key) { resolverSelections... } _1_1_result: item(key: $_1_1_key) { resolverSelections... } _1_2_result: item(key: $_1_2_key) { resolverSelections... } }"



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/graphql/stitching/executor/type_resolver_source.rb', line 52

def build_document(origin_sets_by_operation, operation_name = nil, operation_directives = nil)
  variable_defs = {}
  fields_buffer = String.new

  origin_sets_by_operation.each_with_index do |(op, origin_set), batch_index|
    variable_defs.merge!(op.variables)
    resolver = @executor.request.supergraph.resolvers_by_version[op.resolver]
    fields_buffer << " " unless batch_index.zero?

    if resolver.list?
      fields_buffer << "_" << batch_index.to_s << "_result: " << resolver.field << "("

      resolver.arguments.each_with_index do |arg, i|
        fields_buffer << "," unless i.zero?
        if arg.key?
          variable_name = "_#{batch_index}_key_#{i}".freeze
          @variables[variable_name] = origin_set.map { arg.build(_1) }
          variable_defs[variable_name] = arg.to_type_signature
          fields_buffer << arg.name << ":$" << variable_name
        else
          fields_buffer << arg.name << ":" << arg.value.print
        end
      end

      fields_buffer << ") " << op.selections
    else
      origin_set.each_with_index do |origin_obj, index|
        fields_buffer << " " unless index.zero?
        fields_buffer << "_" << batch_index.to_s << "_" << index.to_s << "_result: " << resolver.field << "("

        resolver.arguments.each_with_index do |arg, i|
          fields_buffer << "," unless i.zero?
          if arg.key?
            variable_name = "_#{batch_index}_#{index}_key_#{i}".freeze
            @variables[variable_name] = arg.build(origin_obj)
            variable_defs[variable_name] = arg.to_type_signature
            fields_buffer << arg.name << ":$" << variable_name
          else
            fields_buffer << arg.name << ":" << arg.value.print
          end
        end

        fields_buffer << ") " << op.selections
      end
    end
  end

  doc_buffer = String.new(QUERY_OP) # << resolver fulfillment always uses query

  if operation_name
    doc_buffer << " " << operation_name
    origin_sets_by_operation.each_key do |op|
      doc_buffer << "_" << op.step.to_s
    end
  end

  unless variable_defs.empty?
    doc_buffer << "("
    variable_defs.each_with_index do |(k, v), i|
      doc_buffer << "," unless i.zero?
      doc_buffer << "$" << k << ":" << v
    end
    doc_buffer << ")"
  end

  if operation_directives
    doc_buffer << " " << operation_directives << " "
  end

  doc_buffer << "{ " << fields_buffer << " }"

  return doc_buffer, variable_defs.keys.tap do |names|
    names.reject! { @variables.key?(_1) }
  end
end

#extract_errors!(origin_sets_by_operation, errors) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/graphql/stitching/executor/type_resolver_source.rb', line 148

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 = Hash.new { |h, k| h[k] = {} }

  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
          pathed_errors_by_op_index = pathed_errors_by_op_index_and_object_id[result_alias[1].to_i]
          by_object_id = pathed_errors_by_op_index[origin_obj.object_id] ||= []
          by_object_id << err
          next
        end
      end
    end

    memo << err
  end

  unless pathed_errors_by_op_index_and_object_id.empty?
    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[op_index].path)
      errors_result.push(*pathed_errors_by_object_id.each_value)
    end
  end

  errors_result.tap(&:flatten!)
end

#fetch(ops) ⇒ Object



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
43
# File 'lib/graphql/stitching/executor/type_resolver_source.rb', line 12

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

    if op.if_type
      # operations planned around unused fragment conditions should not trigger requests
      origin_set.select! { _1[TypeResolver::TYPENAME_EXPORT_NODE.alias] == op.if_type }
    end

    memo[op] = origin_set unless origin_set.empty?
  end

  unless origin_sets_by_operation.empty?
    query_document, variable_names = build_document(
      origin_sets_by_operation,
      @executor.request.operation_name,
      @executor.request.operation_directives,
    )
    variables = @variables.merge!(@executor.request.variables.slice(*variable_names))
    raw_result = @executor.request.supergraph.execute_at_location(@location, query_document, variables, @executor.request)
    @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.step : nil }
end

#merge_results!(origin_sets_by_operation, raw_result) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/graphql/stitching/executor/type_resolver_source.rb', line 128

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 @executor.request.supergraph.resolvers_by_version[op.resolver].list?
      raw_result["_#{batch_index}_result"]
    else
      origin_set.map.with_index { |_, index| raw_result["_#{batch_index}_#{index}_result"] }
    end

    next if results.nil? || results.empty?

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