class Jeql::GraphqlBlock

Constants

GraphQlError
PARAMS_SYNTAX

Public Class Methods

new(tag_name, text, tokens) click to toggle source
Calls superclass method
# File lib/jeql/graphql_block.rb, line 5
def initialize(tag_name, text, tokens)
  super
  @params = text.scan(PARAMS_SYNTAX)
  @text = text
end

Public Instance Methods

render(context) click to toggle source
Calls superclass method
# File lib/jeql/graphql_block.rb, line 11
def render(context)
  hash_params = Hash[@params]

  endpoint_config = context.registers[:site].config["jeql"][hash_params["endpoint"]]
  query = Jeql::Query.new(hash_params["query"], context.registers[:site].config["source"], endpoint_config)
  if query.response.success?
    context['data'] = JSON.parse(query.response.body)['data']
    super
  else
    raise GraphQlError, "The query #{query.query_name} failed"
  end
end