lib/graphtown/query_builder.rb in graphtown-1.0.1 vs lib/graphtown/query_builder.rb in graphtown-1.1.0

- old
+ new

@@ -10,39 +10,42 @@ extend ActiveSupport::Concern class_methods do attr_accessor :graphql_queries - def graphql(graph_name, &block) + def graphql(graph_name, query = nil, &block) self.graphql_queries ||= Queries.new - graphql_queries.send("#{graph_name}=", block) + graphql_queries.send("#{graph_name}=", query || block) end end def queries return self.class.graphql_queries if @_executed_queries client = graphql_client - self.class.graphql_queries.each_pair do |graph_name, block| - graph_dsl = Graphlient::Query.new(&block) - + 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(graph_dsl.to_s, query_variables).data.yield_self do |data| + 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 + + def parse_graphql_query(client, value) + value.is_a?(Proc) ? Graphlient::Query.new(&value).to_s : client.parse(value) end def graphql_client Graphlient::Client.new(graphql_endpoint) do |client| configure_graphql_client(client)