Class: GraphQL::Groups::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/groups/query_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lookahead, object, context) ⇒ QueryBuilder

Returns a new instance of QueryBuilder.



10
11
12
13
14
15
16
# File 'lib/graphql/groups/query_builder.rb', line 10

def initialize(lookahead, object, context)
  @lookahead = lookahead
  @context = context
  type = @lookahead.field.type.of_type
  @base_query = proc { type.authorized_new(object, context).scope }
  super()
end

Class Method Details

.parse(lookahead, object, context) ⇒ Object



6
7
8
# File 'lib/graphql/groups/query_builder.rb', line 6

def self.parse(lookahead, object, context)
  QueryBuilder.new(lookahead, object, context).group_selections
end

Instance Method Details

#group_selections(lookahead = @lookahead, current_context = QueryBuilderContext.new([], @base_query)) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/graphql/groups/query_builder.rb', line 18

def group_selections(lookahead = @lookahead, current_context = QueryBuilderContext.new([], @base_query))
  selections = lookahead.selections
  group_field_type = lookahead.field.type.of_type.field_class
  group_selections = selections.select { |selection| selection.field.is_a?(group_field_type) }
  queries = group_selections.each_with_object([]) do |selection, object|
    field_proc = proc_from_selection(selection.field, selection.arguments)
    context = current_context.update(selection.name, field_proc)
    object << create_pending_queries(selection, context)
  end
  nested_queries = group_selections
                     .filter { |selection| selection.selects?(:group_by) }
                     .each_with_object([]) do |selection, object|
    field_proc = proc_from_selection(selection.field, selection.arguments)
    context = current_context.update(selection.name, field_proc)
    object << group_selections(selection.selection(:group_by), context)
  end
  (queries + nested_queries).flatten
end