batch-loader 1.5.0 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/tests.yml +27 -0
- data/.gitignore +1 -0
- data/CHANGELOG.md +36 -11
- data/README.md +35 -4
- data/batch-loader.gemspec +3 -3
- data/graphql-latest.gemfile +1 -1
- data/lib/batch_loader/graphql.rb +12 -4
- data/lib/batch_loader/version.rb +1 -1
- data/lib/batch_loader.rb +4 -4
- metadata +14 -15
- data/.travis.yml +0 -13
- data/graphql-1.7.gemfile +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c7ca2d2346f7f08d9830c846f03e7d901f4ce04c6b46f352870497c041531c44
|
4
|
+
data.tar.gz: 86d2deca55bfc7dbaf8166d24b60dd989d535dc63bb371e7567e90828912af75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 132a2a41228ea87f9bbf5bd6fc713518642569415b3ceacdeacb140dc7e4818f13972d5b3b9ef3840ea05475ecaba9e0b0c9b09e0515c42f61abe1d577792b6a
|
7
|
+
data.tar.gz: e560361bca6ee8b57d4a8a56085ba6b626155bbc995b063adb20e947a261935ae38b3fa9415f10981937575abfafef3b5f7dc594c1771263fee51c105f5bb6bf
|
@@ -0,0 +1,27 @@
|
|
1
|
+
name: Run tests
|
2
|
+
on:
|
3
|
+
- push
|
4
|
+
jobs:
|
5
|
+
run-tests:
|
6
|
+
strategy:
|
7
|
+
fail-fast: false
|
8
|
+
matrix:
|
9
|
+
version:
|
10
|
+
- 3.2.2
|
11
|
+
- 3.1.4
|
12
|
+
- 3.0.6
|
13
|
+
- 2.7.8
|
14
|
+
runs-on: ubuntu-latest
|
15
|
+
env:
|
16
|
+
CI: true
|
17
|
+
BUNDLE_GEMFILE: ${{ github.workspace }}/graphql-latest.gemfile
|
18
|
+
steps:
|
19
|
+
- name: Checkout a commit
|
20
|
+
uses: actions/checkout@v4
|
21
|
+
- name: Set up Ruby
|
22
|
+
uses: ruby/setup-ruby@v1
|
23
|
+
with:
|
24
|
+
ruby-version: ${{ matrix.version }}
|
25
|
+
bundler-cache: true
|
26
|
+
- name: Run tests
|
27
|
+
run: bundle exec rspec
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -8,11 +8,36 @@ one of the following labels: `Added`, `Changed`, `Deprecated`,
|
|
8
8
|
to manage the versions of this gem so
|
9
9
|
that you can set version constraints properly.
|
10
10
|
|
11
|
-
#### [Unreleased](https://github.com/exAspArk/batch-loader/compare/
|
11
|
+
#### [Unreleased](https://github.com/exAspArk/batch-loader/compare/v2.0.5...HEAD)
|
12
12
|
|
13
13
|
* WIP
|
14
14
|
|
15
|
-
#### [
|
15
|
+
#### [v2.0.5](https://github.com/exAspArk/batch-loader/compare/v2.0.4...v2.0.5) - 2024-04-24
|
16
|
+
|
17
|
+
* `Fixed`: Warning about `BatchLoader` usage instead `BatchLoader::GraphQL` in GraphQL. [#94](https://github.com/exAspArk/batch-loader/pull/94)
|
18
|
+
|
19
|
+
#### [v2.0.4](https://github.com/exAspArk/batch-loader/compare/v2.0.3...v2.0.4) - 2024-04-02
|
20
|
+
|
21
|
+
* `Fixed`: Chaining `#execute_field` trace calls. [#93](https://github.com/exAspArk/batch-loader/pull/93)
|
22
|
+
|
23
|
+
#### [v2.0.3](https://github.com/exAspArk/batch-loader/compare/v2.0.2...v2.0.3) - 2024-03-25
|
24
|
+
|
25
|
+
* `Fixed`: `Schema.tracer(...)` GraphQL deprecation warning by making it compatible with `.trace_with(...)`. [#91](https://github.com/exAspArk/batch-loader/pull/91)
|
26
|
+
|
27
|
+
#### [v2.0.2](https://github.com/exAspArk/batch-loader/compare/v2.0.1...v2.0.2) - 2023-11-22
|
28
|
+
|
29
|
+
* `Fixed`: Compatibility with Ruby 3 kwargs. [#80](https://github.com/exAspArk/batch-loader/pull/80)
|
30
|
+
|
31
|
+
#### [v2.0.1](https://github.com/exAspArk/batch-loader/compare/v2.0.0...v2.0.1) - 2021-02-18
|
32
|
+
|
33
|
+
* `Fixed`: Compatibility with GraphQL and Ruby 3. [#71](https://github.com/exAspArk/batch-loader/pull/71)
|
34
|
+
* `Changed`: GraphQL tests. [03d5153](https://github.com/exAspArk/batch-loader/commit/03d51537bc4033fd3aab42db3eee3e9e1cb365fa)
|
35
|
+
|
36
|
+
#### [v2.0.0](https://github.com/exAspArk/batch-loader/compare/v1.5.0...v2.0.0) - 2021-02-18
|
37
|
+
|
38
|
+
* `Removed`: Support for GraphQL version <= 1.7. [#75](https://github.com/exAspArk/batch-loader/pull/75)
|
39
|
+
|
40
|
+
#### [v1.5.0](https://github.com/exAspArk/batch-loader/compare/v1.4.1...v1.5.0) - 2020-04-20
|
16
41
|
|
17
42
|
* `Added`: Support for GraphQL Interpreter. [#62](https://github.com/exAspArk/batch-loader/pull/62)
|
18
43
|
* `Deprecated`: `BatchLoader.for` in GraphQL. [#62](https://github.com/exAspArk/batch-loader/pull/62)
|
@@ -41,40 +66,40 @@ def lazy_user
|
|
41
66
|
end
|
42
67
|
```
|
43
68
|
|
44
|
-
#### [v1.4.1](https://github.com/exAspArk/batch-loader/compare/v1.4.0...v1.4.1)
|
69
|
+
#### [v1.4.1](https://github.com/exAspArk/batch-loader/compare/v1.4.0...v1.4.1) - 2019-05-24
|
45
70
|
|
46
71
|
* `Fixes`: Does not allow mutating and corrupting a list of items in a `batch` block. [#46](https://github.com/exAspArk/batch-loader/pull/46)
|
47
72
|
|
48
|
-
#### [v1.4.0](https://github.com/exAspArk/batch-loader/compare/v1.3.0...v1.4.0)
|
73
|
+
#### [v1.4.0](https://github.com/exAspArk/batch-loader/compare/v1.3.0...v1.4.0) - 2019-04-29
|
49
74
|
|
50
75
|
* `Added`: new `replace_methods` argument to `BatchLoader#batch` to allow control over `define_method` calls. [#45](https://github.com/exAspArk/batch-loader/pull/45)
|
51
76
|
|
52
|
-
#### [v1.3.0](https://github.com/exAspArk/batch-loader/compare/v1.2.2...v1.3.0)
|
77
|
+
#### [v1.3.0](https://github.com/exAspArk/batch-loader/compare/v1.2.2...v1.3.0) - 2019-02-01
|
53
78
|
|
54
79
|
* `Added`: `BatchLoader::GraphQL.for` to make it compatible with `graphql` gem versions `>= 1.8.7`. [#30](https://github.com/exAspArk/batch-loader/issues/30)
|
55
80
|
|
56
|
-
#### [v1.2.2](https://github.com/exAspArk/batch-loader/compare/v1.2.1...v1.2.2)
|
81
|
+
#### [v1.2.2](https://github.com/exAspArk/batch-loader/compare/v1.2.1...v1.2.2) - 2018-12-03
|
57
82
|
|
58
83
|
* `Fixed`: Identify item by `key` object instead of `key` string representation. [#27](https://github.com/exAspArk/batch-loader/pull/27)
|
59
84
|
|
60
|
-
#### [v1.2.1](https://github.com/exAspArk/batch-loader/compare/v1.2.0...v1.2.1)
|
85
|
+
#### [v1.2.1](https://github.com/exAspArk/batch-loader/compare/v1.2.0...v1.2.1) - 2017-12-16
|
61
86
|
|
62
87
|
* `Fixed`: Do not depend on `method_missing` for `respond_to?`. [#14](https://github.com/exAspArk/batch-loader/pull/14)
|
63
88
|
|
64
|
-
#### [v1.2.0](https://github.com/exAspArk/batch-loader/compare/v1.1.1...v1.2.0)
|
89
|
+
#### [v1.2.0](https://github.com/exAspArk/batch-loader/compare/v1.1.1...v1.2.0) - 2017-11-16
|
65
90
|
|
66
91
|
* `Added`: `key` argument for the `BatchLoader#batch` method. [#12](https://github.com/exAspArk/batch-loader/pull/12)
|
67
92
|
|
68
|
-
#### [v1.1.1](https://github.com/exAspArk/batch-loader/compare/v1.1.0...v1.1.1)
|
93
|
+
#### [v1.1.1](https://github.com/exAspArk/batch-loader/compare/v1.1.0...v1.1.1) - 2017-11-06
|
69
94
|
|
70
95
|
* `Fixed`: `loader`, made it thread-safe again. [#10](https://github.com/exAspArk/batch-loader/pull/10)
|
71
96
|
|
72
|
-
#### [v1.1.0](https://github.com/exAspArk/batch-loader/compare/v1.0.4...v1.1.0)
|
97
|
+
#### [v1.1.0](https://github.com/exAspArk/batch-loader/compare/v1.0.4...v1.1.0) - 2017-11-02
|
73
98
|
|
74
99
|
* `Added`: `default_value` override option. [#8](https://github.com/exAspArk/batch-loader/pull/8)
|
75
100
|
* `Added`: `loader.call {}` block syntax, for memoizing repeat calls to the same item. [#8](https://github.com/exAspArk/batch-loader/pull/8)
|
76
101
|
|
77
|
-
#### [v1.0.4](https://github.com/exAspArk/batch-loader/compare/v1.0.3...v1.0.4)
|
102
|
+
#### [v1.0.4](https://github.com/exAspArk/batch-loader/compare/v1.0.3...v1.0.4) - 2017-10-12
|
78
103
|
|
79
104
|
* `Fixed`: Fix arity bug in `respond_to?` [#3](https://github.com/exAspArk/batch-loader/pull/3)
|
80
105
|
|
data/README.md
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
# BatchLoader
|
2
2
|
|
3
|
-
[](https://travis-ci.org/exAspArk/batch-loader)
|
4
3
|
[](https://coveralls.io/github/exAspArk/batch-loader)
|
5
4
|
[](https://codeclimate.com/github/exAspArk/batch-loader/maintainability)
|
6
5
|
[](https://rubygems.org/gems/batch-loader)
|
@@ -256,7 +255,7 @@ module Types
|
|
256
255
|
field :user, UserType, null: false
|
257
256
|
|
258
257
|
def user
|
259
|
-
|
258
|
+
object.user # N+1 queries
|
260
259
|
end
|
261
260
|
end
|
262
261
|
end
|
@@ -295,7 +294,7 @@ module Types
|
|
295
294
|
field :user, UserType, null: false
|
296
295
|
|
297
296
|
def user
|
298
|
-
BatchLoader::GraphQL.for(
|
297
|
+
BatchLoader::GraphQL.for(object.user_id).batch do |user_ids, loader|
|
299
298
|
User.where(id: user_ids).each { |user| loader.call(user.id, user) }
|
300
299
|
end
|
301
300
|
end
|
@@ -312,7 +311,39 @@ class MyProjectSchema < GraphQL::Schema
|
|
312
311
|
end
|
313
312
|
```
|
314
313
|
|
315
|
-
|
314
|
+
---
|
315
|
+
|
316
|
+
If you need to use BatchLoader with ActiveRecord in multiple places, you can use this `preload:` helper shared by [Aha!](https://www.aha.io/engineering/articles/automatically-avoiding-graphql-n-1s):
|
317
|
+
|
318
|
+
```rb
|
319
|
+
field :user, UserType, null: false, preload: :user
|
320
|
+
# ^^^^^^^^^^^^^^
|
321
|
+
# Simply add this instead of defining custom `user` method with BatchLoader
|
322
|
+
```
|
323
|
+
|
324
|
+
And add this custom field resolver that uses ActiveRecord's preload functionality with BatchLoader:
|
325
|
+
|
326
|
+
```rb
|
327
|
+
# app/graphql/types/base_object.rb
|
328
|
+
field_class Types::PreloadableField
|
329
|
+
|
330
|
+
# app/graphql/types/preloadable_field.rb
|
331
|
+
class Types::PreloadableField < Types::BaseField
|
332
|
+
def initialize(*args, preload: nil, **kwargs, &block)
|
333
|
+
@preloads = preload
|
334
|
+
super(*args, **kwargs, &block)
|
335
|
+
end
|
336
|
+
|
337
|
+
def resolve(type, args, ctx)
|
338
|
+
return super unless @preloads
|
339
|
+
|
340
|
+
BatchLoader::GraphQL.for(type).batch(key: self) do |records, loader|
|
341
|
+
ActiveRecord::Associations::Preloader.new(records: records.map(&:object), associations: @preloads).call
|
342
|
+
records.each { |r| loader.call(r, super(r, args, ctx)) }
|
343
|
+
end
|
344
|
+
end
|
345
|
+
end
|
346
|
+
```
|
316
347
|
|
317
348
|
### Loading multiple items
|
318
349
|
|
data/batch-loader.gemspec
CHANGED
@@ -22,10 +22,10 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.required_ruby_version = '>= 2.1.0' # keyword args
|
23
23
|
|
24
24
|
spec.add_development_dependency "bundler", "~> 2.0"
|
25
|
-
spec.add_development_dependency "rake", "~>
|
25
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
26
26
|
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
-
spec.add_development_dependency "graphql", "~> 1.
|
28
|
-
spec.add_development_dependency "pry
|
27
|
+
spec.add_development_dependency "graphql", "~> 1.8"
|
28
|
+
spec.add_development_dependency "pry", "~> 0.14"
|
29
29
|
spec.add_development_dependency "benchmark-ips", "~> 2.7"
|
30
30
|
spec.add_development_dependency "ruby-prof", "~> 0.16"
|
31
31
|
end
|
data/graphql-latest.gemfile
CHANGED
data/lib/batch_loader/graphql.rb
CHANGED
@@ -2,11 +2,20 @@
|
|
2
2
|
|
3
3
|
class BatchLoader
|
4
4
|
class GraphQL
|
5
|
+
module Trace
|
6
|
+
def execute_field(**_data)
|
7
|
+
result = super
|
8
|
+
result.respond_to?(:__sync) ? BatchLoader::GraphQL.wrap_with_warning(result) : result
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
5
12
|
def self.use(schema_definition)
|
6
13
|
schema_definition.lazy_resolve(BatchLoader::GraphQL, :sync)
|
7
14
|
|
8
15
|
# in cases when BatchLoader is being used instead of BatchLoader::GraphQL
|
9
|
-
if schema_definition.respond_to?(:
|
16
|
+
if schema_definition.respond_to?(:trace_with)
|
17
|
+
schema_definition.trace_with(Trace)
|
18
|
+
elsif schema_definition.respond_to?(:interpreter?) && schema_definition.interpreter?
|
10
19
|
schema_definition.tracer(self)
|
11
20
|
else
|
12
21
|
schema_definition.instrument(:field, self)
|
@@ -36,7 +45,6 @@ class BatchLoader
|
|
36
45
|
warn "DEPRECATION WARNING: using BatchLoader.for in GraphQL is deprecated. Use BatchLoader::GraphQL.for instead or return BatchLoader::GraphQL.wrap from your resolver."
|
37
46
|
wrap(batch_loader)
|
38
47
|
end
|
39
|
-
private_class_method :wrap_with_warning
|
40
48
|
|
41
49
|
def self.wrap(batch_loader)
|
42
50
|
BatchLoader::GraphQL.new.tap do |graphql|
|
@@ -54,8 +62,8 @@ class BatchLoader
|
|
54
62
|
@batch_loader = BatchLoader.for(item)
|
55
63
|
end
|
56
64
|
|
57
|
-
def batch(
|
58
|
-
@batch_loader.batch(
|
65
|
+
def batch(**kwargs, &block)
|
66
|
+
@batch_loader.batch(**kwargs, &block)
|
59
67
|
self
|
60
68
|
end
|
61
69
|
|
data/lib/batch_loader/version.rb
CHANGED
data/lib/batch_loader.rb
CHANGED
@@ -69,8 +69,8 @@ class BatchLoader
|
|
69
69
|
@cache ? @loaded_value : result
|
70
70
|
end
|
71
71
|
|
72
|
-
def method_missing(method_name, *args, &block)
|
73
|
-
__sync!.public_send(method_name, *args, &block)
|
72
|
+
def method_missing(method_name, *args, **kwargs, &block)
|
73
|
+
__sync!.public_send(method_name, *args, **kwargs, &block)
|
74
74
|
end
|
75
75
|
|
76
76
|
def __sync!
|
@@ -120,8 +120,8 @@ class BatchLoader
|
|
120
120
|
def __replace_with!(value)
|
121
121
|
__singleton_class.class_eval do
|
122
122
|
(value.methods - LEFT_INSTANCE_METHODS).each do |method_name|
|
123
|
-
define_method(method_name) do |*args, &block|
|
124
|
-
value.public_send(method_name, *args, &block)
|
123
|
+
define_method(method_name) do |*args, **kwargs, &block|
|
124
|
+
value.public_send(method_name, *args, **kwargs, &block)
|
125
125
|
end
|
126
126
|
end
|
127
127
|
end
|
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:
|
4
|
+
version: 2.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- exAspArk
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-04-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '13.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '13.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,28 +58,28 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '1.
|
61
|
+
version: '1.8'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '1.
|
68
|
+
version: '1.8'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name: pry
|
70
|
+
name: pry
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
75
|
+
version: '0.14'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '0.14'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: benchmark-ips
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -115,9 +115,9 @@ executables: []
|
|
115
115
|
extensions: []
|
116
116
|
extra_rdoc_files: []
|
117
117
|
files:
|
118
|
+
- ".github/workflows/tests.yml"
|
118
119
|
- ".gitignore"
|
119
120
|
- ".rspec"
|
120
|
-
- ".travis.yml"
|
121
121
|
- CHANGELOG.md
|
122
122
|
- CODE_OF_CONDUCT.md
|
123
123
|
- Gemfile
|
@@ -127,7 +127,6 @@ files:
|
|
127
127
|
- batch-loader.gemspec
|
128
128
|
- bin/console
|
129
129
|
- bin/setup
|
130
|
-
- graphql-1.7.gemfile
|
131
130
|
- graphql-latest.gemfile
|
132
131
|
- lib/batch-loader.rb
|
133
132
|
- lib/batch_loader.rb
|
@@ -140,7 +139,7 @@ homepage: https://github.com/exAspArk/batch-loader
|
|
140
139
|
licenses:
|
141
140
|
- MIT
|
142
141
|
metadata: {}
|
143
|
-
post_install_message:
|
142
|
+
post_install_message:
|
144
143
|
rdoc_options: []
|
145
144
|
require_paths:
|
146
145
|
- lib
|
@@ -155,8 +154,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
155
154
|
- !ruby/object:Gem::Version
|
156
155
|
version: '0'
|
157
156
|
requirements: []
|
158
|
-
rubygems_version: 3.
|
159
|
-
signing_key:
|
157
|
+
rubygems_version: 3.4.19
|
158
|
+
signing_key:
|
160
159
|
specification_version: 4
|
161
160
|
summary: Powerful tool to avoid N+1 DB or HTTP queries
|
162
161
|
test_files: []
|
data/.travis.yml
DELETED