Skip to content
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

Use data loader #242

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,4 @@ GraphQL/ObjectDescription:
Exclude:
- app/graphql/application_schema.rb
- app/graphql/types/base_**.rb
- app/graphql/loaders/**/*
- app/graphql/sources/**/*
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ gem "bootsnap", require: false
gem "enumerize"
gem "google-api-client"
gem "graphql"
gem "graphql-batch"
gem "graphql-persisted_queries"
gem "graphql-rails_logger"
gem "health_check"
Expand Down
5 changes: 0 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,6 @@ GEM
os (>= 0.9, < 2.0)
signet (>= 0.16, < 2.a)
graphql (2.0.22)
graphql-batch (0.5.3)
graphql (>= 1.12.18, < 3)
promise.rb (~> 0.7.2)
graphql-persisted_queries (1.7.0)
graphql (>= 1.12)
graphql-rails_logger (1.2.4)
Expand Down Expand Up @@ -234,7 +231,6 @@ GEM
parser (3.2.2.1)
ast (~> 2.4.1)
pg (1.5.3)
promise.rb (0.7.4)
public_suffix (5.0.1)
puma (6.2.2)
nio4r (~> 2.0)
Expand Down Expand Up @@ -392,7 +388,6 @@ DEPENDENCIES
ffaker
google-api-client
graphql
graphql-batch
graphql-persisted_queries
graphql-rails_logger
health_check
Expand Down
4 changes: 1 addition & 3 deletions app/graphql/application_schema.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
require "graphql/batch"

class ApplicationSchema < GraphQL::Schema
mutation(Types::MutationType)
query(Types::QueryType)

use GraphQL::Batch
use GraphQL::Dataloader
use GraphQL::PersistedQueries,
compiled_queries: true,
store: :redis_with_local_cache,
Expand Down
51 changes: 0 additions & 51 deletions app/graphql/loaders/association_loader.rb

This file was deleted.

14 changes: 0 additions & 14 deletions app/graphql/loaders/record_loader.rb

This file was deleted.

13 changes: 13 additions & 0 deletions app/graphql/sources/active_record_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Sources
class ActiveRecordObject < GraphQL::Dataloader::Source
def initialize(model_class)
super()
@model_class = model_class
end

def fetch(ids)
records = @model_class.where(id: ids)
ids.map { |id| records.find { |r| r.id == id.to_i } }
end
end
end
2 changes: 1 addition & 1 deletion app/graphql/types/activity_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ActivityType < Types::BaseObject
field :user, Types::UserType, "Activity initiator", null: false

def user
Loaders::RecordLoader.for(User).load(object.user_id)
dataloader.with(Sources::ActiveRecordObject, User).load(object.user_id)
end
end
end
5 changes: 5 additions & 0 deletions app/graphql/types/public_activity_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ class PublicActivityType < Types::BaseObject

field :body, String, "Body", null: false
field :title, String, "Title", null: false
field :user, Types::UserType, "Activity initiator", null: false

def user
dataloader.with(Sources::ActiveRecordObject, User).load(object.user_id)
end
end
end
10 changes: 7 additions & 3 deletions spec/graphql/types/activity_type_n_plus_one_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

describe Types::ActivityType, :n_plus_one do
describe Types::ActivityType do
let!(:user) { create(:user) }

let(:query) do
Expand All @@ -21,7 +21,11 @@
GRAPHQL
end

populate { |n| create_list(:activity, n, user: user) }
it_behaves_like "succeed graphql request"

include_examples "performs constant number of queries request"
context "when N+1", :n_plus_one do
populate { |n| create_list(:activity, n, user: user) }

include_examples "performs constant number of queries request"
end
end
7 changes: 7 additions & 0 deletions spec/support/shared_examples/succeed_graphql_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
shared_examples "succeed graphql request" do
let(:response) { ApplicationSchema.execute(query).as_json }

it "succeeds" do
expect(response["errors"]).to be_blank
end
end