Class: GraphQL::Query::Context

Inherits:
Object
  • Object
show all
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

Constant Summary collapse

UNSPECIFIED_FETCH_DEFAULT =
Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SharedMethods

#add_error, #backtrace, #execution_errors, #skip

Constructor Details

#initialize(query:, schema: query.schema, 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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/graphql/query/context.rb', line 78

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

#errorsArray<GraphQL::ExecutionError> (readonly)

Returns errors returned during execution.

Returns:



64
65
66
# File 'lib/graphql/query/context.rb', line 64

def errors
  @errors
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.



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

def interpreter=(value)
  @interpreter = value
end

#pathArray<String, Integer> (readonly)

Returns The current position in the result.

Returns:

  • (Array<String, Integer>)

    The current position in the result



73
74
75
# File 'lib/graphql/query/context.rb', line 73

def path
  @path
end

#queryGraphQL::Query (readonly)

Returns The query whose context this is.

Returns:



67
68
69
# File 'lib/graphql/query/context.rb', line 67

def query
  @query
end

#schemaGraphQL::Schema (readonly)

Returns:



70
71
72
# File 'lib/graphql/query/context.rb', line 70

def schema
  @schema
end

#scoped_contextObject

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.



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

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.



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

def value=(value)
  @value = value
end

#wardenGraphQL::Schema::Warden



164
165
166
# File 'lib/graphql/query/context.rb', line 164

def warden
  @warden ||= (@query && @query.warden)
end

Instance Method Details

#[](key) ⇒ Object

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



121
122
123
124
# File 'lib/graphql/query/context.rb', line 121

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:



117
118
119
# File 'lib/graphql/query/context.rb', line 117

def []=(key, value)
  @provided_values[key] = value
end

#dataloaderObject



98
99
100
# File 'lib/graphql/query/context.rb', line 98

def dataloader
  @dataloader ||= self[:dataloader] || (query.multiplex ? query.multiplex.dataloader : schema.dataloader_class.new)
end

#delete(key) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/graphql/query/context.rb', line 126

def delete(key)
  if @scoped_context.key?(key)
    @scoped_context.delete(key)
  else
    @provided_values.delete(key)
  end
end

#dig(key, *other_keys) ⇒ Object



150
151
152
# File 'lib/graphql/query/context.rb', line 150

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



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/graphql/query/context.rb', line 136

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

#inspectObject



183
184
185
# File 'lib/graphql/query/context.rb', line 183

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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.

Parameters:

  • ns (Object)

    a usage-specific namespace identifier

Returns:

  • (Hash)

    namespaced storage



174
175
176
# File 'lib/graphql/query/context.rb', line 174

def namespace(ns)
  @storage[ns]
end

#namespace?(ns) ⇒ Boolean

Returns true if this namespace was accessed before.

Returns:

  • (Boolean)

    true if this namespace was accessed before



179
180
181
# File 'lib/graphql/query/context.rb', line 179

def namespace?(ns)
  @storage.key?(ns)
end

#response_extensionsHash

Returns A hash that will be added verbatim to the result hash, as "extensions" => { ... }.

Returns:

  • (Hash)

    A hash that will be added verbatim to the result hash, as "extensions" => { ... }



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

def response_extensions
  namespace(:__query_result_extensions__)
end

#scoped_merge!(hash) ⇒ Object



187
188
189
# File 'lib/graphql/query/context.rb', line 187

def scoped_merge!(hash)
  @scoped_context = @scoped_context.merge(hash)
end

#scoped_set!(key, value) ⇒ Object



191
192
193
194
# File 'lib/graphql/query/context.rb', line 191

def scoped_set!(key, value)
  scoped_merge!(key => value)
  nil
end

#to_hObject Also known as: to_hash



154
155
156
# File 'lib/graphql/query/context.rb', line 154

def to_h
  @provided_values.merge(@scoped_context)
end