Class: GraphWeaver::GraphQLError

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

T.let(
  /doesn't exist|Cannot query field|Unknown (field|type|argument)|isn't defined|undefined (field|type)/i,
  Regexp,
)

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.



129
130
131
132
133
134
135
# File 'lib/graph_weaver/errors.rb', line 129

def initialize(message:, locations: [], path: nil, extensions: {}, type: nil)
  @message = message
  @locations = locations
  @path = path
  @extensions = extensions
  @error_type = T.let(type, T.nilable(String))
end

Instance Attribute Details

#extensionsObject (readonly)

Returns the value of attribute extensions.



118
119
120
# File 'lib/graph_weaver/errors.rb', line 118

def extensions
  @extensions
end

#locationsObject (readonly)

Returns the value of attribute locations.



112
113
114
# File 'lib/graph_weaver/errors.rb', line 112

def locations
  @locations
end

#messageObject (readonly)

Returns the value of attribute message.



109
110
111
# File 'lib/graph_weaver/errors.rb', line 109

def message
  @message
end

#pathObject (readonly)

Returns the value of attribute path.



115
116
117
# File 'lib/graph_weaver/errors.rb', line 115

def path
  @path
end

Class Method Details

.from_h(hash) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/graph_weaver/errors.rb', line 138

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



153
154
155
# File 'lib/graph_weaver/errors.rb', line 153

def code
  extensions["code"] || @error_type
end

#fieldObject



179
180
181
182
183
184
185
# File 'lib/graph_weaver/errors.rb', line 179

def field
  p = path
  return unless p

  named = p.reject { |seg| seg.is_a?(Integer) || seg.to_s.match?(/\A\d+\z/) }
  named.join(".") unless named.empty?
end

#to_hObject



201
202
203
204
205
206
207
208
209
210
211
# File 'lib/graph_weaver/errors.rb', line 201

def to_h
  {
    "message" => message,
    "code" => code,
    "field" => field,
    "path" => path,
    "locations" => locations,
    "extensions" => extensions,
    "validation" => validation?,
  }
end

#to_sObject



188
189
190
191
192
193
194
195
# File 'lib/graph_weaver/errors.rb', line 188

def to_s
  loc = locations.first
  at = loc ? " at #{loc["line"]}:#{loc["column"]}" : ""
  p = path
  where = p ? " (path: #{p.join(".")})" : ""
  tag = code ? " [#{code}]" : ""
  "#{message}#{at}#{where}#{tag}"
end

#validation?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/graph_weaver/errors.rb', line 170

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