graphql-preload 2.0.0 → 2.0.1
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 +37 -31
- data/lib/graphql/preload/loader.rb +4 -1
- data/lib/graphql/preload/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ba8ef1cfb79ea9620cb392101e7ba7157bd19082
|
4
|
+
data.tar.gz: 96f464f9f3534d0b46328036ffc4a68166037c4b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9d0e48b413c217329464a1410394d6602a454d434ab7bca8898ce16fcdddbc635bdd07de12a68f2461357b5f60de82a655fdcb997337b62b0e832d1178739f4
|
7
|
+
data.tar.gz: e4e1738b221ce13f6e42c1841b752bbfa9b4e6d63308a3f6f93085fc9f55e2a8b09edd8f701192b7740e8ec6df7e26bc44a81772712a9c817edf6dc6d1c932c0
|
data/README.md
CHANGED
@@ -24,51 +24,57 @@ Or install it yourself as:
|
|
24
24
|
|
25
25
|
First, enable preloading in your `GraphQL::Schema`:
|
26
26
|
|
27
|
-
|
28
|
-
|
27
|
+
```ruby
|
28
|
+
Schema = GraphQL::Schema.define do
|
29
|
+
use GraphQL::Batch
|
29
30
|
|
30
|
-
|
31
|
-
|
31
|
+
enable_preloading
|
32
|
+
end
|
33
|
+
```
|
32
34
|
|
33
35
|
Call `preload` when defining your field:
|
34
36
|
|
35
|
-
|
36
|
-
|
37
|
+
```ruby
|
38
|
+
PostType = GraphQL::ObjectType.define do
|
39
|
+
name 'Post'
|
37
40
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
+
field :comments, !types[!CommentType] do
|
42
|
+
# Post.includes(:comments)
|
43
|
+
preload :comments
|
41
44
|
|
42
|
-
|
43
|
-
|
45
|
+
# Post.includes(:comments, :authors)
|
46
|
+
preload [:comments, :authors]
|
44
47
|
|
45
|
-
|
46
|
-
|
48
|
+
# Post.includes(:comments, authors: [:followers, :posts])
|
49
|
+
preload [:comments, { authors: [:followers, :posts] }]
|
47
50
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
+
resolve ->(obj, args, ctx) { obj.comments }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
```
|
51
55
|
|
52
56
|
### `preload_scope`
|
53
57
|
Starting with Rails 4.1, you can scope your preloaded records by passing a valid scope to [`ActiveRecord::Associations::Preloader`](https://apidock.com/rails/v4.1.8/ActiveRecord/Associations/Preloader/preload). Scoping can improve performance by reducing the number of models to be instantiated and can help with certain business goals (e.g., only returning records that have not been soft deleted).
|
54
58
|
|
55
59
|
This functionality is surfaced through the `preload_scope` option:
|
56
60
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
61
|
+
```ruby
|
62
|
+
PostType = GraphQL::ObjectType.define do
|
63
|
+
name 'Post'
|
64
|
+
|
65
|
+
field :comments, !types[!CommentType] do
|
66
|
+
preload :comments
|
67
|
+
preload_scope ->(args, ctx) { Comment.where(deleted_at: nil) }
|
68
|
+
|
69
|
+
# Resolves with records returned from the following query:
|
70
|
+
# SELECT "comments".*
|
71
|
+
# FROM "comments"
|
72
|
+
# WHERE "comments"."deleted_at" IS NULL
|
73
|
+
# AND "comments"."post_id" IN (1, 2, 3)
|
74
|
+
resolve ->(obj, args, ctx) { obj.comments }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
```
|
72
78
|
|
73
79
|
## Development
|
74
80
|
|
@@ -39,7 +39,10 @@ module GraphQL
|
|
39
39
|
end
|
40
40
|
|
41
41
|
private def preload_scope
|
42
|
-
|
42
|
+
return nil unless scope
|
43
|
+
reflection = model.reflect_on_association(association)
|
44
|
+
raise ArgumentError, 'Cannot specify preload_scope for polymorphic associations' if reflection.polymorphic?
|
45
|
+
scope if scope.try(:klass) == reflection.klass
|
43
46
|
end
|
44
47
|
|
45
48
|
private def validate_association
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphql-preload
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Foster, Etienne Tripier
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-05-
|
11
|
+
date: 2018-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|