Class: GraphWeaver::GraphQLError

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

Class Method Summary collapse

Instance Method Summary collapse

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 = message
  @locations = locations
  @path = path
  @extensions = extensions
  @error_type = type
end

Instance Attribute Details

#extensionsObject (readonly)

Returns the value of attribute extensions.



84
85
86
# File 'lib/graph_weaver/errors.rb', line 84

def extensions
  @extensions
end

#locationsObject (readonly)

Returns the value of attribute locations.



84
85
86
# File 'lib/graph_weaver/errors.rb', line 84

def locations
  @locations
end

#messageObject (readonly)

Returns the value of attribute message.



84
85
86
# File 'lib/graph_weaver/errors.rb', line 84

def message
  @message
end

#pathObject (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

#codeObject

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

#fieldObject

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_hObject

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" => message,
    "code" => code,
    "field" => field,
    "path" => path,
    "locations" => locations,
    "extensions" => extensions,
    "validation" => validation?,
  }
end

#to_sObject



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}]" : ""
  "#{message}#{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.

Returns:

  • (Boolean)


121
122
123
# File 'lib/graph_weaver/errors.rb', line 121

def validation?
  code == "GRAPHQL_VALIDATION_FAILED" || VALIDATION_MESSAGE.match?(message)
end