class Zenaton::Services::GraphQL::BaseOperation

@abstract Superclass for graphql queries and mutations. It expects two methods to be implemented in children classes:

Public Class Methods

new(*) click to toggle source

Sets up common dependencies for serialization Don't forget to call super in your children initialize if overriding this method.

# File lib/zenaton/services/graph_ql/base_operation.rb, line 19
def initialize(*)
  @serializer = Services::Serializer.new
  @properties = Services::Properties.new
end

Public Instance Methods

body() click to toggle source

To be implemented in subclasses. Should return the body of the GraphQL request @raise [NotImplemented]

# File lib/zenaton/services/graph_ql/base_operation.rb, line 27
def body
  raise NotImplemented
end
intent_id() click to toggle source

Sets an unique identifier to the query @return [String]

# File lib/zenaton/services/graph_ql/base_operation.rb, line 56
def intent_id
  SecureRandom.uuid
end
query() click to toggle source

Removes duplicate white space from the raw_query @return [String]

# File lib/zenaton/services/graph_ql/base_operation.rb, line 50
def query
  raw_query.gsub(/\s+/, ' ')
end
raw_query() click to toggle source

To be implemented in subclasses. The actual GraphQL query @raise [NotImplemented]

# File lib/zenaton/services/graph_ql/base_operation.rb, line 34
def raw_query
  raise NotImplemented
end
result(response) click to toggle source

Default implementation for parsing GraphQL responses Override in subclasses if needed. @raise [NotImplemented]

# File lib/zenaton/services/graph_ql/base_operation.rb, line 41
def result(response)
  raise Zenaton::ExternalError, format_errors(response) \
    if response['errors']

  response['data']
end

Private Instance Methods

format_error(error) click to toggle source
# File lib/zenaton/services/graph_ql/base_operation.rb, line 67
def format_error(error)
  if error['path']
    "- #{error['path']}: #{error['message']}"
  else
    "- #{error['message']}"
  end
end
format_errors(response) click to toggle source
# File lib/zenaton/services/graph_ql/base_operation.rb, line 62
def format_errors(response)
  response['errors'].map(&method(:format_error))
                    .join("\n")
end