Class: GraphQL::Sources::ActiveRecordAssociation

Inherits:
Dataloader::Source
  • Object
show all
Defined in:
lib/graphql/sources/active_record_association.rb

Overview

A class for loading an ActiveRecord association.

Examples:

‘has_and_belongs_to_many`


class Student
  has_and_belongs_to_many :courses
end

class Course
  has_and_belongs_to_many :students
end

class StudentType < GraphQL::Schema::Object
  field :courses, [CourseType], null: false

  # @return [Array<Course>]
  def courses
    # SELECT "courses_students".* FROM "courses_students" WHERE "courses_students"."student_id" = IN (...)
    # SELECT "courses".* FROM "courses" WHERE "courses"."id" IN (...)
    dataloader
      .with(GraphQL::Sources::ActiveRecordAssociation, :courses)
      .load(object)
  end
end

class CourseType < GraphQL::Schema::Object
  field :students, [StudentType], null: false

  # @return [Array<Student>]
  def students
    # SELECT "courses_students".* FROM "courses_students" WHERE "courses_students"."course_id" = IN (...)
    # SELECT "students".* FROM "students" WHERE "students"."id" IN (...)
    dataloader
      .with(GraphQL::Sources::ActiveRecordAssociation, :students)
      .load(object)
  end
end

‘has_many` / `belongs_to`


class User
  has_many :comments
end

class Comment
  belongs_to :user
end

class UserType < GraphQL::Schema::Object
  field :comments, [CommentType], null: false

  # @return [Array<Comment>]
  def comments
    # SELECT "comments".* FROM "comments" WHERE "comments"."user_id" = IN (...)
    dataloader
      .with(GraphQL::Sources::ActiveRecordAssociation, :comments)
      .load(object)
  end
end

class CommentType < GraphQL::Schema::Object
  field :user, UserType, null: false

  # @return [User]
  def user
    # SELECT "users".* FROM "users" WHERE "users"."id" IN (...)
    dataloader
      .with(GraphQL::Sources::ActiveRecordAssociation, :user)
      .load(object)
  end
end

‘has_one` / `belongs_to`


class User
  has_one :profile
end

class Profile
  belongs_to :user
end

class UserType < GraphQL::Schema::Object
  field :profile, ProfileType, null: false

  # @return [Profile]
  def profile
    # SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = IN (...)
    dataloader
      .with(GraphQL::Sources::ActiveRecordAssociation, :profile)
      .load(object)
  end
end

class ProfileType < GraphQL::Schema::Object
  field :user, UserType, null: false

  # @return [User]
  def user
    # SELECT "users".* FROM "users" WHERE "users"."id" IN (...)
    dataloader
      .with(GraphQL::Sources::ActiveRecordAssociation, :user)
      .load(object)
  end
end

Instance Method Summary collapse

Constructor Details

#initialize(association) ⇒ ActiveRecordAssociation

Returns a new instance of ActiveRecordAssociation.

Parameters:

  • association (Symbol)

    an association to use for loading (e.g. :user)



113
114
115
116
# File 'lib/graphql/sources/active_record_association.rb', line 113

def initialize(association)
  super()
  @association = association
end

Instance Method Details

#fetch(records) ⇒ Object

Parameters:

  • records (Array<ActiveRecord::Base>)

    an array of records



119
120
121
122
123
124
# File 'lib/graphql/sources/active_record_association.rb', line 119

def fetch(records)
  preloader = ActiveRecord::Associations::Preloader.new(records: records, associations: [@association])
  preloader.call

  records.map { |record| record.association(@association).target }
end