Class: ActiveGraphql::Client::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/active_graphql/client/response.rb

Overview

graphql response wrapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graphql_object, error = nil) ⇒ Response

Returns a new instance of Response.



9
10
11
12
13
14
15
16
# File 'lib/active_graphql/client/response.rb', line 9

def initialize(graphql_object, error = nil)
  if graphql_object
    root_field = graphql_object.to_h.keys.first.to_s.underscore
    @graphql_object = graphql_object.public_send(root_field)
  end

  @graphql_error = error if error
end

Instance Attribute Details

#graphql_objectObject (readonly)

Returns the value of attribute graphql_object.



7
8
9
# File 'lib/active_graphql/client/response.rb', line 7

def graphql_object
  @graphql_object
end

Instance Method Details

#detailed_errorsObject



32
33
34
35
36
# File 'lib/active_graphql/client/response.rb', line 32

def detailed_errors
  return [] if graphql_error.blank?

  graphql_error.errors.details['data'].map(&:with_indifferent_access)
end

#errorsObject



38
39
40
# File 'lib/active_graphql/client/response.rb', line 38

def errors
  detailed_errors.map { |error| error[:message] }
end

#resultObject



18
19
20
# File 'lib/active_graphql/client/response.rb', line 18

def result
  graphql_object
end

#result!Object



22
23
24
25
26
# File 'lib/active_graphql/client/response.rb', line 22

def result!
  raise ActiveGraphql::Errors::ResponseError, errors.first if errors.any?

  graphql_object
end

#success?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/active_graphql/client/response.rb', line 28

def success?
  graphql_error.nil?
end