Class: ActiveGraphQL::Query
Defined Under Namespace
Classes: ServerError
Instance Attribute Summary collapse
Instance Method Summary
collapse
#initialize
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
7
8
9
|
# File 'lib/activegraphql/query.rb', line 7
def action
@action
end
|
#config ⇒ Object
Returns the value of attribute config.
7
8
9
|
# File 'lib/activegraphql/query.rb', line 7
def config
@config
end
|
#graph ⇒ Object
Returns the value of attribute graph.
7
8
9
|
# File 'lib/activegraphql/query.rb', line 7
def graph
@graph
end
|
#locale ⇒ Object
Returns the value of attribute locale.
7
8
9
|
# File 'lib/activegraphql/query.rb', line 7
def locale
@locale
end
|
#params ⇒ Object
Returns the value of attribute params.
7
8
9
|
# File 'lib/activegraphql/query.rb', line 7
def params
@params
end
|
#response ⇒ Object
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
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
|
#qaction ⇒ Object
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
|
#qparams ⇒ Object
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
|
28
29
30
31
32
33
|
# File 'lib/activegraphql/query.rb', line 28
def
{}.tap do ||
['Authorization'] = "Bearer #{auth_token}" if
['Accept-Language'] = locale.to_s if locale.present?
end
end
|
#request_options ⇒ Object
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] = if .present?
opts.merge!(config[:http]) if config[:http].present?
end
end
|
#request_params ⇒ Object
35
36
37
|
# File 'lib/activegraphql/query.rb', line 35
def request_params
{ query: to_s }
end
|
#response_data ⇒ Object
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_messages ⇒ Object
48
49
50
|
# File 'lib/activegraphql/query.rb', line 48
def response_error_messages
response_errors.map { |e| e[:message] }
end
|
#response_errors ⇒ Object
44
45
46
|
# File 'lib/activegraphql/query.rb', line 44
def response_errors
to_snake_case(response['errors'])
end
|
#to_s ⇒ Object
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
|