module Graphtown::QueryBuilder

Public Instance Methods

configure_graphql_client(client) click to toggle source

override in concrete class if need be

# File lib/graphtown/query_builder.rb, line 64
def configure_graphql_client(client); end
graphql(graph_name, query = nil, &block) click to toggle source
# File lib/graphtown/query_builder.rb, line 15
def graphql(graph_name, query = nil, &block)
  self.graphql_queries ||= Queries.new
  graphql_queries.send("#{graph_name}=", query || block)
end
graphql_client() click to toggle source
# File lib/graphtown/query_builder.rb, line 50
def graphql_client
  Graphlient::Client.new(graphql_endpoint) do |client|
    configure_graphql_client(client)
  end
end
graphql_endpoint() click to toggle source

override in concrete class if need be

# File lib/graphtown/query_builder.rb, line 57
def graphql_endpoint
  raise "Please add graphql_endpoint to your config file" unless config[:graphql_endpoint]

  config[:graphql_endpoint]
end
parse_graphql_query(client, value) click to toggle source
# File lib/graphtown/query_builder.rb, line 46
def parse_graphql_query(client, value)
  value.is_a?(Proc) ? Graphlient::Query.new(&value).to_s : client.parse(value)
end
queries() click to toggle source
# File lib/graphtown/query_builder.rb, line 21
def queries
  return self.class.graphql_queries if @_executed_queries

  client = graphql_client
  self.class.graphql_queries.each_pair do |graph_name, value|
    query = parse_graphql_query(client, value)
    query_variables = if respond_to?("variables_for_#{graph_name}")
                        send("variables_for_#{graph_name}")
                      end

    self.class.graphql_queries.send(
      "#{graph_name}=",
      client.execute(query, query_variables).data.yield_self do |data|
        # avoid having to do query.foo.foo if possible
        data.send(graph_name)
      rescue NoMethodError
        data
      end
    )
  end

  @_executed_queries = true
  self.class.graphql_queries
end