Module: Rails::GraphQL::Source::ActiveRecordSource::Builders

Included in:
Rails::GraphQL::Source::ActiveRecordSource
Defined in:
lib/rails/graphql/source/active_record/builders.rb

Overview

All the helper methods for building the source

Instance Method Summary collapse

Instance Method Details

#build_primary_key_arguments(holder) ⇒ Object

Build arguments that correctly reflect the primary key, as a single column or as an array of columns



47
48
49
50
51
52
53
# File 'lib/rails/graphql/source/active_record/builders.rb', line 47

def build_primary_key_arguments(holder)
  if primary_key.is_a?(::Array)
    primary_key.each { |key| holder.argument(key, :id, null: false) }
  else
    holder.argument(primary_key, :id, null: false)
  end
end

#each_attribute(holder, skip_primary_key = true) ⇒ Object

Iterate over all the attributes, except the primary key, from the model but already set to be imported to GraphQL fields TODO: Turn into an enumerator



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rails/graphql/source/active_record/builders.rb', line 23

def each_attribute(holder, skip_primary_key = true)
  adapter_key = GraphQL.ar_adapter_key(adapter_name)

  skip_fields = Set.new
  skip_fields << model.inheritance_column
  skip_fields << primary_key unless skip_primary_key

  send(:"#{adapter_key}_attributes") do |attribute, *args, **xargs|
    yield(attribute, *args, **xargs) unless skip_fields.include?(attribute)
  end
end

#each_reflection(&block) ⇒ Object

Iterate over all the model reflections



36
37
38
39
40
41
42
43
# File 'lib/rails/graphql/source/active_record/builders.rb', line 36

def each_reflection(&block)
  model._reflections.each_value.select do |reflection|
    reflection = model._reflections[reflection.to_s] \
      unless reflection.is_a?(abstract_reflection)

    !reflection.nil?
  end.each(&block)
end

#id_columnsObject

List of all columns that should be threated as IDs TODO: Add a exclusive cache for the build process



9
10
11
12
13
14
15
16
17
18
# File 'lib/rails/graphql/source/active_record/builders.rb', line 9

def id_columns
  @id_columns ||= begin
    result = Set.new(GraphQL.enumerate(primary_key))
    each_reflection.each_with_object(result) do |item, arr|
      next unless item.belongs_to?
      arr << item.foreign_key.to_s
      arr << item.foreign_key if item.polymorphic?
    end
  end
end