batch-loader 2.0.1 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/tests.yml +27 -0
- data/CHANGELOG.md +9 -1
- data/README.md +35 -4
- data/graphql-latest.gemfile +1 -1
- data/lib/batch_loader/graphql.rb +10 -1
- data/lib/batch_loader/version.rb +1 -1
- data/lib/batch_loader.rb +4 -4
- metadata +4 -4
- data/.travis.yml +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 275e001ad653f18baa10d3869939654094e5e57e4139b859e324def6c555f0fd
|
4
|
+
data.tar.gz: 19e4077a85ffc3fc53bdc1faff69be79f377d49f81fc8bf3724517ff28347008
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 742f9412aa389b2507122ceec3a19fb5e846ea6bffb6a9a87269946960d250b70fcb5c0cfaa2660ff73df8902f3abf38f16900f95e2781465dd8b0e199bce9fd
|
7
|
+
data.tar.gz: 6a4ba810b7f326215920207811456ceb3aa506c17166566e7faa89a6611405d1094f9ce2b78caa56ccd125c6fedfb49fd448cbdaa08cce3f3e881930e946b4d9
|
@@ -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/CHANGELOG.md
CHANGED
@@ -8,10 +8,18 @@ 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/v2.0.
|
11
|
+
#### [Unreleased](https://github.com/exAspArk/batch-loader/compare/v2.0.3...HEAD)
|
12
12
|
|
13
13
|
* WIP
|
14
14
|
|
15
|
+
#### [v2.0.3](https://github.com/exAspArk/batch-loader/compare/v2.0.2...v2.0.3) - 2024-03-25
|
16
|
+
|
17
|
+
* `Fixed`: `Schema.tracer(...)` GraphQL deprecation warning by making it compatible with `.trace_with(...)`. [#91](https://github.com/exAspArk/batch-loader/pull/91)
|
18
|
+
|
19
|
+
#### [v2.0.2](https://github.com/exAspArk/batch-loader/compare/v2.0.1...v2.0.2) - 2023-11-22
|
20
|
+
|
21
|
+
* `Fixed`: Compatibility with Ruby 3 kwargs. [#80](https://github.com/exAspArk/batch-loader/pull/80)
|
22
|
+
|
15
23
|
#### [v2.0.1](https://github.com/exAspArk/batch-loader/compare/v2.0.0...v2.0.1) - 2021-02-18
|
16
24
|
|
17
25
|
* `Fixed`: Compatibility with GraphQL and Ruby 3. [#71](https://github.com/exAspArk/batch-loader/pull/71)
|
data/README.md
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
# BatchLoader
|
2
2
|
|
3
|
-
[![Build Status](https://travis-ci.org/exAspArk/batch-loader.svg?branch=master)](https://travis-ci.org/exAspArk/batch-loader)
|
4
3
|
[![Coverage Status](https://coveralls.io/repos/github/exAspArk/batch-loader/badge.svg)](https://coveralls.io/github/exAspArk/batch-loader)
|
5
4
|
[![Code Climate](https://img.shields.io/codeclimate/maintainability/exAspArk/batch-loader.svg)](https://codeclimate.com/github/exAspArk/batch-loader/maintainability)
|
6
5
|
[![Downloads](https://img.shields.io/gem/dt/batch-loader.svg)](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/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 = yield
|
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)
|
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: 2.0.
|
4
|
+
version: 2.0.3
|
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: 2024-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -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
|
@@ -154,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
154
154
|
- !ruby/object:Gem::Version
|
155
155
|
version: '0'
|
156
156
|
requirements: []
|
157
|
-
rubygems_version: 3.
|
157
|
+
rubygems_version: 3.4.19
|
158
158
|
signing_key:
|
159
159
|
specification_version: 4
|
160
160
|
summary: Powerful tool to avoid N+1 DB or HTTP queries
|