Class: GraphQL::ExecutionError

Inherits:
RuntimeError
  • Object
show all
Defined in:
lib/graphql/execution_error.rb

Overview

If a field's resolve function returns an ExecutionError, the error will be inserted into the response's "errors" key and the field will resolve to nil.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, ast_node: nil, ast_nodes: nil, options: nil, extensions: nil) ⇒ ExecutionError

Returns a new instance of ExecutionError.



26
27
28
29
30
31
# File 'lib/graphql/execution_error.rb', line 26

def initialize(message, ast_node: nil, ast_nodes: nil, options: nil, extensions: nil)
  @ast_nodes = ast_nodes || [ast_node]
  @options = options
  @extensions = extensions
  super(message)
end

Instance Attribute Details

#extensions ⇒ Object

Returns

  • Hash — Optional custom data for error objects which will be added under the extensions key.


24
25
26
# File 'lib/graphql/execution_error.rb', line 24

def extensions
  @extensions
end

#options ⇒ Object

Deprecated: Use extensions instead of options. The GraphQL spec recommends that any custom entries in an error be under the extensions key.

Returns

  • Hash — Optional data for error objects


19
20
21
# File 'lib/graphql/execution_error.rb', line 19

def options
  @options
end

#path ⇒ Object

Returns

  • String — an array describing the JSON-path into the execution response which corresponds to this error.


12
13
14
# File 'lib/graphql/execution_error.rb', line 12

def path
  @path
end

Instance Method Details

#finalize_graphql_result(query, result_data, key) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/graphql/execution_error.rb', line 33

def finalize_graphql_result(query, result_data, key)
  add_this_error = !query.context.errors.any? { |e| e.eql?(self) }
  if add_this_error
    query.context.add_error(self)
  end
  if ast_node.is_a?(GraphQL::Language::Nodes::Directive)
    # This is for backwards compatibility ... what does the spec say?
    result_data.delete(key)
  else
    result_data[key] = nil
  end
end

#to_h ⇒ Object

Returns

  • Hash — An entry for the response's "errors" key

:call-seq:

to_h() -> Hash


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/graphql/execution_error.rb', line 52

def to_h
  hash = {
    "message" => message,
  }
  if ast_node
    hash["locations"] = @ast_nodes.map { |a| { "line" => a.line, "column" => a.col } }
  end
  if path
    hash["path"] = path
  end
  if options
    hash.merge!(options)
  end
  if extensions
    hash["extensions"] = extensions.each_with_object({}) { |(key, value), ext|
      ext[key.to_s] = value
    }
  end
  hash
end