Skip to content

Commit

Permalink
Use the dataloader and remove dependency on graphql-batch
Browse files Browse the repository at this point in the history
  • Loading branch information
rzane committed Mar 19, 2021
1 parent c245d85 commit 2c551e7
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 46 deletions.
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ A collection of utilities for building GraphQL APIs.
- [Installation](#installation)
- [Usage](#usage)
- [GraphQL::Extras::Controller](#graphqlextrascontroller)
- [GraphQL::Extras::AssociationLoader](#graphqlextrasassociationloader)
- [GraphQL::Extras::Preload](#graphqlextraspreload)
- [GraphQL::Extras::PreloadSource](#graphqlextraspreloadsource)
- [GraphQL::Extras::Types](#graphqlextrastypes)
- [Date](#date)
- [DateTime](#datetime)
Expand Down Expand Up @@ -53,21 +53,15 @@ class GraphqlController < ApplicationController
end
```

### GraphQL::Extras::AssociationLoader

This is a subclass of [`GraphQL::Batch::Loader`](https://github.com/Shopify/graphql-batch) that performs eager loading of Active Record associations.

```ruby
loader = GraphQL::Extras::AssociationLoader.for(:blog)
loader.load(Post.first)
loader.load_many(Post.all)
```

### GraphQL::Extras::Preload

This allows you to preload associations before resolving fields.

```ruby
class Schema < GraphQL::Schema
use GraphQL::Dataloader
end

class BaseField < GraphQL::Schema::Field
prepend GraphQL::Extras::Preload
end
Expand All @@ -87,6 +81,16 @@ class PostType < BaseObject
end
```

### GraphQL::Extras::PreloadSource

This is a subclass of [`GraphQL::Dataloader::Source`](https://graphql-ruby.org/dataloader/overview.html) that performs eager loading of Active Record associations.

```ruby
loader = dataloader.with(GraphQL::Extras::PreloadSource, :blog)
loader.load(Post.first)
loader.load_many(Post.all)
```

### GraphQL::Extras::Types

In your base classes, you should include the `GraphQL::Extras::Types`.
Expand Down
1 change: 0 additions & 1 deletion graphql-extras.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ Gem::Specification.new do |spec|

spec.add_dependency "activesupport", ">= 5.2"
spec.add_dependency "graphql", "~> 1.12"
spec.add_dependency "graphql-batch", "~> 0.4"

spec.add_development_dependency "bundler", "~> 2.0"
spec.add_development_dependency "rake", "~> 13.0"
Expand Down
1 change: 0 additions & 1 deletion lib/graphql/extras.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require "graphql/extras/version"
require "graphql/extras/types"
require "graphql/extras/controller"
require "graphql/extras/association_loader"
require "graphql/extras/preload"

module GraphQL
Expand Down
24 changes: 0 additions & 24 deletions lib/graphql/extras/association_loader.rb

This file was deleted.

26 changes: 18 additions & 8 deletions lib/graphql/extras/preload.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
require "graphql/extras/association_loader"

module GraphQL
module Extras
class PreloadSource < GraphQL::Dataloader::Source
def initialize(preload)
@preload = preload
end

def fetch(records)
preloader = ActiveRecord::Associations::Preloader.new
preloader.preload(records, @preload)
records
end
end

module Preload
# @override
def initialize(*args, preload: nil, **opts, &block)
Expand All @@ -10,13 +20,13 @@ def initialize(*args, preload: nil, **opts, &block)
end

# @override
def resolve(object, args, ctx)
return super unless @preload

loader = AssociationLoader.for(@preload)
loader.load(object.object).then do
super(object, args, ctx)
def resolve(object, args, context)
if @preload
loader = context.dataloader.with(PreloadSource, @preload)
loader.load(object.object)
end

super
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/graphql/extras/preload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def bars_batched; Bar.all; end

class BatchSchema < GraphQL::Schema
query BatchQueryType
use GraphQL::Batch
use GraphQL::Dataloader
end

before :all do
Expand Down

0 comments on commit 2c551e7

Please sign in to comment.