Skip to content

Commit 2c551e7

Browse files
committed
Use the dataloader and remove dependency on graphql-batch
1 parent c245d85 commit 2c551e7

File tree

6 files changed

+34
-46
lines changed

6 files changed

+34
-46
lines changed

README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ A collection of utilities for building GraphQL APIs.
1414
- [Installation](#installation)
1515
- [Usage](#usage)
1616
- [GraphQL::Extras::Controller](#graphqlextrascontroller)
17-
- [GraphQL::Extras::AssociationLoader](#graphqlextrasassociationloader)
1817
- [GraphQL::Extras::Preload](#graphqlextraspreload)
18+
- [GraphQL::Extras::PreloadSource](#graphqlextraspreloadsource)
1919
- [GraphQL::Extras::Types](#graphqlextrastypes)
2020
- [Date](#date)
2121
- [DateTime](#datetime)
@@ -53,21 +53,15 @@ class GraphqlController < ApplicationController
5353
end
5454
```
5555

56-
### GraphQL::Extras::AssociationLoader
57-
58-
This is a subclass of [`GraphQL::Batch::Loader`](https://github.com/Shopify/graphql-batch) that performs eager loading of Active Record associations.
59-
60-
```ruby
61-
loader = GraphQL::Extras::AssociationLoader.for(:blog)
62-
loader.load(Post.first)
63-
loader.load_many(Post.all)
64-
```
65-
6656
### GraphQL::Extras::Preload
6757

6858
This allows you to preload associations before resolving fields.
6959

7060
```ruby
61+
class Schema < GraphQL::Schema
62+
use GraphQL::Dataloader
63+
end
64+
7165
class BaseField < GraphQL::Schema::Field
7266
prepend GraphQL::Extras::Preload
7367
end
@@ -87,6 +81,16 @@ class PostType < BaseObject
8781
end
8882
```
8983

84+
### GraphQL::Extras::PreloadSource
85+
86+
This is a subclass of [`GraphQL::Dataloader::Source`](https://graphql-ruby.org/dataloader/overview.html) that performs eager loading of Active Record associations.
87+
88+
```ruby
89+
loader = dataloader.with(GraphQL::Extras::PreloadSource, :blog)
90+
loader.load(Post.first)
91+
loader.load_many(Post.all)
92+
```
93+
9094
### GraphQL::Extras::Types
9195

9296
In your base classes, you should include the `GraphQL::Extras::Types`.

graphql-extras.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ Gem::Specification.new do |spec|
2626

2727
spec.add_dependency "activesupport", ">= 5.2"
2828
spec.add_dependency "graphql", "~> 1.12"
29-
spec.add_dependency "graphql-batch", "~> 0.4"
3029

3130
spec.add_development_dependency "bundler", "~> 2.0"
3231
spec.add_development_dependency "rake", "~> 13.0"

lib/graphql/extras.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require "graphql/extras/version"
22
require "graphql/extras/types"
33
require "graphql/extras/controller"
4-
require "graphql/extras/association_loader"
54
require "graphql/extras/preload"
65

76
module GraphQL

lib/graphql/extras/association_loader.rb

Lines changed: 0 additions & 24 deletions
This file was deleted.

lib/graphql/extras/preload.rb

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
require "graphql/extras/association_loader"
2-
31
module GraphQL
42
module Extras
3+
class PreloadSource < GraphQL::Dataloader::Source
4+
def initialize(preload)
5+
@preload = preload
6+
end
7+
8+
def fetch(records)
9+
preloader = ActiveRecord::Associations::Preloader.new
10+
preloader.preload(records, @preload)
11+
records
12+
end
13+
end
14+
515
module Preload
616
# @override
717
def initialize(*args, preload: nil, **opts, &block)
@@ -10,13 +20,13 @@ def initialize(*args, preload: nil, **opts, &block)
1020
end
1121

1222
# @override
13-
def resolve(object, args, ctx)
14-
return super unless @preload
15-
16-
loader = AssociationLoader.for(@preload)
17-
loader.load(object.object).then do
18-
super(object, args, ctx)
23+
def resolve(object, args, context)
24+
if @preload
25+
loader = context.dataloader.with(PreloadSource, @preload)
26+
loader.load(object.object)
1927
end
28+
29+
super
2030
end
2131
end
2232
end

spec/graphql/extras/preload_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def bars_batched; Bar.all; end
3434

3535
class BatchSchema < GraphQL::Schema
3636
query BatchQueryType
37-
use GraphQL::Batch
37+
use GraphQL::Dataloader
3838
end
3939

4040
before :all do

0 commit comments

Comments
 (0)