batch-loader 2.0.0 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c1f4ff945285ef2833b1e70fd4c840b5a5dac71184941822cc5e4ed0b699e403
4
- data.tar.gz: fa257f5d8e8a43e39300ad916112affb26365b7d9bf98d09a612a8652e512ffc
3
+ metadata.gz: b25f607d37b2fec3d406a5f3451647a9c66e1f6ec061467c3b293ade83d1df06
4
+ data.tar.gz: ec1ea4291bf48e05ff606ce6de70908b70275c36a39016c361cc7303a33ebf79
5
5
  SHA512:
6
- metadata.gz: 998465ddf29713fa411892ce9d88c2195d992444ef2f2e6e22a61fe9c1af036534e580791e7313517e084efb79e46826e9fe6ecf37442b703dc4adc8715954b9
7
- data.tar.gz: 9834b2600bc1673d57926a245f463b5f8dd4bf637a59a5c6d384249fe9a6c4d2006adc500884f0593d31db2e81c48cc2bde151605a4e30e79a49530f0114f4bf
6
+ metadata.gz: 31e92589ff20dc24ca93a4d2e3dece6b0dabd42114988e0ddcdea6f49c58f639656ab66219923bdc26e5673c5878e4b95b347d6844f4ec50d5fbf799b8d1e9e1
7
+ data.tar.gz: e88f2da06cbd50ba0a92c1327a0d4c4193e7656cc48dac405429c34a4f2e66bce67bbd0c1dffab95e53c04ad741eb33f4a35799fe9dc9cfd214f09fdf4d6f95d
@@ -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,16 +8,24 @@ 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.0...HEAD)
11
+ #### [Unreleased](https://github.com/exAspArk/batch-loader/compare/v2.0.2...HEAD)
12
12
 
13
13
  * WIP
14
14
 
