class GraphQL::Query::SerialExecution::FieldResolution

Attributes

arguments[R]
field[R]
irep_node[R]
parent_type[R]
query[R]
target[R]

Public Class Methods

new(selection, parent_type, target, query_ctx) click to toggle source
# File lib/graphql/query/serial_execution/field_resolution.rb, line 8
def initialize(selection, parent_type, target, query_ctx)
  @irep_node = selection
  @selection = selection
  @parent_type = parent_type
  @target = target
  @query = query_ctx.query
  @field = irep_node.definition
  @field_ctx = query_ctx.spawn_child(
    key: irep_node.name,
    object: target,
    irep_node: irep_node,
  )
  @arguments = @query.arguments_for(irep_node, @field)
end

Public Instance Methods

execution_context() click to toggle source

GraphQL::Batch depends on this

# File lib/graphql/query/serial_execution/field_resolution.rb, line 34
def execution_context
  @field_ctx
end
result() click to toggle source
# File lib/graphql/query/serial_execution/field_resolution.rb, line 23
def result
  result_name = irep_node.name
  raw_value = get_raw_value
  if raw_value.is_a?(GraphQL::Execution::Execute::Skip)
    {}
  else
    { result_name => get_finished_value(raw_value) }
  end
end

Private Instance Methods

get_finished_value(raw_value) click to toggle source

After getting the value from the field's resolve method, continue by “finishing” the value, eg. executing sub-fields or coercing values

# File lib/graphql/query/serial_execution/field_resolution.rb, line 42
def get_finished_value(raw_value)
  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
    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 + [index]
        @query.context.errors.push(error)
      end
    end
  end

  begin
    GraphQL::Query::SerialExecution::ValueResolution.resolve(
      parent_type,
      field,
      field.type,
      raw_value,
      @selection,
      @field_ctx,
    )
  rescue GraphQL::Query::Executor::PropagateNull
    if field.type.kind.non_null?
      raise
    else
      nil
    end
  end
end
get_raw_value() click to toggle source

Get the result of:

  • Any middleware on this schema

  • The field's resolve method

If the middleware chain returns a GraphQL::ExecutionError, its message is added to the “errors” key.

# File lib/graphql/query/serial_execution/field_resolution.rb, line 82
def get_raw_value
  begin
    @field_ctx.schema.middleware.invoke([parent_type, target, field, arguments, @field_ctx]) # rubocop:disable Development/ContextIsPassedCop -- unrelated
  rescue GraphQL::ExecutionError => err
    err
  end
end