Class: Zenaton::Services::GraphQL::BaseOperation Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/zenaton/services/graph_ql/base_operation.rb

Overview

This class is abstract.

Superclass for graphql queries and mutations.

It expects two methods to be implemented in children classes:

  • #body

  • #raw_query

Instance Method Summary collapse

Constructor Details

#initializeBaseOperation

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



19
20
21
22
# File 'lib/zenaton/services/graph_ql/base_operation.rb', line 19

def initialize(*)
  @serializer = Services::Serializer.new
  @properties = Services::Properties.new
end

Instance Method Details

#bodyObject

To be implemented in subclasses. Should return the body of the GraphQL request

Raises:



27
28
29
# File 'lib/zenaton/services/graph_ql/base_operation.rb', line 27

def body
  raise NotImplemented
end

#intent_idString

Sets an unique identifier to the query

Returns:

  • (String)


56
57
58
# File 'lib/zenaton/services/graph_ql/base_operation.rb', line 56

def intent_id
  SecureRandom.uuid
end

#queryString

Removes duplicate white space from the raw_query

Returns:

  • (String)


50
51
52
# File 'lib/zenaton/services/graph_ql/base_operation.rb', line 50

def query
  raw_query.gsub(/\s+/, ' ')
end

#raw_queryObject

To be implemented in subclasses. The actual GraphQL query

Raises:



34
35
36
# File 'lib/zenaton/services/graph_ql/base_operation.rb', line 34

def raw_query
  raise NotImplemented
end

#result(response) ⇒ Object

Default implementation for parsing GraphQL responses Override in subclasses if needed.

Raises:



41
42
43
44
45
46
# 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