15
- #### [v2.0.0](https://github.com/exAspArk/batch-loader/compare/v1.5.0...v2.0.0)
15
+ #### [v2.0.2](https://github.com/exAspArk/batch-loader/compare/v2.0.1...v2.0.2) - 2023-11-22
16
+
17
+ * `Fixed`: Compatibility with Ruby 3 kwargs. [#80](https://github.com/exAspArk/batch-loader/pull/80)
18
+
19
+ #### [v2.0.1](https://github.com/exAspArk/batch-loader/compare/v2.0.0...v2.0.1) - 2021-02-18
20
+
21
+ * `Fixed`: Compatibility with GraphQL and Ruby 3. [#71](https://github.com/exAspArk/batch-loader/pull/71)
22
+ * `Changed`: GraphQL tests. [03d5153](https://github.com/exAspArk/batch-loader/commit/03d51537bc4033fd3aab42db3eee3e9e1cb365fa)
23
+
24
+ #### [v2.0.0](https://github.com/exAspArk/batch-loader/compare/v1.5.0...v2.0.0) - 2021-02-18
16
25
 
17
26
  * `Removed`: Support for GraphQL version <= 1.7. [#75](https://github.com/exAspArk/batch-loader/pull/75)
18
- * `Fixed`: Compatibility with Ruby 3. [#71](https://github.com/exAspArk/batch-loader/pull/71)
19
27
 
20
- #### [v1.5.0](https://github.com/exAspArk/batch-loader/compare/v1.4.1...v1.5.0)
28
+ #### [v1.5.0](https://github.com/exAspArk/batch-loader/compare/v1.4.1...v1.5.0) - 2020-04-20
21
29
 
22
30
  * `Added`: Support for GraphQL Interpreter. [#62](https://github.com/exAspArk/batch-loader/pull/62)
23
31
  * `Deprecated`: `BatchLoader.for` in GraphQL. [#62](https://github.com/exAspArk/batch-loader/pull/62)
@@ -46,40 +54,40 @@ def lazy_user
46
54
  end
47
55
  ```
48
56
 
49
- #### [v1.4.1](https://github.com/exAspArk/batch-loader/compare/v1.4.0...v1.4.1)
57
+ #### [v1.4.1](https://github.com/exAspArk/batch-loader/compare/v1.4.0...v1.4.1) - 2019-05-24
50
58
 
51
59
  * `Fixes`: Does not allow mutating and corrupting a list of items in a `batch` block. [#46](https://github.com/exAspArk/batch-loader/pull/46)
52
60
 
53
- #### [v1.4.0](https://github.com/exAspArk/batch-loader/compare/v1.3.0...v1.4.0)
61
+ #### [v1.4.0](https://github.com/exAspArk/batch-loader/compare/v1.3.0...v1.4.0) - 2019-04-29
54
62
 
55
63
  * `Added`: new `replace_methods` argument to `BatchLoader#batch` to allow control over `define_method` calls. [#45](https://github.com/exAspArk/batch-loader/pull/45)
56
64
 
57
- #### [v1.3.0](https://github.com/exAspArk/batch-loader/compare/v1.2.2...v1.3.0)
65
+ #### [v1.3.0](https://github.com/exAspArk/batch-loader/compare/v1.2.2...v1.3.0) - 2019-02-01
58
66
 
59
67
  * `Added`: `BatchLoader::GraphQL.for` to make it compatible with `graphql` gem versions `>= 1.8.7`. [#30](https://github.com/exAspArk/batch-loader/issues/30)
60
68
 
61
- #### [v1.2.2](https://github.com/exAspArk/batch-loader/compare/v1.2.1...v1.2.2)
69
+ #### [v1.2.2](https://github.com/exAspArk/batch-loader/compare/v1.2.1...v1.2.2) - 2018-12-03
62
70
 
63
71
  * `Fixed`: Identify item by `key` object instead of `key` string representation. [#27](https://github.com/exAspArk/batch-loader/pull/27)
64
72
 
65
- #### [v1.2.1](https://github.com/exAspArk/batch-loader/compare/v1.2.0...v1.2.1)
73
+ #### [v1.2.1](https://github.com/exAspArk/batch-loader/compare/v1.2.0...v1.2.1) - 2017-12-16
66
74
 
67
75
  * `Fixed`: Do not depend on `method_missing` for `respond_to?`. [#14](https://github.com/exAspArk/batch-loader/pull/14)
68
76
 
69
- #### [v1.2.0](https://github.com/exAspArk/batch-loader/compare/v1.1.1...v1.2.0)
77
+ #### [v1.2.0](https://github.com/exAspArk/batch-loader/compare/v1.1.1...v1.2.0) - 2017-11-16
70
78
 
71
79
  * `Added`: `key` argument for the `BatchLoader#batch` method. [#12](https://github.com/exAspArk/batch-loader/pull/12)
72
80
 
73
- #### [v1.1.1](https://github.com/exAspArk/batch-loader/compare/v1.1.0...v1.1.1)
81
+ #### [v1.1.1](https://github.com/exAspArk/batch-loader/compare/v1.1.0...v1.1.1) - 2017-11-06
74
82
 
75
83
  * `Fixed`: `loader`, made it thread-safe again. [#10](https://github.com/exAspArk/batch-loader/pull/10)
76
84
 
77
- #### [v1.1.0](https://github.com/exAspArk/batch-loader/compare/v1.0.4...v1.1.0)
85
+ #### [v1.1.0](https://github.com/exAspArk/batch-loader/compare/v1.0.4...v1.1.0) - 2017-11-02
78
86
 
79
87
  * `Added`: `default_value` override option. [#8](https://github.com/exAspArk/batch-loader/pull/8)
80
88
  * `Added`: `loader.call {}` block syntax, for memoizing repeat calls to the same item. [#8](https://github.com/exAspArk/batch-loader/pull/8)
81
89
 
82
- #### [v1.0.4](https://github.com/exAspArk/batch-loader/compare/v1.0.3...v1.0.4)
90
+ #### [v1.0.4](https://github.com/exAspArk/batch-loader/compare/v1.0.3...v1.0.4) - 2017-10-12
83
91
 
84
92
  * `Fixed`: Fix arity bug in `respond_to?` [#3](https://github.com/exAspArk/batch-loader/pull/3)
85
93
 
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
- post.user # N+1 queries
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(post.user_id).batch do |user_ids, loader|
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
- That's it.
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.preload(records.map(&:object), @preloads)
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
 
@@ -2,6 +2,6 @@ source "https://rubygems.org"
2
2
 
3
3
  gem 'coveralls'
4
4
 
5
- gem "graphql", ">= 1.8"
5
+ gem "graphql", "~> 1.8"
6
6
 
7
7
  gemspec
@@ -54,8 +54,8 @@ class BatchLoader
54
54
  @batch_loader = BatchLoader.for(item)
55
55
  end
56
56
 
57
- def batch(*kwargs, &block)
58
- @batch_loader.batch(*kwargs, &block)
57
+ def batch(**kwargs, &block)
58
+ @batch_loader.batch(**kwargs, &block)
59
59
  self
60
60
  end
61
61
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class BatchLoader
4
- VERSION = "2.0.0"
4
+ VERSION = "2.0.2"
5
5
  end
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.0
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - exAspArk
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-18 00:00:00.000000000 Z
11
+ date: 2023-11-23 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.2.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
data/.travis.yml DELETED
@@ -1,13 +0,0 @@
1
- language: ruby
2
- before_install: gem install bundler -v 2.0.1
3
- rvm:
4
- - 2.3.8
5
- - 2.4.10
6
- - 2.5.8
7
- - 2.6.6
8
- - 2.7.2
9
- - 3.0.0
10
- env:
11
- - CI=true
12
- gemfile:
13
- - graphql-latest.gemfile