Module: GraphQL::Execution::Execute::ExecutionFunctions Private

Included in:
GraphQL::Execution::Execute
Defined in:
lib/graphql/execution/execute.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Class Method Summary collapse

Class Method Details

.continue_or_wait(raw_value, field_type, field_ctx) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

If the returned object is lazy (unfinished), assign the lazy object to .value= so we can resolve it later. When we resolve it later, reassign it to .value= so that the finished value replaces the unfinished one.

If the returned object is finished, continue to coerce and resolve child fields


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/graphql/execution/execute.rb', line 168

def continue_or_wait(raw_value, field_type, field_ctx)
  if field_ctx.schema.lazy?(raw_value)
    field_ctx.value = Execution::Lazy.new {
      inner_value = begin
          begin
            field_ctx.schema.sync_lazy(raw_value)
          rescue GraphQL::UnauthorizedError => err
            field_ctx.schema.unauthorized_object(err)
          end
        rescue GraphQL::ExecutionError => err
          err
        end

      field_ctx.value = continue_or_wait(inner_value, field_type, field_ctx)
    }
  else
    field_ctx.value = continue_resolve_field(raw_value, field_type, field_ctx)
  end
end

.continue_resolve_field(raw_value, field_type, field_ctx) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/graphql/execution/execute.rb', line 188

def continue_resolve_field(raw_value, field_type, field_ctx)
  if field_ctx.parent.invalid_null?
    return nil
  end
  query = field_ctx.query

  case raw_value
  when GraphQL::ExecutionError
    raw_value.ast_node ||= field_ctx.ast_node
    raw_value.path = field_ctx.path
    query.context.errors.push(raw_value)
  when Array
    if field_type.non_null?
      # List type errors are handled above, this is for the case of fields returning an array of errors
      list_errors = raw_value.each_with_index.select { |value, _| value.is_a?(GraphQL::ExecutionError) }
      if list_errors.any?
        list_errors.each do |error, index|
          error.ast_node = field_ctx.ast_node
          error.path = field_ctx.path + (field_ctx.type.list? ? [index] : [])
          query.context.errors.push(error)
        end
      end
    end
  end

  resolve_value(
    raw_value,
    field_type,
    field_ctx,
  )
end

