batch-loader 2.0.1 → 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: 5dec97d9cd0d12fbc684e4e7f1e68daa1371b028a82cf74ae4413ef2f39692d3
4
- data.tar.gz: 4cf02a45980a5beadfba923aaf92e844b269009663b23f5749b48444eff0fba7
3
+ metadata.gz: b25f607d37b2fec3d406a5f3451647a9c66e1f6ec061467c3b293ade83d1df06
4
+ data.tar.gz: ec1ea4291bf48e05ff606ce6de70908b70275c36a39016c361cc7303a33ebf79
5
5
  SHA512:
6
- metadata.gz: 4d4d2be50ff4ecac7222e21aeeca4d3f399f823ac27d1d249525a787a1b1e6550c7568301a02be7b9ced07ce8576056bc8769b022ad32ef1d75abf297d6ecf92
7
- data.tar.gz: 76560434aac7edd60f61e528a3eb1ad1cd2ed38231faa9d0e5f306a2b8809549567d2553ea28b5805304f22554278b59f51163c0989c094d840ce91343a2159c
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,10 +8,14 @@ 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.1...HEAD)
11
+ #### [Unreleased](https://github.com/exAspArk/batch-loader/compare/v2.0.2...HEAD)
12
12
 
13
13
  * WIP
14
14
 
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
+
15
19
  #### [v2.0.1](https://github.com/exAspArk/batch-loader/compare/v2.0.0...v2.0.1) - 2021-02-18
16
20
 
17
21
  * `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
- 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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class BatchLoader
4
- VERSION = "2.0.1"
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.1
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