-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This support likely allows any association.
- Loading branch information
Showing
6 changed files
with
197 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
graphql-sources (1.4.0) | ||
graphql-sources (1.5.0) | ||
graphql | ||
rails | ||
zeitwerk | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# frozen_string_literal: true | ||
|
||
module GraphQL | ||
module Sources | ||
# A class for loading an ActiveRecord association. | ||
# | ||
# @example `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 | ||
# | ||
# | ||
# @example `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 | ||
# | ||
# @example `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 | ||
class ActiveRecordAssociation < GraphQL::Dataloader::Source | ||
# @param association [Symbol] an association to use for loading (e.g. :user) | ||
def initialize(association) | ||
super() | ||
@association = association | ||
end | ||
|
||
# @param records [Array<ActiveRecord::Base>] an array of records | ||
def fetch(records) | ||
preloader = ActiveRecord::Associations::Preloader.new(records: records, associations: [@association]) | ||
preloader.call | ||
|
||
records.map { |record| record.association(@association).target } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,6 @@ | |
|
||
module GraphQL | ||
module Sources | ||
VERSION = '1.4.0' | ||
VERSION = '1.5.0' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe GraphQL::Sources::ActiveRecordAssociation do | ||
describe '#fetch' do | ||
subject(:result) do | ||
GraphQL::Dataloader.with_dataloading do |dataloader| | ||
dataloader | ||
.with(described_class, :comments) | ||
.request(user) | ||
.load | ||
end | ||
end | ||
|
||
let!(:user) { create(:user) } | ||
let!(:comments) { create_pair(:comment, user: user) } | ||
|
||
it 'loads many records' do | ||
expect(result).to match_array(comments) | ||
end | ||
end | ||
end |