Class: GraphWeaver::GraphQLError
- Inherits:
-
Object
- Object
- GraphWeaver::GraphQLError
- Defined in:
- lib/graph_weaver/errors.rb
Overview
One entry from a GraphQL response's top-level errors array. A value
object (not raised) — the response envelope and QueryError carry these.
Match on #code (extensions) rather than the message string.
Constant Summary collapse
- VALIDATION_MESSAGE =
Message shapes servers use when they reject the shape of a query (unknown field/type/argument). Heuristic by necessity: only Apollo sets a standard code (GRAPHQL_VALIDATION_FAILED); graphql-ruby and GitHub speak in messages.
/doesn't exist|Cannot query field|Unknown (field|type|argument)|isn't defined|undefined (field|type)/i
Instance Attribute Summary collapse
-
#extensions ⇒ Object
readonly
Returns the value of attribute extensions.
-
#locations ⇒ Object
readonly
Returns the value of attribute locations.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
-
#code ⇒ Object
The machine-readable error code, e.g.
-
#field ⇒ Object
The field the error points at, as a stable dotted path with list indices stripped — ["people", 3, "email"] => "people.email".
-
#initialize(message:, locations: [], path: nil, extensions: {}, type: nil) ⇒ GraphQLError
constructor
A new instance of GraphQLError.
-
#to_h ⇒ Object
JSON-ready: the problematic field (both forms — #field for grouping, #path with indices for exact location), the machine code, and the server's full extensions.
- #to_s ⇒ Object
-
#validation? ⇒ Boolean
True when this error looks like the server rejected the query's shape — for a generated module that usually means the schema changed after generation.
Constructor Details
#initialize(message:, locations: [], path: nil, extensions: {}, type: nil) ⇒ GraphQLError
Returns a new instance of GraphQLError.
86 87 88 89 90 91 92 |
# File 'lib/graph_weaver/errors.rb', line 86 def initialize(message:, locations: [], path: nil, extensions: {}, type: nil) @message = @locations = locations @path = path @extensions = extensions @error_type = type end |
Instance Attribute Details
#extensions ⇒ Object (readonly)
Returns the value of attribute extensions.
84 85 86 |
# File 'lib/graph_weaver/errors.rb', line 84 def extensions @extensions end |
#locations ⇒ Object (readonly)
Returns the value of attribute locations.
84 85 86 |
# File 'lib/graph_weaver/errors.rb', line 84 def locations @locations end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
84 85 86 |
# File 'lib/graph_weaver/errors.rb', line 84 def @message end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
84 85 86 |
# File 'lib/graph_weaver/errors.rb', line 84 def path @path end |
Class Method Details
.from_h(hash) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/graph_weaver/errors.rb', line 94 def self.from_h(hash) new( message: hash["message"] || "(no message)", locations: hash["locations"] || [], path: hash["path"], extensions: hash["extensions"] || {}, type: hash["type"], ) end |
Instance Method Details
#code ⇒ Object
The machine-readable error code, e.g. "THROTTLED" — the thing to branch on. Read from extensions.code (the Apollo/spec-adjacent convention) or a top-level "type" (GitHub's dialect: NOT_FOUND, FORBIDDEN). nil when the server sent neither.
108 109 110 |
# File 'lib/graph_weaver/errors.rb', line 108 def code extensions["code"] || @error_type end |
#field ⇒ Object
The field the error points at, as a stable dotted path with list indices stripped — ["people", 3, "email"] => "people.email". The parseable key for grouping/reporting (the raw #path keeps indices). nil for global errors with no path.
129 130 131 132 133 134 |
# File 'lib/graph_weaver/errors.rb', line 129 def field return unless path named = path.reject { |seg| seg.is_a?(Integer) || seg.to_s.match?(/\A\d+\z/) } named.join(".") unless named.empty? end |
#to_h ⇒ Object
JSON-ready: the problematic field (both forms — #field for grouping, #path with indices for exact location), the machine code, and the server's full extensions.
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/graph_weaver/errors.rb', line 147 def to_h { "message" => , "code" => code, "field" => field, "path" => path, "locations" => locations, "extensions" => extensions, "validation" => validation?, } end |
#to_s ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/graph_weaver/errors.rb', line 136 def to_s loc = locations&.first at = loc ? " at #{loc["line"]}:#{loc["column"]}" : "" where = path ? " (path: #{path.join(".")})" : "" tag = code ? " [#{code}]" : "" "#{}#{at}#{where}#{tag}" end |
#validation? ⇒ Boolean
True when this error looks like the server rejected the query's shape — for a generated module that usually means the schema changed after generation.
121 122 123 |
# File 'lib/graph_weaver/errors.rb', line 121 def validation? code == "GRAPHQL_VALIDATION_FAILED" || VALIDATION_MESSAGE.match?() end |