Class: GraphQL::Query::Context

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

Classes: FieldResolutionContext

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Delegate

def_delegators

Constructor Details

#initialize(query:, values:) ⇒ 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



42
43
44
45
46
47
48
49
50
51
# File 'lib/graphql/query/context.rb', line 42

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

Instance Attribute Details

#errorsArray<GraphQL::ExecutionError> (readonly)

Returns errors returned during execution.

Returns:



28
29
30
# File 'lib/graphql/query/context.rb', line 28

def errors
  @errors
end

#execution_strategyObject Also known as: strategy

Returns the value of attribute execution_strategy.



8
9
10
# File 'lib/graphql/query/context.rb', line 8

def execution_strategy
  @execution_strategy
end

#irep_nodeGraphQL::InternalRepresentation::Node

Returns The internal representation for this query node.

Returns:



20
21
22
# File 'lib/graphql/query/context.rb', line 20

def irep_node
  @irep_node
end

#pathArray<String, Integer> (readonly)

Returns The current position in the result.

Returns:

  • (Array<String, Integer>)

    The current position in the result



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

def path
  @path
end

#queryGraphQL::Query (readonly)

Returns The query whose context this is.

Returns:



31
32
33
# File 'lib/graphql/query/context.rb', line 31

def query
  @query
end

#schemaGraphQL::Schema (readonly)

Returns:



34
35
36
# File 'lib/graphql/query/context.rb', line 34

def schema
  @schema
end

Instance Method Details

#[](key) ⇒ Object

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



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

#[]=(key, value) ⇒ Object

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



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

#add_error(error) ⇒ void

This method returns an undefined value.

Add error at query-level.

Parameters:



93
94
95
96
97
98
99
# File 'lib/graphql/query/context.rb', line 93

def add_error(error)
  if !error.is_a?(ExecutionError)
    raise TypeError, "expected error to be a ExecutionError, but was #{error.class}"
  end
  errors << error
  nil
end

#ast_nodeGraphQL::Language::Nodes::Field

Returns The AST node for the currently-executing field.

Returns:



23
24
25
# File 'lib/graphql/query/context.rb', line 23

def ast_node
  @irep_node.ast_node
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



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

def namespace(ns)
  @storage[ns]
end

#skipObject

Return this value to tell the runtime to exclude this field from the response altogether



86
87
88
# File 'lib/graphql/query/context.rb', line 86

def skip
  GraphQL::Execution::Execute::SKIP
end

#spawn(key:, selection:, parent_type:, field:) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/graphql/query/context.rb', line 73

def spawn(key:, selection:, parent_type:, field:)
  FieldResolutionContext.new(
    context: self,
    parent: self,
    key: key,
    selection: selection,
    parent_type: parent_type,
    field: field,
  )
end

#wardenGraphQL::Schema::Warden



62
63
64
# File 'lib/graphql/query/context.rb', line 62

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