Skip to content

Commit 6dc6e44

Browse files
committed
Add batch-loader
1 parent 13459c9 commit 6dc6e44

File tree

10 files changed

+164
-63
lines changed

10 files changed

+164
-63
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ gem 'rolify'
3333
gem 'pry'
3434
gem 'graphql'
3535
gem 'graphiql-rails', group: :development
36-
gem 'graphql-batch'
36+
gem 'batch-loader'
3737

3838
group :development, :test do
3939
# Call 'byebug' anywhere in the code to stop execution and get a debugger console

Gemfile.lock

Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4-
actioncable (5.0.2)
5-
actionpack (= 5.0.2)
4+
actioncable (5.0.6)
5+
actionpack (= 5.0.6)
66
nio4r (>= 1.2, < 3.0)
77
websocket-driver (~> 0.6.1)
8-
actionmailer (5.0.2)
9-
actionpack (= 5.0.2)
10-
actionview (= 5.0.2)
11-
activejob (= 5.0.2)
8+
actionmailer (5.0.6)
9+
actionpack (= 5.0.6)
10+
actionview (= 5.0.6)
11+
activejob (= 5.0.6)
1212
mail (~> 2.5, >= 2.5.4)
1313
rails-dom-testing (~> 2.0)
14-
actionpack (5.0.2)
15-
actionview (= 5.0.2)
16-
activesupport (= 5.0.2)
14+
actionpack (5.0.6)
15+
actionview (= 5.0.6)
16+
activesupport (= 5.0.6)
1717
rack (~> 2.0)
1818
rack-test (~> 0.6.3)
1919
rails-dom-testing (~> 2.0)
2020
rails-html-sanitizer (~> 1.0, >= 1.0.2)
21-
actionview (5.0.2)
22-
activesupport (= 5.0.2)
21+
actionview (5.0.6)
22+
activesupport (= 5.0.6)
2323
builder (~> 3.1)
2424
erubis (~> 2.7.0)
2525
rails-dom-testing (~> 2.0)
2626
rails-html-sanitizer (~> 1.0, >= 1.0.3)
27-
activejob (5.0.2)
28-
activesupport (= 5.0.2)
27+
activejob (5.0.6)
28+
activesupport (= 5.0.6)
2929
globalid (>= 0.3.6)
30-
activemodel (5.0.2)
31-
activesupport (= 5.0.2)
32-
activerecord (5.0.2)
33-
activemodel (= 5.0.2)
34-
activesupport (= 5.0.2)
30+
activemodel (5.0.6)
31+
activesupport (= 5.0.6)
32+
activerecord (5.0.6)
33+
activemodel (= 5.0.6)
34+
activesupport (= 5.0.6)
3535
arel (~> 7.0)
36-
activesupport (5.0.2)
36+
activesupport (5.0.6)
3737
concurrent-ruby (~> 1.0, >= 1.0.2)
3838
i18n (~> 0.7)
3939
minitest (~> 5.1)
4040
tzinfo (~> 1.1)
4141
arel (7.1.4)
42+
batch-loader (1.0.1)
4243
bcrypt (3.1.11)
4344
builder (3.2.3)
4445
byebug (9.0.6)
@@ -63,15 +64,12 @@ GEM
6364
ffi (1.9.18)
6465
foreman (0.82.0)
6566
thor (~> 0.19.1)
66-
globalid (0.3.7)
67-
activesupport (>= 4.1.0)
68-
graphiql-rails (1.4.1)
67+
globalid (0.4.0)
68+
activesupport (>= 4.2.0)
69+
graphiql-rails (1.4.4)
6970
rails
70-
graphql (1.6.3)
71-
graphql-batch (0.3.3)
72-
graphql (>= 0.8, < 2)
73-
promise.rb (~> 0.7.2)
74-
i18n (0.8.1)
71+
graphql (1.6.8)
72+
i18n (0.8.6)
7573
jsonapi-resources (0.9.0)
7674
activerecord (>= 4.1)
7775
concurrent-ruby
@@ -81,53 +79,52 @@ GEM
8179
rb-inotify (~> 0.9, >= 0.9.7)
8280
loofah (2.0.3)
8381
nokogiri (>= 1.5.9)
84-
mail (2.6.4)
82+
mail (2.6.6)
8583
mime-types (>= 1.16, < 4)
8684
method_source (0.8.2)
8785
mime-types (3.1)
8886
mime-types-data (~> 3.2015)
8987
mime-types-data (3.2016.0521)
90-
mini_portile2 (2.1.0)
91-
minitest (5.10.1)
92-
nio4r (2.0.0)
93-
nokogiri (1.7.1)
94-
mini_portile2 (~> 2.1.0)
88+
mini_portile2 (2.2.0)
89+
minitest (5.10.3)
90+
nio4r (2.1.0)
91+
nokogiri (1.8.0)
92+
mini_portile2 (~> 2.2.0)
9593
orm_adapter (0.5.0)
9694
pg (0.20.0)
97-
promise.rb (0.7.3)
9895
pry (0.10.4)
9996
coderay (~> 1.1.0)
10097
method_source (~> 0.8.1)
10198
slop (~> 3.4)
10299
puma (3.8.2)
103-
rack (2.0.1)
100+
rack (2.0.3)
104101
rack-cors (0.4.1)
105102
rack-test (0.6.3)
106103
rack (>= 1.0)
107-
rails (5.0.2)
108-
actioncable (= 5.0.2)
109-
actionmailer (= 5.0.2)
110-
actionpack (= 5.0.2)
111-
actionview (= 5.0.2)
112-
activejob (= 5.0.2)
113-
activemodel (= 5.0.2)
114-
activerecord (= 5.0.2)
115-
activesupport (= 5.0.2)
116-
bundler (>= 1.3.0, < 2.0)
117-
railties (= 5.0.2)
104+
rails (5.0.6)
105+
actioncable (= 5.0.6)
106+
actionmailer (= 5.0.6)
107+
actionpack (= 5.0.6)
108+
actionview (= 5.0.6)
109+
activejob (= 5.0.6)
110+
activemodel (= 5.0.6)
111+
activerecord (= 5.0.6)
112+
activesupport (= 5.0.6)
113+
bundler (>= 1.3.0)
114+
railties (= 5.0.6)
118115
sprockets-rails (>= 2.0.0)
119-
rails-dom-testing (2.0.2)
120-
activesupport (>= 4.2.0, < 6.0)
121-
nokogiri (~> 1.6)
116+
rails-dom-testing (2.0.3)
117+
activesupport (>= 4.2.0)
118+
nokogiri (>= 1.6)
122119
rails-html-sanitizer (1.0.3)
123120
loofah (~> 2.0)
124-
railties (5.0.2)
125-
actionpack (= 5.0.2)
126-
activesupport (= 5.0.2)
121+
railties (5.0.6)
122+
actionpack (= 5.0.6)
123+
activesupport (= 5.0.6)
127124
method_source
128125
rake (>= 0.8.7)
129126
thor (>= 0.18.1, < 2.0)
130-
rake (12.0.0)
127+
rake (12.1.0)
131128
rb-fsevent (0.9.8)
132129
rb-inotify (0.9.8)
133130
ffi (>= 0.5.0)
@@ -160,7 +157,7 @@ GEM
160157
sprockets (3.7.1)
161158
concurrent-ruby (~> 1.0)
162159
rack (> 1, < 3)
163-
sprockets-rails (3.2.0)
160+
sprockets-rails (3.2.1)
164161
actionpack (>= 4.0)
165162
activesupport (>= 4.0)
166163
sprockets (>= 3.0.0)
@@ -178,6 +175,7 @@ PLATFORMS
178175
ruby
179176

