class ActiveGraphQL::Query

Attributes

action[RW]
config[RW]
graph[RW]
locale[RW]
params[RW]
response[RW]

Public Instance Methods

get(*graph) click to toggle source
# 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() click to toggle source
# File lib/activegraphql/query.rb, line 59
def qaction
  action.to_s.camelize(:lower)
end
qargument(value) click to toggle source
# File lib/activegraphql/query.rb, line 73
def qargument(value)
  case value
  when Array
    "[#{value.map{ |v| qargument(v) }.join(', ')}]"
  when String
    "\"#{value}\""
  else
    value.to_s
  end
end
qgraph(graph) click to toggle source
# File lib/activegraphql/query.rb, line 84
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() click to toggle source
# 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)}: #{qargument(v)}"
  end

  param_strings.join(', ')
end
request_headers() click to toggle source
# 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_options() click to toggle source
# 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_params() click to toggle source
# File lib/activegraphql/query.rb, line 35
def request_params
  { query: to_s }
end
response_data() click to toggle source
# File lib/activegraphql/query.rb, line 39
def response_data
  return unless response['data']
  to_snake_case(response['data'][qaction])
end
response_error_messages() click to toggle source
# File lib/activegraphql/query.rb, line 48
def response_error_messages
  response_errors.map { |e| e[:message] }
end
response_errors() click to toggle source
# File lib/activegraphql/query.rb, line 44
def response_errors
  to_snake_case(response['errors'])
end
to_s() click to toggle source
# File lib/activegraphql/query.rb, line 52
def to_s
  str = "{ #{qaction}"
  str << (qparams.present? ? "(#{qparams}) {" : ' {')
  str << " #{qgraph(graph)} } }"
  str
end

Private Instance Methods

auth_config() click to toggle source
# File lib/activegraphql/query.rb, line 103
def auth_config
  @auth_config ||= config[:auth] || {}
end
auth_header?() click to toggle source
# File lib/activegraphql/query.rb, line 99
def auth_header?
  auth_strategy == :bearer
end
auth_strategy() click to toggle source
# File lib/activegraphql/query.rb, line 107
def auth_strategy
  @auth_strategy ||= auth_config[:strategy]
end
auth_token() click to toggle source

ActiveGraphQL currently supports bearer authorization with given class to encode. So if the “bearer” is not configured or the “class” is not present it's returning a nil token.

# File lib/activegraphql/query.rb, line 114
def auth_token
  return if auth_config[:strategy] != :bearer || auth_config[:class].blank?
  @auth_token ||= auth_config[:class].encode
end
to_snake_case(value) click to toggle source
# File lib/activegraphql/query.rb, line 119
def to_snake_case(value)
  case value
  when Array
    value.map { |v| to_snake_case(v) }
  when Hash
    Hash[value.map { |k, v| [k.to_s.underscore.to_sym, to_snake_case(v)] }]
  else
    value
  end
end