Class: GraphQL::Query::Context

Inherits:
Object
  • Object
show all
Extended by:
Delegate
Includes:
SharedMethods
Defined in:
lib/graphql/query/context.rb

Overview

Expose some query-specific info to field resolve functions. It delegates [] to the hash that's passed to GraphQL::Query#initialize.

Defined Under Namespace

Modules: SharedMethods Classes: FieldResolutionContext

Instance Attribute Summary collapse

Attributes included from SharedMethods

#object, #skipped, #value

Instance Method Summary collapse

Methods included from Delegate

def_delegators

Methods included from SharedMethods

#add_error, #backtrace, #delete, #invalid_null?, #skip, #spawn_child

Constructor Details

#initialize(query:, values:, object:) ⇒ Context

Make a new context which delegates key lookup to values

Parameters:

  • query (GraphQL::Query)

    the query who owns this context

  • values (Hash)

    A hash of arbitrary values which will be accessible at query-time



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/graphql/query/context.rb', line 114

def initialize(query:, values: , object:)
  @query = query
  @schema = query.schema
  @provided_values = values || {}
  @object = object
  # Namespaced storage, where user-provided values are in `nil` namespace:
  @storage = Hash.new { |h, k| h[k] = {} }
  @storage[nil] = @provided_values
  @errors = []
  @path = []
  @value = nil
  @context = self # for SharedMethods
end

Instance Attribute Details

#errorsArray<GraphQL::ExecutionError> (readonly)

Returns errors returned during execution.

Returns:



100
101
102
# File 'lib/graphql/query/context.rb', line 100

def errors
  @errors
end

#execution_strategyObject Also known as: strategy

Returns the value of attribute execution_strategy.



78
79
80
# File 'lib/graphql/query/context.rb', line 78

def execution_strategy
  @execution_strategy
end

#pathArray<String, Integer> (readonly)

Returns The current position in the result.

Returns:

  • (Array<String, Integer>)

    The current position in the result



109
110
111
# File 'lib/graphql/query/context.rb', line 109

def path
  @path
end

#queryGraphQL::Query (readonly)

Returns The query whose context this is.

Returns:



103
104
105
# File 'lib/graphql/query/context.rb', line 103

def query
  @query
end

#schemaGraphQL::Schema (readonly)

Returns:



106
107
108
# File 'lib/graphql/query/context.rb', line 106

def schema
  @schema
end

#value=(value) ⇒ Object (writeonly)

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.



129
130
131
# File 'lib/graphql/query/context.rb', line 129

def value=(value)
  @value = value
end

Instance Method Details

#[](key) ⇒ Object

Lookup key from the hash passed to Schema#execute as context:



# File 'lib/graphql/query/context.rb', line 133

#[]=(key, value) ⇒ Object

Reassign key to the hash passed to Schema#execute as context:



# File 'lib/graphql/query/context.rb', line 136

#ast_nodeGraphQL::Language::Nodes::Field

Returns The AST node for the currently-executing field.

Returns:



95
96
97
# File 'lib/graphql/query/context.rb', line 95

def ast_node
  @irep_node.ast_node
end

#inspectObject



152
153
154
# File 'lib/graphql/query/context.rb', line 152

def inspect
  "#<Query::Context ...>"
end

#irep_nodeGraphQL::InternalRepresentation::Node

Returns The internal representation for this query node.

Returns:



90
91
92
# File 'lib/graphql/query/context.rb', line 90

def irep_node
  @irep_node ||= query.irep_selection
end

#namespace(ns) ⇒ Hash

Get an isolated hash for ns. Doesn't affect user-provided storage.

Parameters:

  • ns (Object)

    a usage-specific namespace identifier

Returns:

  • (Hash)

    namespaced storage



148
149
150
# File 'lib/graphql/query/context.rb', line 148

def namespace(ns)
  @storage[ns]
end

#received_null_childObject

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.



157
158
159
160
# File 'lib/graphql/query/context.rb', line 157

def received_null_child
  @invalid_null = true
  @value = nil
end

#wardenGraphQL::Schema::Warden



141
142
143
# File 'lib/graphql/query/context.rb', line 141

def warden
  @warden ||= @query.warden
end