180177
DEPENDENCIES
178+
batch-loader
181179
byebug
182180
cancan
183181
devise_token_auth
@@ -186,7 +184,6 @@ DEPENDENCIES
186184
foreman
187185
graphiql-rails
188186
graphql
189-
graphql-batch
190187
jsonapi-resources
191188
listen (~> 3.0.5)
192189
pg (~> 0.18)

README.md

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,72 @@ Demo user: user1@example.com / Secret123
3030
Add list, edit and form components in `client/src/components/` based on one of existing.
3131

3232
### [graphiql](http://localhost:3001/graphiql)
33-
33+
34+
Categories
35+
36+
{
37+
categories {
38+
id
39+
name
40+
}
41+
}
42+
43+
Categories with posts
44+
45+
{
46+
categories(with_posts: true) {
47+
id
48+
name
49+
posts {
50+
id
51+
title
52+
category {
53+
id
54+
name
55+
}
56+
comments {
57+
id
58+
body
59+
}
60+
}
61+
}
62+
}
63+
64+
One category
65+
3466
{
35-
categories {
67+
category(id: 1) {
68+
id
3669
name
3770
}
3871
}
72+
73+
Posts
74+
75+
{
76+
posts {
77+
id
78+
title
79+
category {
80+
id
81+
name
82+
}
83+
}
84+
}
85+
86+
One post
87+
88+
{
89+
post(id: 1) {
90+
id
91+
title
92+
category {
93+
id
94+
name
95+
}
96+
comments {
97+
id
98+
body
99+
}
100+
}
101+
}

app/graphql/loaders/.keep

Whitespace-only changes.
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
RailsJsonApiServerSchema = GraphQL::Schema.define do
22
query(Types::QueryType)
3-
4-
# GraphQL::Batch setup:
5-
use GraphQL::Batch
3+
use BatchLoader::GraphQL
64
end

app/graphql/types/category_type.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
field :id, types.Int
44
field :name, types.String
55
field :created_at, types.String
6-
end
6+
field :posts, types[Types::PostType]
7+
end

app/graphql/types/comment_type.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Types::CommentType = GraphQL::ObjectType.define do
2+
name "Comment"
3+
field :id, types.Int
4+
field :body, types.String
5+
field :created_at, types.String
6+
field :post, Types::PostType
7+
end

app/graphql/types/post_type.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Types::PostType = GraphQL::ObjectType.define do
2+
name "Post"
3+
field :id, types.Int
4+
field :title, types.String
5+
field :created_at, types.String
6+
field :category do
7+
type Types::CategoryType
8+
resolve ->(obj, args, ctx) {
9+
BatchLoader.for(obj.category_id).batch do |ids, loader|
10+
Category.where(id: ids).each { |record| loader.call(record.id, record) }
11+
end
12+
}
13+
end
14+
field :comments, types[Types::CommentType]
15+
end

app/graphql/types/query_type.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,27 @@
1212
field :categories do
1313
type types[Types::CategoryType]
1414
argument :id, types[types.ID]
15+
argument :with_posts, types.Boolean
1516
description "Find Categories"
1617
resolve ->(obj, args, ctx) {
17-
args["ids"] ? Category.where(id: args["ids"]) : Category.all
18+
categories = (args["ids"] ? Category.where(id: args["ids"]) : Category.all)
19+
args["with_posts"] ? categories.includes(posts: :comments) : categories
1820
}
1921
end
22+
23+
field :posts do
24+
type types[Types::PostType]
25+
argument :id, types[types.ID]
26+
description "Find Posts"
27+
resolve ->(obj, args, ctx) {
28+
args["ids"] ? Post.where(id: args["ids"]) : Post.all
29+
}
30+
end
31+
32+
field :post do
33+
type Types::PostType
34+
argument :id, !types.ID
35+
description "Find a Post by ID"
36+
resolve ->(obj, args, ctx) { Post.find(args["id"]) }
37+
end
2038
end

config/application.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,7 @@ class Application < Rails::Application
3838
# This is necessary if your schema can't be completely dumped by the schema dumper,
3939
# like if you have constraints or database-specific column types
4040
config.active_record.schema_format = :sql
41+
42+
config.autoload_paths << Rails.root.join("app", "types")
4143
end
4244
end

0 commit comments

Comments
 (0)