-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Improve built-in Rails support for Dataloader #5213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
569f58e
Add ActiveRecordSource
rmosolgo d036245
Add ActiveRecordAssociationSource
rmosolgo e2c45a0
Add HasDataloader
rmosolgo a43bf11
Merge branch 'master' into rails-dataloader
rmosolgo 0aa0153
Merge branch 'master' into rails-dataloader
rmosolgo 746b0df
Fix merge
rmosolgo 6e8bea2
Fix loading by casted key
rmosolgo 2d70803
Fix attribute access for Rails 7
rmosolgo 7938781
Fix lint error
rmosolgo d787b48
Bypass sync when association is already loaded
rmosolgo a26bf7b
Add test coverage for dataloader helpers
rmosolgo 217ae2b
Merge branch 'master' into rails-dataloader
rmosolgo 8172088
improve test coverage
rmosolgo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
64 changes: 64 additions & 0 deletions
64
lib/graphql/dataloader/active_record_association_source.rb
This file contains hidden or 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,64 @@ | ||
| # frozen_string_literal: true | ||
| require "graphql/dataloader/source" | ||
| require "graphql/dataloader/active_record_source" | ||
|
|
||
| module GraphQL | ||
| class Dataloader | ||
| class ActiveRecordAssociationSource < GraphQL::Dataloader::Source | ||
| RECORD_SOURCE_CLASS = ActiveRecordSource | ||
|
|
||
| def initialize(association, scope: nil) | ||
| @association = association | ||
| @scope = nil | ||
| end | ||
|
|
||
| def load(record) | ||
| if (assoc = record.association(@association))&.loaded? | ||
| assoc.target | ||
| else | ||
| super | ||
| end | ||
| end | ||
|
|
||
| def fetch(records) | ||
| record_classes = Set.new.compare_by_identity | ||
| associated_classes = Set.new.compare_by_identity | ||
| records.each do |record| | ||
| if record_classes.add?(record.class) | ||
| reflection = record.class.reflect_on_association(@association) | ||
| if !reflection.polymorphic? && reflection.klass | ||
rmosolgo marked this conversation as resolved.
Show resolved
Hide resolved
rmosolgo marked this conversation as resolved.
Show resolved
Hide resolved
rmosolgo marked this conversation as resolved.
Show resolved
Hide resolved
rmosolgo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| associated_classes.add(reflection.klass) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| available_records = [] | ||
| associated_classes.each do |assoc_class| | ||
| already_loaded_records = dataloader.with(RECORD_SOURCE_CLASS, assoc_class).results.values | ||
| available_records.concat(already_loaded_records) | ||
| end | ||
|
|
||
| ::ActiveRecord::Associations::Preloader.new(records: records, associations: @association, available_records: available_records, scope: @scope).call | ||
|
|
||
| loaded_associated_records = records.map { |r| r.public_send(@association) } | ||
| records_by_model = {} | ||
| loaded_associated_records.each do |record| | ||
| if record | ||
rmosolgo marked this conversation as resolved.
Show resolved
Hide resolved
rmosolgo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| updates = records_by_model[record.class] ||= {} | ||
| updates[record.id] = record | ||
| end | ||
| end | ||
|
|
||
| if @scope.nil? | ||
| # Don't cache records loaded via scope because they might have reduced `SELECT`s | ||
| # Could check .select_values here? | ||
| records_by_model.each do |model_class, updates| | ||
| dataloader.with(RECORD_SOURCE_CLASS, model_class).merge(updates) | ||
| end | ||
| end | ||
|
|
||
| loaded_associated_records | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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,26 @@ | ||
| # frozen_string_literal: true | ||
| require "graphql/dataloader/source" | ||
|
|
||
| module GraphQL | ||
| class Dataloader | ||
| class ActiveRecordSource < GraphQL::Dataloader::Source | ||
| def initialize(model_class, find_by: model_class.primary_key) | ||
| @model_class = model_class | ||
| @find_by = find_by | ||
| @type_for_column = @model_class.type_for_attribute(@find_by) | ||
| end | ||
|
|
||
| def load(requested_key) | ||
| casted_key = @type_for_column.cast(requested_key) | ||
| super(casted_key) | ||
| end | ||
|
|
||
| def fetch(record_ids) | ||
| records = @model_class.where(@find_by => record_ids) | ||
| record_lookup = {} | ||
| records.each { |r| record_lookup[r.public_send(@find_by)] = r } | ||
| record_ids.map { |id| record_lookup[id] } | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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,47 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module GraphQL | ||
| class Schema | ||
| class Member | ||
| module HasDataloader | ||
| # @return [GraphQL::Dataloader] The dataloader for the currently-running query | ||
| def dataloader | ||
| context.dataloader | ||
| end | ||
|
|
||
| # Find an object with ActiveRecord via {Dataloader::ActiveRecordSource}. | ||
| # @param model [Class<ActiveRecord::Base>] | ||
| # @param find_by_value [Object] Usually an `id`, might be another value if `find_by:` is also provided | ||
| # @param find_by [Symbol, String] A column name to look the record up by. (Defaults to the model's primary key.) | ||
| # @return [ActiveRecord::Base, nil] | ||
| def dataload_record(model, find_by_value, find_by: nil) | ||
| source = if find_by | ||
rmosolgo marked this conversation as resolved.
Show resolved
Hide resolved
rmosolgo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| dataloader.with(Dataloader::ActiveRecordSource, model, find_by: find_by) | ||
| else | ||
| dataloader.with(Dataloader::ActiveRecordSource, model) | ||
| end | ||
|
|
||
| source.load(find_by_value) | ||
| end | ||
|
|
||
| # Look up an associated record using a Rails association. | ||
| # @param association_name [Symbol] A `belongs_to` or `has_one` association. (If a `has_many` association is named here, it will be selected without pagination.) | ||
| # @param record [ActiveRecord::Base] The object that the association belongs to. | ||
| # @param scope [ActiveRecord::Relation] A scope to look up the associated record in | ||
| # @return [ActiveRecord::Base, nil] The associated record, if there is one | ||
| # @example Looking up a belongs_to on the current object | ||
| # dataload_association(:parent) # Equivalent to `object.parent`, but dataloaded | ||
| # @example Looking up an associated record on some other object | ||
| # dataload_association(:post, comment) # Equivalent to `comment.post`, but dataloaded | ||
| def dataload_association(association_name, record = object, scope: nil) | ||
| source = if scope | ||
rmosolgo marked this conversation as resolved.
Show resolved
Hide resolved
rmosolgo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| dataloader.with(Dataloader::ActiveRecordAssociationSource, association_name, scope: scope) | ||
| else | ||
| dataloader.with(Dataloader::ActiveRecordAssociationSource, association_name) | ||
| end | ||
| source.load(record) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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
74 changes: 74 additions & 0 deletions
74
spec/graphql/dataloader/active_record_association_source_spec.rb
This file contains hidden or 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,74 @@ | ||
| # frozen_string_literal: true | ||
| require "spec_helper" | ||
|
|
||
| describe GraphQL::Dataloader::ActiveRecordAssociationSource do | ||
| if testing_rails? | ||
| it_dataloads "queries for associated records when the association isn't already loaded" do |d| | ||
| my_first_car = ::Album.find(2) | ||
| homey = ::Album.find(4) | ||
| log = with_active_record_log(colorize: false) do | ||
| vulfpeck, chon = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :band).load_all([my_first_car, homey]) | ||
| assert_equal "Vulfpeck", vulfpeck.name | ||
| assert_equal "Chon", chon.name | ||
| end | ||
|
|
||
| assert_includes log, '[["id", 1], ["id", 3]]' | ||
|
|
||
| toms_story = ::Album.find(3) | ||
| log = with_active_record_log(colorize: false) do | ||
| vulfpeck, chon, toms_story_band = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :band).load_all([my_first_car, homey, toms_story]) | ||
| assert_equal "Vulfpeck", vulfpeck.name | ||
| assert_equal "Chon", chon.name | ||
| assert_equal "Tom's Story", toms_story_band.name | ||
| end | ||
|
|
||
| assert_includes log, '[["id", 2]]' | ||
| end | ||
|
|
||
| it_dataloads "doesn't load records that are already cached by ActiveRecordSource" do |d| | ||
| d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load_all([1,2,3]) | ||
|
|
||
| my_first_car = ::Album.find(2) | ||
| homey = ::Album.find(4) | ||
| toms_story = ::Album.find(3) | ||
|
|
||
| log = with_active_record_log(colorize: false) do | ||
| vulfpeck, chon, toms_story_band = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :band).load_all([my_first_car, homey, toms_story]) | ||
| assert_equal "Vulfpeck", vulfpeck.name | ||
| assert_equal "Chon", chon.name | ||
| assert_equal "Tom's Story", toms_story_band.name | ||
| end | ||
|
|
||
| assert_equal "", log | ||
| end | ||
|
|
||
| it_dataloads "warms the cache for ActiveRecordSource" do |d| | ||
| my_first_car = ::Album.find(2) | ||
| homey = ::Album.find(4) | ||
| toms_story = ::Album.find(3) | ||
| d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :band).load_all([my_first_car, homey, toms_story]) | ||
|
|
||
| log = with_active_record_log(colorize: false) do | ||
| d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load_all([1,2,3]) | ||
| end | ||
|
|
||
| assert_equal "", log | ||
| end | ||
|
|
||
| it_dataloads "doesn't pause when the association is already loaded" do |d| | ||
| source = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :band) | ||
| assert_equal 0, source.results.size | ||
| assert_equal 0, source.pending.size | ||
|
|
||
| my_first_car = ::Album.find(2) | ||
| vulfpeck = my_first_car.band | ||
|
|
||
| vulfpeck2 = source.load(my_first_car) | ||
|
|
||
| assert_equal vulfpeck, vulfpeck2 | ||
|
|
||
| assert_equal 0, source.results.size | ||
| assert_equal 0, source.pending.size | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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,98 @@ | ||
| # frozen_string_literal: true | ||
| require "spec_helper" | ||
|
|
||
| describe GraphQL::Dataloader::ActiveRecordSource do | ||
| if testing_rails? | ||
| describe "finding by ID" do | ||
| it_dataloads "loads once, then returns from a cache when available" do |d| | ||
| log = with_active_record_log(colorize: false) do | ||
| r1 = d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load(1) | ||
| assert_equal "Vulfpeck", r1.name | ||
| end | ||
|
|
||
| assert_includes log, 'SELECT "bands".* FROM "bands" WHERE "bands"."id" = ? [["id", 1]]' | ||
|
|
||
| log = with_active_record_log(colorize: false) do | ||
| r1 = d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load(1) | ||
| assert_equal "Vulfpeck", r1.name | ||
| end | ||
|
|
||
| assert_equal "", log | ||
|
|
||
| log = with_active_record_log(colorize: false) do | ||
| records = d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load_all([1, 99, 2, 3]) | ||
| assert_equal ["Vulfpeck", nil, "Tom's Story", "Chon"], records.map { |r| r&.name } | ||
| end | ||
|
|
||
| assert_includes log, '[["id", 99], ["id", 2], ["id", 3]]' | ||
| end | ||
|
|
||
| it_dataloads "casts load values to the column type" do |d| | ||
| log = with_active_record_log(colorize: false) do | ||
| r1 = d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load("1") | ||
| assert_equal "Vulfpeck", r1.name | ||
| end | ||
|
|
||
| assert_includes log, 'SELECT "bands".* FROM "bands" WHERE "bands"."id" = ? [["id", 1]]' | ||
|
|
||
| log = with_active_record_log(colorize: false) do | ||
| d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load(1) | ||
| end | ||
|
|
||
| assert_equal "", log | ||
|
|
||
| log = with_active_record_log(colorize: false) do | ||
| d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load("1") | ||
| end | ||
|
|
||
| assert_equal "", log | ||
| end | ||
| end | ||
|
|
||
| describe "finding by other columns" do | ||
| it_dataloads "uses the alternative primary key" do |d| | ||
| log = with_active_record_log(colorize: false) do | ||
| r1 = d.with(GraphQL::Dataloader::ActiveRecordSource, AlternativeBand).load("Vulfpeck") | ||
| assert_equal "Vulfpeck", r1.name | ||
| if Rails::VERSION::STRING > "8" | ||
| assert_equal 1, r1["id"] | ||
| else | ||
| assert_equal 1, r1._read_attribute("id") | ||
| end | ||
| end | ||
|
|
||
| assert_includes log, 'SELECT "bands".* FROM "bands" WHERE "bands"."name" = ? [["name", "Vulfpeck"]]' | ||
| end | ||
|
|
||
| it_dataloads "uses specified find_by columns" do |d| | ||
| log = with_active_record_log(colorize: false) do | ||
| r1 = d.with(GraphQL::Dataloader::ActiveRecordSource, Band, find_by: :name).load("Chon") | ||
| assert_equal "Chon", r1.name | ||
| assert_equal 3, r1.id | ||
| end | ||
|
|
||
| assert_includes log, 'SELECT "bands".* FROM "bands" WHERE "bands"."name" = ? [["name", "Chon"]]' | ||
| end | ||
| end | ||
|
|
||
| describe "warming the cache" do | ||
| it_dataloads "can receive passed-in objects with a class" do |d| | ||
| d.with(GraphQL::Dataloader::ActiveRecordSource, Band).merge({ 100 => Band.find(3) }) | ||
| log = with_active_record_log(colorize: false) do | ||
| band3 = d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load(100) | ||
| assert_equal "Chon", band3.name | ||
| assert_equal 3, band3.id | ||
| end | ||
|
|
||
| assert_equal "", log | ||
| end | ||
| it "can infer class of passed-in objects" | ||
| end | ||
|
|
||
| describe "in queries" do | ||
| it "loads records with dataload_record" | ||
|
|
||
| it "accepts custom find-by with dataload_record" | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.