Module: Graphtown::QueryBuilder

Extended by:
ActiveSupport::Concern
Defined in:
lib/graphtown/query_builder.rb

Defined Under Namespace

Classes: Queries

Instance Method Summary collapse

Instance Method Details

#configure_graphql_client(client) ⇒ Object

override in concrete class if need be



64
# File 'lib/graphtown/query_builder.rb', line 64

def configure_graphql_client(client); end

#graphql_clientObject



50
51
52
53
54
# 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_endpointObject

override in concrete class if need be



57
58
59
60
61
# 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) ⇒ Object



46
47
48
# 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

#queriesObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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