Class: GraphWeaver::Response

Inherits:
Object
  • Object
show all
Extended by:
T::Generic, T::Sig
Includes:
ErrorFiltering
Defined in:
lib/graph_weaver/response.rb

Overview

The envelope every generated #execute returns: the typed data (nil on a total failure), the top-level GraphQL errors, and top-level extensions (cost/throttle metadata). Generic over the query's Result type so response.data stays fully typed — the generated code instantiates it as GraphWeaver::Response. #data! is the strict accessor: the result, or a raised QueryError.

Constant Summary collapse

Data =
type_member

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ErrorFiltering

#each_error, #entity_id, #errors_at, #errors_by_field, #report, #schema_stale?

Constructor Details

#initialize(data:, errors: [], extensions: {}) ⇒ Response

Returns a new instance of Response.



38
39
40
41
42
# File 'lib/graph_weaver/response.rb', line 38

def initialize(data:, errors: [], extensions: {})
  @data = data
  @errors = errors
  @extensions = extensions
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



23
24
25
# File 'lib/graph_weaver/response.rb', line 23

def data
  @data
end

#errorsObject (readonly)

Returns the value of attribute errors.



26
27
28
# File 'lib/graph_weaver/response.rb', line 26

def errors
  @errors
end

#extensionsObject (readonly)

Returns the value of attribute extensions.



29
30
31
# File 'lib/graph_weaver/response.rb', line 29

def extensions
  @extensions
end

Instance Method Details

#data!Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/graph_weaver/response.rb', line 50

def data!
  raise GraphWeaver::QueryError.new(errors, data: data, extensions: extensions) unless errors.empty?

  # a well-formed GraphQL response always pairs null data with errors; a
  # server (or an errors-stripping proxy) that returns neither is broken —
  # brand it rather than leaking a bare `T.must` TypeError
  data || raise(GraphWeaver::QueryError.new(
    [GraphWeaver::GraphQLError.new(message: "response carried neither data nor errors")],
    extensions: extensions,
  ))
end

#errors?Boolean

Returns:

  • (Boolean)


45
# File 'lib/graph_weaver/response.rb', line 45

def errors? = !errors.empty?