Module: GraphQL::Query::SerialExecution::ValueResolution

Defined in:
lib/graphql/query/serial_execution/value_resolution.rb

Class Method Summary collapse

Class Method Details

.resolve(parent_type, field_defn, field_type, value, irep_nodes, query_ctx) ⇒ Object


5
6
7
8
9
10
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
43
44
45
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
# File 'lib/graphql/query/serial_execution/value_resolution.rb', line 5

def self.resolve(parent_type, field_defn, field_type, value, irep_nodes, query_ctx)
  if value.nil? || value.is_a?(GraphQL::ExecutionError)
    if field_type.kind.non_null?
      raise GraphQL::InvalidNullError.new(parent_type.name, field_defn.name, value)
    else
      nil
    end
  else
    case field_type.kind
    when GraphQL::TypeKinds::SCALAR
      field_type.coerce_result(value)
    when GraphQL::TypeKinds::ENUM
      field_type.coerce_result(value, query_ctx.query.warden)
    when GraphQL::TypeKinds::LIST
      wrapped_type = field_type.of_type
      result = value.each_with_index.map do |inner_value, index|
        inner_ctx = query_ctx.spawn(path: query_ctx.path + [index], irep_node: query_ctx.irep_node)
        inner_result = resolve(
          parent_type,
          field_defn,
          wrapped_type,
          inner_value,
          irep_nodes,
          inner_ctx,
        )
        inner_result
      end
      result
    when GraphQL::TypeKinds::NON_NULL
      wrapped_type = field_type.of_type
      resolve(
        parent_type,
        field_defn,
        wrapped_type,
        value,
        irep_nodes,
        query_ctx,
      )
    when GraphQL::TypeKinds::OBJECT
      query_ctx.execution_strategy.selection_resolution.resolve(
        value,
        field_type,
        irep_nodes,
        query_ctx
      )
    when GraphQL::TypeKinds::UNION, GraphQL::TypeKinds::INTERFACE
      query = query_ctx.query
      resolved_type = query.resolve_type(value)
      possible_types = query.possible_types(field_type)

      if !possible_types.include?(resolved_type)
        raise GraphQL::UnresolvedTypeError.new(irep_nodes.first.definition_name, field_type, parent_type, resolved_type, possible_types)
      else
        resolve(
          parent_type,
          field_defn,
          resolved_type,
          value,
          irep_nodes,
          query_ctx,
        )
      end
    else
      raise("Unknown type kind: #{field_type.kind}")
    end
  end
end