Class: GraphQL::Query::Context
- Inherits:
- 
      Object
      
        - Object
- GraphQL::Query::Context
 
- Extended by:
- Forwardable
- 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: ExecutionErrors, FieldResolutionContext
Constant Summary collapse
- UNSPECIFIED_FETCH_DEFAULT =
- Object.new 
Instance Attribute Summary collapse
- 
  
    
      #errors  ⇒ Array<GraphQL::ExecutionError> 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Errors returned during execution. 
- 
  
    
      #execution_strategy  ⇒ Object 
    
    
      (also: #strategy)
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute execution_strategy. 
- 
  
    
      #interpreter  ⇒ Object 
    
    
  
  
  
  
    
    
      writeonly
    
  
  
  
  
  private
  
    
- 
  
    
      #path  ⇒ Array<String, Integer> 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    The current position in the result. 
- 
  
    
      #query  ⇒ GraphQL::Query 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    The query whose context this is. 
- 
  
    
      #schema  ⇒ GraphQL::Schema 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    
- 
  
    
      #scoped_context  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  private
  
    
- 
  
    
      #value  ⇒ Object 
    
    
  
  
  
  
    
    
      writeonly
    
  
  
  
  
  private
  
    
- 
  
    
      #warden  ⇒ GraphQL::Schema::Warden 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    
Attributes included from SharedMethods
Instance Method Summary collapse
- 
  
    
      #[](key)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Lookup keyfrom the hash passed to Schema#execute ascontext:.
- 
  
    
      #[]=(key, value)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Reassign keyto the hash passed to Schema#execute ascontext:.
- 
  
    
      #ast_node  ⇒ GraphQL::Language::Nodes::Field 
    
    
  
  
  
  
  
  
  
  
  
    The AST node for the currently-executing field. 
- 
  
    
      #dataloader  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
- 
  
    
      #delete(key)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
- 
  
    
      #dig(key, *other_keys)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
- 
  
    
      #fetch(key, default = UNSPECIFIED_FETCH_DEFAULT)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
- 
  
    
      #initialize(query:, schema: query.schema, values:, object:)  ⇒ Context 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    Make a new context which delegates key lookup to values.
- 
  
    
      #inspect  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
- 
  
    
      #irep_node  ⇒ GraphQL::InternalRepresentation::Node 
    
    
  
  
  
  
  
  
  
  
  
    The internal representation for this query node. 
- 
  
    
      #key?(key)  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    
- 
  
    
      #namespace(ns)  ⇒ Hash 
    
    
  
  
  
  
  
  
  
  
  
    Get an isolated hash for ns.
- 
  
    
      #namespace?(ns)  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    True if this namespace was accessed before. 
- 
  
    
      #received_null_child  ⇒ Object 
    
    
  
  
  
  
  
  
  
  private
  
    
- 
  
    
      #response_extensions  ⇒ Hash 
    
    
  
  
  
  
  
  
  
  
  
    A hash that will be added verbatim to the result hash, as "extensions" => { ... }.
- 
  
    
      #scoped_merge!(hash)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
- 
  
    
      #scoped_set!(key, value)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
- 
  
    
      #to_h  ⇒ Object 
    
    
      (also: #to_hash)
    
  
  
  
  
  
  
  
  
  
    
Methods included from SharedMethods
#add_error, #backtrace, #delete_child, #execution_errors, #invalid_null?, #lookahead, #skip, #spawn_child
Constructor Details
#initialize(query:, schema: query.schema, values:, object:) ⇒ Context
Make a new context which delegates key lookup to values
| 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | # File 'lib/graphql/query/context.rb', line 144 def initialize(query:, schema: query.schema, values:, object:) @query = query @schema = 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 @scoped_context = {} end | 
Instance Attribute Details
#errors ⇒ Array<GraphQL::ExecutionError> (readonly)
Returns errors returned during execution.
| 130 131 132 | # File 'lib/graphql/query/context.rb', line 130 def errors @errors end | 
#execution_strategy ⇒ Object Also known as: strategy
Returns the value of attribute execution_strategy.
| 108 109 110 | # File 'lib/graphql/query/context.rb', line 108 def execution_strategy @execution_strategy end | 
#interpreter=(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.
| 169 170 171 | # File 'lib/graphql/query/context.rb', line 169 def interpreter=(value) @interpreter = value end | 
#path ⇒ Array<String, Integer> (readonly)
Returns The current position in the result.
| 139 140 141 | # File 'lib/graphql/query/context.rb', line 139 def path @path end | 
#query ⇒ GraphQL::Query (readonly)
Returns The query whose context this is.
| 133 134 135 | # File 'lib/graphql/query/context.rb', line 133 def query @query end | 
#schema ⇒ GraphQL::Schema (readonly)
| 136 137 138 | # File 'lib/graphql/query/context.rb', line 136 def schema @schema end | 
#scoped_context ⇒ 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.
| 175 176 177 | # File 'lib/graphql/query/context.rb', line 175 def scoped_context @scoped_context 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.
| 172 173 174 | # File 'lib/graphql/query/context.rb', line 172 def value=(value) @value = value end | 
#warden ⇒ GraphQL::Schema::Warden
| 230 231 232 | # File 'lib/graphql/query/context.rb', line 230 def warden @warden ||= (@query && @query.warden) end | 
Instance Method Details
#[](key) ⇒ Object
Lookup key from the hash passed to Schema#execute as context:
| 187 188 189 190 | # File 'lib/graphql/query/context.rb', line 187 def [](key) return @scoped_context[key] if @scoped_context.key?(key) @provided_values[key] end | 
#[]=(key, value) ⇒ Object
Reassign key to the hash passed to Schema#execute as context:
| 183 184 185 | # File 'lib/graphql/query/context.rb', line 183 def []=(key, value) @provided_values[key] = value end | 
#ast_node ⇒ GraphQL::Language::Nodes::Field
Returns The AST node for the currently-executing field.
| 125 126 127 | # File 'lib/graphql/query/context.rb', line 125 def ast_node @irep_node.ast_node end | 
#dataloader ⇒ Object
| 164 165 166 | # File 'lib/graphql/query/context.rb', line 164 def dataloader @dataloader ||= self[:dataloader] || (query.multiplex ? query.multiplex.dataloader : schema.dataloader_class.new) end | 
#delete(key) ⇒ Object
| 192 193 194 195 196 197 198 | # File 'lib/graphql/query/context.rb', line 192 def delete(key) if @scoped_context.key?(key) @scoped_context.delete(key) else @provided_values.delete(key) end end | 
#dig(key, *other_keys) ⇒ Object
| 216 217 218 | # File 'lib/graphql/query/context.rb', line 216 def dig(key, *other_keys) @scoped_context.key?(key) ? @scoped_context.dig(key, *other_keys) : @provided_values.dig(key, *other_keys) end | 
#fetch(key, default = UNSPECIFIED_FETCH_DEFAULT) ⇒ Object
| 202 203 204 205 206 207 208 209 210 211 212 213 214 | # File 'lib/graphql/query/context.rb', line 202 def fetch(key, default = UNSPECIFIED_FETCH_DEFAULT) if @scoped_context.key?(key) @scoped_context[key] elsif @provided_values.key?(key) @provided_values[key] elsif default != UNSPECIFIED_FETCH_DEFAULT default elsif block_given? yield(self, key) else raise KeyError.new(key: key) end end | 
#inspect ⇒ Object
| 249 250 251 | # File 'lib/graphql/query/context.rb', line 249 def inspect "#<Query::Context ...>" end | 
#irep_node ⇒ GraphQL::InternalRepresentation::Node
Returns The internal representation for this query node.
| 120 121 122 | # File 'lib/graphql/query/context.rb', line 120 def irep_node @irep_node ||= query.irep_selection end | 
#key?(key) ⇒ Boolean
| 225 226 227 | # File 'lib/graphql/query/context.rb', line 225 def key?(key) @scoped_context.key?(key) || @provided_values.key?(key) end | 
#namespace(ns) ⇒ Hash
Get an isolated hash for ns. Doesn’t affect user-provided storage.
| 240 241 242 | # File 'lib/graphql/query/context.rb', line 240 def namespace(ns) @storage[ns] end | 
#namespace?(ns) ⇒ Boolean
Returns true if this namespace was accessed before.
| 245 246 247 | # File 'lib/graphql/query/context.rb', line 245 def namespace?(ns) @storage.key?(ns) end | 
#received_null_child ⇒ 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.
| 254 255 256 257 | # File 'lib/graphql/query/context.rb', line 254 def received_null_child @invalid_null = true @value = nil end | 
#response_extensions ⇒ Hash
Returns A hash that will be added verbatim to the result hash, as "extensions" => { ... }.
| 160 161 162 | # File 'lib/graphql/query/context.rb', line 160 def response_extensions namespace(:__query_result_extensions__) end | 
#scoped_merge!(hash) ⇒ Object
| 259 260 261 | # File 'lib/graphql/query/context.rb', line 259 def scoped_merge!(hash) @scoped_context = @scoped_context.merge(hash) end | 
#scoped_set!(key, value) ⇒ Object
| 263 264 265 266 | # File 'lib/graphql/query/context.rb', line 263 def scoped_set!(key, value) scoped_merge!(key => value) nil end | 
#to_h ⇒ Object Also known as: to_hash
| 220 221 222 | # File 'lib/graphql/query/context.rb', line 220 def to_h @provided_values.merge(@scoped_context) end |