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 +4 -4
- data/README.md +33 -0
- data/lib/graphql/sources/active_record_count.rb +17 -12
- data/lib/graphql/sources/active_record_exists.rb +40 -0
- data/lib/graphql/sources/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b83f8f866fbc38f5645d8887f0de9952e56126bdd0a48b0e2d5bca5099b4b110
|
4
|
+
data.tar.gz: bba362e0571ae8a9abdb344a6d17cb4894fac92a5d8f67ac7f556e0a336d6459
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
8
|
-
# has_many :
|
7
|
+
# class User
|
8
|
+
# has_many :purchases
|
9
9
|
# end
|
10
10
|
#
|
11
|
-
# class
|
12
|
-
#
|
11
|
+
# class Product
|
12
|
+
# has_many :purchases
|
13
13
|
# end
|
14
14
|
#
|
15
|
-
# class
|
16
|
-
#
|
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
|
23
|
+
# def purchased
|
19
24
|
# dataloader
|
20
|
-
# .with(GraphQL::Sources::ActiveRecordCount, ::
|
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 "
|
28
|
-
# FROM "
|
29
|
-
# WHERE "
|
30
|
-
# GROUP BY "
|
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
|
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.
|
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
|