Class: Rails::GraphQL::Request::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/graphql/request/context.rb

Overview

GraphQL Request Context

This class is used as context for the response while processing fields, objects, or any data that it’s going to be placed on the response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



13
14
15
16
# File 'lib/rails/graphql/request/context.rb', line 13

def initialize
  @stack = []
  @current = Helpers::AttributeDelegator.new(self, :current_value, cache: false)
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



11
12
13
# File 'lib/rails/graphql/request/context.rb', line 11

def current
  @current
end

Instance Method Details

#ancestorsObject

Get all ancestors objects



37
38
39
# File 'lib/rails/graphql/request/context.rb', line 37

def ancestors
  @stack[1..-1]
end

#at(index) ⇒ Object

Get a value at the given index



32
33
34
# File 'lib/rails/graphql/request/context.rb', line 32

def at(index)
  @stack[index]
end

#current_valueObject

Get the current value, which basically means the first item on the current stack



48
49
50
# File 'lib/rails/graphql/request/context.rb', line 48

def current_value
  at(0)
end

#override_value(value) ⇒ Object Also known as: current_value=

Change the current value, either form hits or the actual value



53
54
55
# File 'lib/rails/graphql/request/context.rb', line 53

def override_value(value)
  @stack[0] = value
end

#parentObject

Find the parent object



42
43
44
# File 'lib/rails/graphql/request/context.rb', line 42

def parent
  at(1)
end

#stackObject

Return a duplicated version of the stack, for safety



27
28
29
# File 'lib/rails/graphql/request/context.rb', line 27

def stack
  @stack.dup
end

#stacked(value) ⇒ Object

Add, exec, and then remove the value from the stack



19
20
21
22
23
24
# File 'lib/rails/graphql/request/context.rb', line 19

def stacked(value)
  @stack.unshift(value) unless value.eql?(@stack[0])
  yield(@current)
ensure
  @stack.shift
end