.lazy_resolve_root_selection(result, query: nil, multiplex: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


66
67
68
69
70
71
72
73
74
75
# File 'lib/graphql/execution/execute.rb', line 66

def lazy_resolve_root_selection(result, query: nil, multiplex: nil)
  if query.nil? && multiplex.queries.length == 1
    query = multiplex.queries[0]
  end

  tracer = (query || multiplex)
  tracer.trace("execute_query_lazy", {multiplex: multiplex, query: query}) do
    GraphQL::Execution::Lazy.resolve(result)
  end
end

.resolve_field(object, field_ctx) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


119
120
121
122
123
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
# File 'lib/graphql/execution/execute.rb', line 119

def resolve_field(object, field_ctx)
  query = field_ctx.query
  irep_node = field_ctx.irep_node
  parent_type = irep_node.owner_type
  field = field_ctx.field

  raw_value = begin
    begin
      arguments = query.arguments_for(irep_node, field)
      field_ctx.trace("execute_field", { context: field_ctx }) do
        field_ctx.schema.middleware.invoke([parent_type, object, field, arguments, field_ctx])
      end
    rescue GraphQL::UnauthorizedFieldError => err
      err.field ||= field
      field_ctx.schema.unauthorized_field(err)
    rescue GraphQL::UnauthorizedError => err
      field_ctx.schema.unauthorized_object(err)
    end
  rescue GraphQL::ExecutionError => err
    err
  end

  if field_ctx.schema.lazy?(raw_value)
    field_ctx.value = Execution::Lazy.new {
      inner_value = field_ctx.trace("execute_field_lazy", {context: field_ctx}) {
        begin
          begin
            field_ctx.field.lazy_resolve(raw_value, arguments, field_ctx)
          rescue GraphQL::UnauthorizedError => err
            field_ctx.schema.unauthorized_object(err)
          end
        rescue GraphQL::ExecutionError => err
          err
        end
      }
      continue_or_wait(inner_value, field_ctx.type, field_ctx)
    }
  else
    continue_or_wait(raw_value, field_ctx.type, field_ctx)
  end
end

.resolve_root_selection(query) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/graphql/execution/execute.rb', line 48

def resolve_root_selection(query)
  query.trace("execute_query", query: query) do
    operation = query.selected_operation
    op_type = operation.operation_type
    root_type = query.root_type_for_operation(op_type)
    if query.context[:__root_unauthorized]
      # This was set by member/instrumentation.rb so that we wouldn't continue.
    else
      resolve_selection(
        query.root_value,
        root_type,
        query.context,
        mutation: query.mutation?
      )
    end
  end
end

.resolve_selection(object, current_type, current_ctx, mutation: false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


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
# File 'lib/graphql/execution/execute.rb', line 77

def resolve_selection(object, current_type, current_ctx, mutation: false )
  # Assign this _before_ resolving the children
  # so that when a child propagates null, the selection result is
  # ready for it.
  current_ctx.value = {}

  selections_on_type = current_ctx.irep_node.typed_children[current_type]

  selections_on_type.each do |name, child_irep_node|
    field_ctx = current_ctx.spawn_child(
      key: name,
      object: object,
      irep_node: child_irep_node,
    )

    field_result = resolve_field(
      object,
      field_ctx
    )

    if field_result.is_a?(Skip)
      next
    end

    if mutation
      GraphQL::Execution::Lazy.resolve(field_ctx)
    end


    # If the last subselection caused a null to propagate to _this_ selection,
    # then we may as well quit executing fields because they
    # won't be in the response
    if current_ctx.invalid_null?
      break
    else
      current_ctx.value[name] = field_ctx
    end
  end

  current_ctx.value
end

.resolve_value(value, field_type, field_ctx) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/graphql/execution/execute.rb', line 220

def resolve_value(value, field_type, field_ctx)
  field_defn = field_ctx.field

  if value.nil?
    if field_type.kind.non_null?
      parent_type = field_ctx.irep_node.owner_type
      type_error = GraphQL::InvalidNullError.new(parent_type, field_defn, value)
      field_ctx.schema.type_error(type_error, field_ctx)
      PROPAGATE_NULL
    else
      nil
    end
  elsif value.is_a?(GraphQL::ExecutionError)
    if field_type.kind.non_null?
      PROPAGATE_NULL
    else
      nil
    end
  elsif value.is_a?(Array) && value.any? && value.all? {|v| v.is_a?(GraphQL::ExecutionError)}
    if field_type.kind.non_null?
      PROPAGATE_NULL
    else
      nil
    end
  elsif value.is_a?(Skip)
    field_ctx.value = value
  else
    case field_type.kind
    when GraphQL::TypeKinds::SCALAR, GraphQL::TypeKinds::ENUM
      field_type.coerce_result(value, field_ctx)
    when GraphQL::TypeKinds::LIST
      inner_type = field_type.of_type
      i = 0
      result = []
      field_ctx.value = result

      value.each do |inner_value|
        inner_ctx = field_ctx.spawn_child(
          key: i,
          object: inner_value,
          irep_node: field_ctx.irep_node,
        )

        inner_result = continue_or_wait(
          inner_value,
          inner_type,
          inner_ctx,
        )

        return PROPAGATE_NULL if inner_result == PROPAGATE_NULL

        result << inner_ctx
        i += 1
      end

      result
    when GraphQL::TypeKinds::NON_NULL
      inner_type = field_type.of_type
      resolve_value(
        value,
        inner_type,
        field_ctx,
      )
    when GraphQL::TypeKinds::OBJECT
      resolve_selection(
        value,
        field_type,
        field_ctx
      )
    when GraphQL::TypeKinds::UNION, GraphQL::TypeKinds::INTERFACE
      query = field_ctx.query
      resolved_type_or_lazy = field_type.resolve_type(value, field_ctx)
      query.schema.after_lazy(resolved_type_or_lazy) do |resolved_type|
        possible_types = query.possible_types(field_type)

        if !possible_types.include?(resolved_type)
          parent_type = field_ctx.irep_node.owner_type
          type_error = GraphQL::UnresolvedTypeError.new(value, field_defn, parent_type, resolved_type, possible_types)
          field_ctx.schema.type_error(type_error, field_ctx)
          PROPAGATE_NULL
        else
          resolve_value(
            value,
            resolved_type,
            field_ctx,
          )
        end
      end
    else
      raise("Unknown type kind: #{field_type.kind}")
    end
  end
end