Skip to content
This repository was archived by the owner on Sep 24, 2019. It is now read-only.

Commit bbe5b26

Browse files
committed
Use graphql-active_record_batcher
1 parent 436044b commit bbe5b26

File tree

6 files changed

+28
-16
lines changed

6 files changed

+28
-16
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ gem 'puma', '~> 3.0'
1212
# Use GraphQL!
1313
gem 'graphql', github: 'rmosolgo/graphql-ruby', ref: 'a655714240c59e86c84bb22b82d776be02eddb8f'
1414
gem 'graphql-batch'
15+
gem 'graphql-active_record_batcher'
1516
# GraphiQL Interface
1617
gem 'graphiql-rails', '~> 1.4.1'
1718

Gemfile.lock

+9-5
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,15 @@ GEM
5858
activesupport (>= 4.1.0)
5959
graphiql-rails (1.4.1)
6060
rails
61+
graphql-active_record_batcher (0.3.1)
62+
activerecord
63+
graphql
64+
graphql-batch
65+
promise.rb
6166
graphql-batch (0.3.1)
6267
graphql (>= 0.8, < 2)
6368
promise.rb (~> 0.7.2)
6469
i18n (0.8.0)
65-
json (2.0.2)
6670
listen (3.0.8)
6771
rb-fsevent (~> 0.9, >= 0.9.4)
6872
rb-inotify (~> 0.9, >= 0.9.7)
@@ -129,9 +133,8 @@ GEM
129133
thread_safe (0.3.5)
130134
tzinfo (1.2.2)
131135
thread_safe (~> 0.1)
132-
uglifier (2.7.2)
133-
execjs (>= 0.3.0)
134-
json (>= 1.8.0)
136+
uglifier (3.0.4)
137+
execjs (>= 0.3.0, < 3)
135138
web-console (3.4.0)
136139
actionview (>= 5.0)
137140
activemodel (>= 5.0)
@@ -149,6 +152,7 @@ DEPENDENCIES
149152
byebug
150153
graphiql-rails (~> 1.4.1)
151154
graphql!
155+
graphql-active_record_batcher
152156
graphql-batch
153157
listen (~> 3.0.5)
154158
pg
@@ -162,4 +166,4 @@ DEPENDENCIES
162166
web-console (>= 3.3.0)
163167

164168
BUNDLED WITH
165-
1.14.3
169+
1.14.4

app/models/graph/schema.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Graph
22
Schema = GraphQL::Schema.define do
3+
enable_active_record_batching
4+
35
query Graph::Types::Query
46
mutation Graph::Mutations::Mutation
57

@@ -23,8 +25,5 @@ module Graph
2325
model = Object.const_get(gid.model_name)
2426
Graph::FindLoader.for(model).load(gid.model_id.to_i)
2527
end
26-
27-
lazy_resolve(Promise, :sync)
28-
instrument(:query, GraphQL::Batch::Setup)
2928
end
3029
end

app/models/graph/types/person.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@ module Types
3333
property: :hair_color
3434

3535
field :height, types.Int, "The height of the person in centimeters."
36+
3637
field :homeworld, Planet, "A planet that this person was born on or inhabits." do
37-
resolve -> (person, _, _) do
38-
Graph::AssociationLoader.for(::Person, :homeworld).load(person)
39-
end
38+
preloads :homeworld
39+
end
40+
41+
field :species, Species, "The species of this person." do
42+
preloads :species
4043
end
44+
4145
field :mass, types.Int, "The mass of the person in kilograms."
4246
field :name, !types.String, "The name of this person."
4347
field :skinColor, types.String, "The skin color of this person.", property: :skin_color

app/models/graph/types/rating.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ module Types
66

77
interfaces [GraphQL::Relay::Node.interface]
88

9-
field :user, !Graph::Types::User, "The critic."
10-
field :film, !Graph::Types::Film, "The rated film."
9+
field :user, !Graph::Types::User, "The critic." do
10+
preloads :user
11+
end
12+
13+
field :film, !Graph::Types::Film, "The rated film." do
14+
preloads :film
15+
end
16+
1117
field :rating, !types.Int, "A film rating from 0 to 5."
1218
field :createdAt, !types.String,
1319
"The ISO 8601 date format of the time that this resource was created.", property: :created_at

app/models/graph/types/species.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ module Types
5151

5252
field :language, types.String, "The language commonly spoken by this species."
5353
field :homeworld, Graph::Types::Planet, "A planet that this species originates from type." do
54-
resolve -> (species, _, _) do
55-
Graph::AssociationLoader.for(::Species, :homeworld).load(species)
56-
end
54+
preloads :homeworld
5755
end
5856
end
5957
end

0 commit comments

Comments
 (0)