Class: GraphQL::Query::Partial

Inherits:
Object
  • Object
show all
Includes:
Runnable
Defined in:
lib/graphql/query/partial.rb

Overview

This class is like a GraphQL::Query, except it can run on an arbitrary path within a query string.

It depends on a “parent” GraphQL::Query.

During execution, it calls query-related tracing hooks but passes itself as query:.

The Partial will use your Schema.resolve_type hook to find the right GraphQL type to use for object in some cases.

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Runnable

#after_lazy, #arguments_cache, #arguments_for, #handle_or_reraise

Constructor Details

#initialize(path:, object:, query:, context: nil) ⇒ Partial

Returns a new instance of Partial.

Parameters:

  • path (Array<String, Integer>)

    A path in query.query_string to start executing from

  • object (Object)

    A starting object for execution

  • query (GraphQL::Query)

    A full query instance that this partial is based on. Caches are shared.

  • context (Hash) (defaults to: nil)

    Extra context values to merge into query.context, if provided



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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/graphql/query/partial.rb', line 21

def initialize(path:, object:, query:, context: nil)
  @path = path
  @object = object
  @query = query
  @schema = query.schema
  context_vals = @query.context.to_h
  if context
    context_vals = context_vals.merge(context)
  end
  @context = GraphQL::Query::Context.new(query: self, schema: @query.schema, values: context_vals)
  @multiplex = nil
  @result_values = nil
  @result = nil
  selections = [@query.selected_operation]
  type = @query.root_type
  parent_type = nil
  field_defn = nil
  @path.each do |name_in_doc|
    if name_in_doc.is_a?(Integer)
      if type.list?
        type = type.unwrap
        next
      else
        raise ArgumentError, "Received path with index `#{name_in_doc}`, but type wasn't a list. Type: #{type.to_type_signature}, path: #{@path}"
      end
    end

    next_selections = []
    selections.each do |selection|
      selections_to_check = []
      selections_to_check.concat(selection.selections)
      while (sel = selections_to_check.shift)
        case sel
        when GraphQL::Language::Nodes::InlineFragment
          selections_to_check.concat(sel.selections)
        when GraphQL::Language::Nodes::FragmentSpread
          fragment = @query.fragments[sel.name]
          selections_to_check.concat(fragment.selections)
        when GraphQL::Language::Nodes::Field
          if sel.alias == name_in_doc || sel.name == name_in_doc
            next_selections << sel
          end
        else
          raise "Unexpected selection in partial path: #{sel.class}, #{sel.inspect}"
        end
      end
    end

    if next_selections.empty?
      raise ArgumentError, "Path `#{@path.inspect}` is not present in this query. `#{name_in_doc.inspect}` was not found. Try a different path or rewrite the query to include it."
    end
    field_name = next_selections.first.name
    field_defn = @schema.get_field(type, field_name, @query.context) || raise("Invariant: no field called #{field_name} on #{type.graphql_name}")
    parent_type = type
    type = field_defn.type
    if type.non_null?
      type = type.of_type
    end
    selections = next_selections
  end
  @parent_type = parent_type
  @ast_nodes = selections
  @root_type = type
  @field_definition = field_defn
  @leaf = @root_type.unwrap.kind.leaf?
end

Instance Attribute Details

#ast_nodesObject (readonly)

Returns the value of attribute ast_nodes.



92
93
94
# File 'lib/graphql/query/partial.rb', line 92

def ast_nodes
  @ast_nodes
end

#contextObject (readonly)

Returns the value of attribute context.



92
93
94
# File 'lib/graphql/query/partial.rb', line 92

def context
  @context
end

#field_definitionObject (readonly)

Returns the value of attribute field_definition.



92
93
94
# File 'lib/graphql/query/partial.rb', line 92

def field_definition
  @field_definition
end

#multiplexObject

Returns the value of attribute multiplex.



94
95
96
# File 'lib/graphql/query/partial.rb', line 94

def multiplex
  @multiplex
end

#objectObject (readonly)

Returns the value of attribute object.



92
93
94
# File 'lib/graphql/query/partial.rb', line 92

def object
  @object
end

#parent_typeObject (readonly)

Returns the value of attribute parent_type.



92
93
94
# File 'lib/graphql/query/partial.rb', line 92

def parent_type
  @parent_type
end

#pathObject (readonly)

Returns the value of attribute path.



92
93
94
# File 'lib/graphql/query/partial.rb', line 92

def path
  @path
end

#queryObject (readonly)

Returns the value of attribute query.



92
93
94
# File 'lib/graphql/query/partial.rb', line 92

def query
  @query
end

#result_valuesObject

Returns the value of attribute result_values.



94
95
96
# File 'lib/graphql/query/partial.rb', line 94

def result_values
  @result_values
end

#root_typeObject (readonly)

Returns the value of attribute root_type.



92
93
94
# File 'lib/graphql/query/partial.rb', line 92

def root_type
  @root_type
end

#schemaObject (readonly)

Returns the value of attribute schema.



92
93
94
# File 'lib/graphql/query/partial.rb', line 92

def schema
  @schema
end

Instance Method Details

#analysis_errors=(_ignored) ⇒ Object



139
140
141
# File 'lib/graphql/query/partial.rb', line 139

def analysis_errors=(_ignored)
  # pass
end

#analyzersObject



135
136
137
# File 'lib/graphql/query/partial.rb', line 135

def analyzers
  EmptyObjects::EMPTY_ARRAY
end

#current_traceObject



111
112
113
# File 'lib/graphql/query/partial.rb', line 111

def current_trace
  @query.current_trace
end

#fragmentsObject



127
128
129
# File 'lib/graphql/query/partial.rb', line 127

def fragments
  @query.fragments
end

#leaf?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/graphql/query/partial.rb', line 88

def leaf?
  @leaf
end

#resolve_typeObject



119
120
121
# File 'lib/graphql/query/partial.rb', line 119

def resolve_type(...)
  @query.resolve_type(...)
end

#resultObject



107
108
109
# File 'lib/graphql/query/partial.rb', line 107

def result
  @result ||= Result.new(query: self, values: result_values)
end

#selected_operationObject



147
148
149
# File 'lib/graphql/query/partial.rb', line 147

def selected_operation
  ast_nodes.first
end

#static_errorsObject



151
152
153
# File 'lib/graphql/query/partial.rb', line 151

def static_errors
  @query.static_errors
end

#subscription?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/graphql/query/partial.rb', line 143

def subscription?
  @query.subscription?
end

#typesObject



115
116
117
# File 'lib/graphql/query/partial.rb', line 115

def types
  @query.types
end

#valid?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/graphql/query/partial.rb', line 131

def valid?
  @query.valid?
end

#variablesObject



123
124
125
# File 'lib/graphql/query/partial.rb', line 123

def variables
  @query.variables
end