graphql-sources 1.3.0 → 1.4.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b4484ab76e8479321a3b7c451654dfbf6f4d2bc8468305829e45d9edfebf7115
4
- data.tar.gz: e4838bff5cd3e1755e5a5cb3a714ba0502fdf2c24c6db4aeec190e45b2d9f238
3
+ metadata.gz: b83f8f866fbc38f5645d8887f0de9952e56126bdd0a48b0e2d5bca5099b4b110
4
+ data.tar.gz: bba362e0571ae8a9abdb344a6d17cb4894fac92a5d8f67ac7f556e0a336d6459
5
5
  SHA512:
6
- metadata.gz: 7f4ffac3729a6210f9a4ec7b5fbeebe37442d0e4c3578714e1648bc136b51f7abfe71b28432253607580c475f9c1f74992226f5af363857a715554dce0e92393
7
- data.tar.gz: f10d7510af4681949aac56d252496da8d633151cd0151e9f34bf920e9d9d192c9943e869f506f2c92c4733c1a8662070173fb923405aa66f3f4967642d46907f
6
+ metadata.gz: 696372d3f88a6db7d223c26ec3a27c77138a4473d03dc4285c8904f79271d2f044762133af5e7fa88c475c4ae2b035a66a4fa4caab699004b7dd2103ae9ce63a
7
+ data.tar.gz: 9f7014bd5a85a5c6f89e55da33f989f4939caf00411876d024f93cafa5b115e412668aa3cd8bb80a6cc168d4c9ababdc4484c340e1b772f8ed2a721ba2492c5a
data/README.md CHANGED
@@ -186,6 +186,39 @@ WHERE "likes"."post_id" IN (1, 2, 3, ...)
186
186
  GROUP BY "likes"."post_id"
187
187
  ```
188
188
 
189
+ ### Loading Exists
190
+
191
+ ```ruby
192
+ class User
193
+ has_many :purchases
194
+ end
195
+ ```
196
+
197
+ ```ruby
198
+ class Purchase
199
+ belongs_to :product
200
+ belongs_to :user
201
+ end
202
+ ```
203
+
204
+ ```ruby
205
+ class Product
206
+ has_many :purchases
207
+ end
208
+ ```
209
+
210
+ ```ruby
211
+ class ProductType
212
+ field :purchased, Boolean, null: false
213
+
214
+ def purchased
215
+ dataloader
216
+ .with(GraphQL::Sources::ActiveRecordExists, ::Purchase.where(user: context.user), key: :product_id)
217
+ .load(object.id)
218
+ end
219
+ end
220
+ ```
221
+
189
222
  ### Loading with `Rails.cache`
190
223
 
191
224
  ```ruby
@@ -4,30 +4,35 @@ module GraphQL
4
4
  module Sources
5
5
  # A class for loading a count of records.
6
6
  #
7
- # class Post
8
- # has_many :likes
7
+ # class User
8
+ # has_many :purchases
9
9
  # end
10
10
  #
11
- # class Like
12
- # belongs_to :post
11
+ # class Product
12
+ # has_many :purchases
13
13
  # end
14
14
  #
15
- # class PostType < GraphQL::Schema::Object
16
- # field :likes, Integer, null: false
15
+ # class Purchase
16
+ # belongs_to :product
17
+ # belongs_to :user
18
+ # end
19
+ #
20
+ # class ProductType < GraphQL::Schema::Object
21
+ # field :purchased, Boolean, null: false
17
22
  #
18
- # def likes
23
+ # def purchased
19
24
  # dataloader
20
- # .with(GraphQL::Sources::ActiveRecordCount, ::Like, key: :post_id)
25
+ # .with(GraphQL::Sources::ActiveRecordCount, ::Purchase.where(user: context.user), key: :product_id)
21
26
  # .load(object.id)
22
27
  # end
23
28
  # end
24
29
  #
25
30
  # The resulting SQL query is:
26
31
  #
27
- # SELECT "likes"."post_id", COUNT(*)
28
- # FROM "likes"
29
- # WHERE "likes"."post_id" IN (1, 2, 3, ...)
30
- # GROUP BY "likes"."post_id"
32
+ # SELECT "purchases"."post_id", COUNT(*)
33
+ # FROM "purchases"
34
+ # WHERE "purchases"."user_id" = ... AND "purchases"."product_id" IN (1, 2, 3, ...)
35
+ # GROUP BY "purchases"."post_id"
31
36
  class ActiveRecordCount < ActiveRecordBase
32
37
  # @param keys [Array] an array of keys
33
38
  # @return [Array] grouped counts for the keys
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Sources
5
+ # A class for checking existence of records.
6
+ #
7
+ # class Post
8
+ # has_many :likes
9
+ # end
10
+ #
11
+ # class Like
12
+ # belongs_to :post
13
+ # end
14
+ #
15
+ # class PostType < GraphQL::Schema::Object
16
+ # field :likes, Integer, null: false
17
+ #
18
+ # def likes
19
+ # dataloader
20
+ # .with(GraphQL::Sources::ActiveRecordExists, ::Like, key: :post_id)
21
+ # .load(object.id)
22
+ # end
23
+ # end
24
+ #
25
+ # The resulting SQL query is:
26
+ #
27
+ # SELECT "likes"."post_id"
28
+ # FROM "likes"
29
+ # WHERE "likes"."post_id" IN (1, 2, 3, ...)
30
+ # GROUP BY "likes"."post_id"
31
+ class ActiveRecordExists < ActiveRecordBase
32
+ # @param keys [Array] an array of keys
33
+ # @return [Array<Boolean>] an array of booleans
34
+ def fetch(keys)
35
+ set = Set.new(models(keys: keys).group(@key).pluck(@key))
36
+ keys.map { |key| set.member?(key) }
37
+ end
38
+ end
39
+ end
40
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module GraphQL
4
4
  module Sources
5
- VERSION = '1.3.0'
5
+ VERSION = '1.4.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-sources
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Sylvestre
@@ -220,6 +220,7 @@ files:
220
220
  - lib/graphql/sources/active_record_base.rb
221
221
  - lib/graphql/sources/active_record_collection.rb
222
222
  - lib/graphql/sources/active_record_count.rb
223
+ - lib/graphql/sources/active_record_exists.rb
223
224
  - lib/graphql/sources/active_record_object.rb
224
225
  - lib/graphql/sources/active_storage_base.rb
225
226
  - lib/graphql/sources/active_storage_has_many_attached.rb