batch-loader 1.4.1 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +12 -22
- data/CHANGELOG.md +29 -0
- data/README.md +53 -29
- data/graphql-1.7.gemfile +1 -1
- data/{graphql-1.8.gemfile → graphql-latest.gemfile} +1 -1
- data/lib/batch_loader/graphql.rb +23 -3
- data/lib/batch_loader/version.rb +1 -1
- metadata +4 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 061a141c0204a3e32aa437b0258e0051b1a0d725de948a762f2444d6994ebfb8
|
4
|
+
data.tar.gz: 9ca768f5ecbe6fe8b72edd4a9161d7aee635023ce111f26f2406a71633a2e2ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c09ec97852edb3ff184a5e5f4fb90e615cdf86b65920a7e9da56e5e4fd633e99ca5252d48a08b8802f860a960539fe5adafebf9b26b910ebe03423fa66b58b3f
|
7
|
+
data.tar.gz: 14769f25d9e61311700997c3008f6df75eaf7b5fbce5db121a36dacbf483246d145862f63c813d4273d30f626690a1941fb17abd1e31b8b91f90d477c67001a1
|
data/.travis.yml
CHANGED
@@ -1,23 +1,13 @@
|
|
1
|
-
sudo: false
|
2
1
|
language: ruby
|
3
|
-
before_install: gem install bundler -v
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
- gemfile: graphql-1.8.gemfile
|
16
|
-
env: GRAPHQL_RUBY_VERSION=1_8 CI=true
|
17
|
-
rvm: 2.4.5
|
18
|
-
- gemfile: graphql-1.7.gemfile
|
19
|
-
env: GRAPHQL_RUBY_VERSION=1_7 CI=true
|
20
|
-
rvm: 2.5.3
|
21
|
-
- gemfile: graphql-1.8.gemfile
|
22
|
-
env: GRAPHQL_RUBY_VERSION=1_8 CI=true
|
23
|
-
rvm: 2.5.3
|
2
|
+
before_install: gem install bundler -v 2.0.1
|
3
|
+
rvm:
|
4
|
+
- 2.3.8
|
5
|
+
- 2.4.9
|
6
|
+
- 2.5.7
|
7
|
+
- 2.6.5
|
8
|
+
- 2.7.0
|
9
|
+
env:
|
10
|
+
- CI=true
|
11
|
+
gemfile:
|
12
|
+
- graphql-1.7.gemfile
|
13
|
+
- graphql-latest.gemfile
|
data/CHANGELOG.md
CHANGED
@@ -12,6 +12,35 @@ that you can set version constraints properly.
|
|
12
12
|
|
13
13
|
* WIP
|
14
14
|
|
15
|
+
#### [v1.5.0](https://github.com/exAspArk/batch-loader/compare/v1.4.1...v1.5.0)
|
16
|
+
|
17
|
+
* `Added`: Support for GraphQL Interpreter. [#62](https://github.com/exAspArk/batch-loader/pull/62)
|
18
|
+
* `Deprecated`: `BatchLoader.for` in GraphQL. [#62](https://github.com/exAspArk/batch-loader/pull/62)
|
19
|
+
|
20
|
+
Please use `BatchLoader::GraphQL.for` instead:
|
21
|
+
|
22
|
+
```rb
|
23
|
+
field :user, UserType, null: false
|
24
|
+
|
25
|
+
def user # resolver
|
26
|
+
BatchLoader::GraphQL.for...
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
Or wrap a BatchLoader instance with `BatchLoader::GraphQL.wrap`:
|
31
|
+
|
32
|
+
```rb
|
33
|
+
field :user, UserType, null: false
|
34
|
+
|
35
|
+
def user # resolver
|
36
|
+
BatchLoader::GraphQL.wrap(lazy_user)
|
37
|
+
end
|
38
|
+
|
39
|
+
def lazy_user
|
40
|
+
BatchLoader.for...
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
15
44
|
#### [v1.4.1](https://github.com/exAspArk/batch-loader/compare/v1.4.0...v1.4.1)
|
16
45
|
|
17
46
|
* `Fixes`: Does not allow mutating and corrupting a list of items in a `batch` block. [#46](https://github.com/exAspArk/batch-loader/pull/46)
|
data/README.md
CHANGED
@@ -37,9 +37,9 @@ Developers from these companies use `BatchLoader`:
|
|
37
37
|
* [Replacing methods](#replacing-methods)
|
38
38
|
* [Installation](#installation)
|
39
39
|
* [API](#api)
|
40
|
+
* [Related tools](#related-tools)
|
40
41
|
* [Implementation details](#implementation-details)
|
41
42
|
* [Development](#development)
|
42
|
-
* [Related gems](#related-gems)
|
43
43
|
* [Contributing](#contributing)
|
44
44
|
* [Alternatives](#alternatives)
|
45
45
|
* [License](#license)
|
@@ -235,23 +235,38 @@ Batching is particularly useful with GraphQL. Using such techniques as preloadin
|
|
235
235
|
Let's take a look at the simple [graphql-ruby](https://github.com/rmosolgo/graphql-ruby) schema example:
|
236
236
|
|
237
237
|
```ruby
|
238
|
-
|
239
|
-
query QueryType
|
238
|
+
class MyProjectSchema < GraphQL::Schema
|
239
|
+
query Types::QueryType
|
240
240
|
end
|
241
241
|
|
242
|
-
|
243
|
-
|
244
|
-
|
242
|
+
module Types
|
243
|
+
class QueryType < Types::BaseObject
|
244
|
+
field :posts, [PostType], null: false
|
245
|
+
|
246
|
+
def posts
|
247
|
+
Post.all
|
248
|
+
end
|
249
|
+
end
|
245
250
|
end
|
246
251
|
|
247
|
-
|
248
|
-
|
249
|
-
|
252
|
+
module Types
|
253
|
+
class PostType < Types::BaseObject
|
254
|
+
name "Post"
|
255
|
+
|
256
|
+
field :user, UserType, null: false
|
257
|
+
|
258
|
+
def user
|
259
|
+
post.user # N+1 queries
|
260
|
+
end
|
261
|
+
end
|
250
262
|
end
|
251
263
|
|
252
|
-
|
253
|
-
|
254
|
-
|
264
|
+
module Types
|
265
|
+
class UserType < Types::BaseObject
|
266
|
+
name "User"
|
267
|
+
|
268
|
+
field :name, String, null: false
|
269
|
+
end
|
255
270
|
end
|
256
271
|
```
|
257
272
|
|
@@ -267,17 +282,22 @@ query = "
|
|
267
282
|
}
|
268
283
|
}
|
269
284
|
"
|
270
|
-
|
285
|
+
MyProjectSchema.execute(query)
|
271
286
|
```
|
272
287
|
|
273
288
|
To avoid this problem, all we have to do is to change the resolver to return `BatchLoader::GraphQL` ([#32](https://github.com/exAspArk/batch-loader/pull/32) explains why not just `BatchLoader`):
|
274
289
|
|
275
290
|
```ruby
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
291
|
+
module Types
|
292
|
+
class PostType < Types::BaseObject
|
293
|
+
name "Post"
|
294
|
+
|
295
|
+
field :user, UserType, null: false
|
296
|
+
|
297
|
+
def user
|
298
|
+
BatchLoader::GraphQL.for(post.user_id).batch do |user_ids, loader|
|
299
|
+
User.where(id: user_ids).each { |user| loader.call(user.id, user) }
|
300
|
+
end
|
281
301
|
end
|
282
302
|
end
|
283
303
|
end
|
@@ -286,8 +306,8 @@ end
|
|
286
306
|
And setup GraphQL to use the built-in `lazy_resolve` method:
|
287
307
|
|
288
308
|
```ruby
|
289
|
-
|
290
|
-
query QueryType
|
309
|
+
class MyProjectSchema < GraphQL::Schema
|
310
|
+
query Types::QueryType
|
291
311
|
use BatchLoader::GraphQL
|
292
312
|
end
|
293
313
|
```
|
@@ -304,7 +324,7 @@ BatchLoader.for(post.user_id).batch(default_value: NullUser.new) do |user_ids, l
|
|
304
324
|
end
|
305
325
|
```
|
306
326
|
|
307
|
-
For batches where the value is some kind of collection, such as an Array or Hash, `loader` also supports being called with a block, which yields the _current_ value, and returns the _next_ value. This is extremely useful for 1:Many relationships:
|
327
|
+
For batches where the value is some kind of collection, such as an Array or Hash, `loader` also supports being called with a block, which yields the _current_ value, and returns the _next_ value. This is extremely useful for 1:Many (`has_many`) relationships:
|
308
328
|
|
309
329
|
```ruby
|
310
330
|
BatchLoader.for(user.id).batch(default_value: []) do |user_ids, loader|
|
@@ -441,6 +461,18 @@ end
|
|
441
461
|
| `loader` | - | Lambda which should be called to load values loaded in batch. |
|
442
462
|
| `args` | `{default_value: nil, cache: true, replace_methods: true, key: nil}` | Arguments passed to the `batch` method. |
|
443
463
|
|
464
|
+
## Related tools
|
465
|
+
|
466
|
+
These gems are built by using `BatchLoader`:
|
467
|
+
|
468
|
+
* [decidim-core](https://github.com/decidim/decidim/) – participatory democracy framework made with Ruby on Rails.
|
469
|
+
* [ams_lazy_relationships](https://github.com/Bajena/ams_lazy_relationships/) – ActiveModel Serializers add-on for eliminating N+1 queries.
|
470
|
+
* [batch-loader-active-record](https://github.com/mathieul/batch-loader-active-record/) – ActiveRecord lazy association generator to avoid N+1 DB queries.
|
471
|
+
|
472
|
+
`BatchLoader` in other programming languages:
|
473
|
+
|
474
|
+
* [batch_loader](https://github.com/exaspark/batch_loader) - Elixir implementation.
|
475
|
+
|
444
476
|
## Implementation details
|
445
477
|
|
446
478
|
See the [slides](https://speakerdeck.com/exaspark/batching-a-powerful-way-to-solve-n-plus-1-queries) [37-42].
|
@@ -451,14 +483,6 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
|
451
483
|
|
452
484
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
453
485
|
|
454
|
-
## Related gems
|
455
|
-
|
456
|
-
These gems are built by using `BatchLoader`:
|
457
|
-
|
458
|
-
* [decidim-core](https://github.com/decidim/decidim/) – participatory democracy framework made with Ruby on Rails.
|
459
|
-
* [ams_lazy_relationships](https://github.com/Bajena/ams_lazy_relationships/) – ActiveModel Serializers add-on for eliminating N+1 queries.
|
460
|
-
* [batch-loader-active-record](https://github.com/mathieul/batch-loader-active-record/) – ActiveRecord lazy association generator to avoid N+1 DB queries.
|
461
|
-
|
462
486
|
## Contributing
|
463
487
|
|
464
488
|
Bug reports and pull requests are welcome on GitHub at https://github.com/exAspArk/batch-loader. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
data/graphql-1.7.gemfile
CHANGED
data/lib/batch_loader/graphql.rb
CHANGED
@@ -4,20 +4,40 @@ class BatchLoader
|
|
4
4
|
class GraphQL
|
5
5
|
def self.use(schema_definition)
|
6
6
|
schema_definition.lazy_resolve(BatchLoader::GraphQL, :sync)
|
7
|
-
|
8
|
-
|
7
|
+
|
8
|
+
# in cases when BatchLoader is being used instead of BatchLoader::GraphQL
|
9
|
+
if schema_definition.respond_to?(:interpreter?) && schema_definition.interpreter?
|
10
|
+
schema_definition.tracer(self)
|
11
|
+
else
|
12
|
+
schema_definition.instrument(:field, self)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.trace(event, _data)
|
17
|
+
if event == 'execute_field'
|
18
|
+
result = yield
|
19
|
+
result.respond_to?(:__sync) ? wrap_with_warning(result) : result
|
20
|
+
else
|
21
|
+
yield
|
22
|
+
end
|
9
23
|
end
|
10
24
|
|
11
25
|
def self.instrument(type, field)
|
12
26
|
old_resolve_proc = field.resolve_proc
|
13
27
|
new_resolve_proc = ->(object, arguments, context) do
|
14
28
|
result = old_resolve_proc.call(object, arguments, context)
|
15
|
-
result.respond_to?(:__sync) ?
|
29
|
+
result.respond_to?(:__sync) ? wrap_with_warning(result) : result
|
16
30
|
end
|
17
31
|
|
18
32
|
field.redefine { resolve(new_resolve_proc) }
|
19
33
|
end
|
20
34
|
|
35
|
+
def self.wrap_with_warning(batch_loader)
|
36
|
+
warn "DEPRECATION WARNING: using BatchLoader.for in GraphQL is deprecated. Use BatchLoader::GraphQL.for instead or return BatchLoader::GraphQL.wrap from your resolver."
|
37
|
+
wrap(batch_loader)
|
38
|
+
end
|
39
|
+
private_class_method :wrap_with_warning
|
40
|
+
|
21
41
|
def self.wrap(batch_loader)
|
22
42
|
BatchLoader::GraphQL.new.tap do |graphql|
|
23
43
|
graphql.batch_loader = batch_loader
|
data/lib/batch_loader/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: batch-loader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- exAspArk
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -128,7 +128,7 @@ files:
|
|
128
128
|
- bin/console
|
129
129
|
- bin/setup
|
130
130
|
- graphql-1.7.gemfile
|
131
|
-
- graphql-
|
131
|
+
- graphql-latest.gemfile
|
132
132
|
- lib/batch-loader.rb
|
133
133
|
- lib/batch_loader.rb
|
134
134
|
- lib/batch_loader/executor.rb
|
@@ -155,8 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
155
155
|
- !ruby/object:Gem::Version
|
156
156
|
version: '0'
|
157
157
|
requirements: []
|
158
|
-
|
159
|
-
rubygems_version: 2.6.11
|
158
|
+
rubygems_version: 3.0.3
|
160
159
|
signing_key:
|
161
160
|
specification_version: 4
|
162
161
|
summary: Powerful tool to avoid N+1 DB or HTTP queries
|