Class: ActiveGraphQL::Query

Inherits:
Support::Fancy show all
Defined in:
lib/activegraphql/query.rb

Defined Under Namespace

Classes: ServerError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Support::Fancy

#initialize

Constructor Details

This class inherits a constructor from ActiveGraphQL::Support::Fancy

Instance Attribute Details

#actionObject

Returns the value of attribute action.



7
8
9
# File 'lib/activegraphql/query.rb', line 7

def action
  @action
end

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/activegraphql/query.rb', line 7

def config
  @config
end

#graphObject

Returns the value of attribute graph.



7
8
9
# File 'lib/activegraphql/query.rb', line 7

def graph
  @graph
end

#localeObject

Returns the value of attribute locale.



7
8
9
# File 'lib/activegraphql/query.rb', line 7

def locale
  @locale
end

#paramsObject

Returns the value of attribute params.



7
8
9
# File 'lib/activegraphql/query.rb', line 7

def params
  @params
end

#responseObject

Returns the value of attribute response.



7
8
9
# File 'lib/activegraphql/query.rb', line 7

def response
  @response
end

Instance Method Details

#get(*graph) ⇒ Object

Raises:



11
12
13
14
15
16
17
18
# File 'lib/activegraphql/query.rb', line 11

def get(*graph)
  self.graph = graph

  self.response = HTTParty.get(config[:url], request_options)

  raise(ServerError, response_error_messages) if response_errors.present?
  response_data
end

#qactionObject



59
60
61
# File 'lib/activegraphql/query.rb', line 59

def qaction
  action.to_s.camelize(:lower)
end

#qgraph(graph) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/activegraphql/query.rb', line 73

def qgraph(graph)
  graph_strings = graph.map do |item|
    case item
    when Symbol
      item.to_s.camelize(:lower)
    when Hash
      item.map { |k, v| "#{k.to_s.camelize(:lower)} { #{qgraph(v)} }" }
    end
  end

  graph_strings.join(', ')
end

#qparamsObject



63
64
65
66
67
68
69
70
71
# File 'lib/activegraphql/query.rb', line 63

def qparams
  return if params.blank?

  param_strings = params.map do |k, v|
    "#{k.to_s.camelize(:lower)}: \"#{v}\""
  end

  param_strings.join(', ')
end

#request_headersObject



28
29
30
31
32
33
# File 'lib/activegraphql/query.rb', line 28

def request_headers
  {}.tap do |headers|
    headers['Authorization'] = "Bearer #{auth_token}" if auth_header?
    headers['Accept-Language'] = locale.to_s if locale.present?
  end
end

#request_optionsObject



20
21
22
23
24
25
26
# File 'lib/activegraphql/query.rb', line 20

def request_options
  {}.tap do |opts|
    opts[:query] = request_params
    opts[:headers] = request_headers if request_headers.present?
    opts.merge!(config[:http]) if config[:http].present?
  end
end

#request_paramsObject



35
36
37
# File 'lib/activegraphql/query.rb', line 35

def request_params
  { query: to_s }
end

#response_dataObject



39
40
41
42
# File 'lib/activegraphql/query.rb', line 39

def response_data
  return unless response['data']
  to_snake_case(response['data'][qaction])
end

#response_error_messagesObject



48
49
50
# File 'lib/activegraphql/query.rb', line 48

def response_error_messages
  response_errors.map { |e| e[:message] }
end

#response_errorsObject



44
45
46
# File 'lib/activegraphql/query.rb', line 44

def response_errors
  to_snake_case(response['errors'])
end

#to_sObject



52
53
54
55
56
57
# File 'lib/activegraphql/query.rb', line 52

def to_s
  str = "{ #{qaction}"
  str << (qparams.present? ? "(#{qparams}) {" : ' {')
  str << " #{qgraph(graph)} } }"
  str
end