class GraphQLIncludable::IncludesBuilder

Attributes

included_path[R]
includes[R]

Public Class Methods

new(only_one_path: true) click to toggle source
# File lib/graphql_includable/includes_builder.rb, line 5
def initialize(only_one_path: true)
  @only_one_path = only_one_path
  @included_path = []
  @includes = GraphQLIncludable::Includes.new(nil)
end

Public Instance Methods

active_record_includes() click to toggle source
# File lib/graphql_includable/includes_builder.rb, line 15
def active_record_includes
  @includes.active_record_includes
end
includes?() click to toggle source
# File lib/graphql_includable/includes_builder.rb, line 11
def includes?
  @included_path.present?
end
path(*symbols, &block) click to toggle source
# File lib/graphql_includable/includes_builder.rb, line 23
def path(*symbols, &block)
  raise ArgumentError, 'Can only add path once' if @included_path.present? && @only_one_path

  if symbols.present?
    first, *rest = symbols
    includes = @includes.add_child(first)
    rest.each do |key|
      includes = includes.add_child(key)
    end
  else
    includes = @includes
  end

  if block_given?
    nested = GraphQLIncludable::IncludesBuilder.new
    nested.instance_eval(&block)
    symbols += nested.included_path
    includes.merge_includes(nested.includes)
  end
  @included_path = symbols
end
path_leaf_includes() click to toggle source
# File lib/graphql_includable/includes_builder.rb, line 19
def path_leaf_includes
  @includes.dig(included_path)
end
sibling_path(*symbols, &block) click to toggle source
# File lib/graphql_includable/includes_builder.rb, line 45
def sibling_path(*symbols, &block)
  if symbols.present?
    first, *rest = symbols
    includes = @includes.add_child(first)
    rest.each do |key|
      includes = includes.add_child(key)
    end
  else
    includes = @includes
  end

  return unless block_given?
  nested = GraphQLIncludable::IncludesBuilder.new(only_one_path: false)
  nested.instance_eval(&block)
  includes.merge_includes(nested.includes)
end