graphql-preload 2.0.0 → 2.0.1

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
  SHA1:
3
- metadata.gz: c9cba006dfca0e52d2cbd40c5364594f358a3ac6
4
- data.tar.gz: 3086dde1fc427ba144b721b9ae0d10cdca6f0f2d
3
+ metadata.gz: ba8ef1cfb79ea9620cb392101e7ba7157bd19082
4
+ data.tar.gz: 96f464f9f3534d0b46328036ffc4a68166037c4b
5
5
  SHA512:
6
- metadata.gz: d1490ee109e5d08f0bd5c8a5d84ba29981487a0a7950942fbab89936272dbe73180e135406b748c297991381ea867fea8b5a5a2ac2d684e3ab29d8945562bc94
7
- data.tar.gz: 7de182aa0dba1117bbfb15482e7a218407f4022bdc62d8336f1133cac9c2149d424be4fa43f35e23dcc4ccab28692a154ce4e0f6ff8c5e73ccdf3cffbc862122
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
- Schema = GraphQL::Schema.define do
28
- use GraphQL::Batch
27
+ ```ruby
28
+ Schema = GraphQL::Schema.define do
29
+ use GraphQL::Batch
29
30
 
30
- enable_preloading
31
- end
31
+ enable_preloading
32
+ end
33
+ ```
32
34
 
33
35
  Call `preload` when defining your field:
34
36
 
35
- PostType = GraphQL::ObjectType.define do
36
- name 'Post'
37
+ ```ruby
38
+ PostType = GraphQL::ObjectType.define do
39
+ name 'Post'
37
40
 
38
- field :comments, !types[!CommentType] do
39
- # Post.includes(:comments)
40
- preload :comments
41
+ field :comments, !types[!CommentType] do
42
+ # Post.includes(:comments)
43
+ preload :comments
41
44
 
42
- # Post.includes(:comments, :authors)
43
- preload [:comments, :authors]
45
+ # Post.includes(:comments, :authors)
46
+ preload [:comments, :authors]
44
47
 
45
- # Post.includes(:comments, authors: [:followers, :posts])
46
- preload [:comments, { authors: [:followers, :posts] }]
48
+ # Post.includes(:comments, authors: [:followers, :posts])
49
+ preload [:comments, { authors: [:followers, :posts] }]
47
50
 
48
- resolve ->(obj, args, ctx) { obj.comments }
49
- end
50
- end
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
- PostType = GraphQL::ObjectType.define do
58
- name 'Post'
59
-
60
- field :comments, !types[!CommentType] do
61
- preload :comments
62
- preload_scope ->(args, ctx) { Comment.where(deleted_at: nil) }
63
-
64
- # Resolves with records returned from the following query:
65
- # SELECT "comments".*
66
- # FROM "comments"
67
- # WHERE "comments"."deleted_at" IS NULL
68
- # AND "comments"."post_id" IN (1, 2, 3)
69
- resolve ->(obj, args, ctx) { obj.comments }
70
- end
71
- end
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
- scope if scope.try(:klass) == model.reflect_on_association(association).klass
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
@@ -1,5 +1,5 @@
1
1
  module GraphQL
2
2
  module Preload
3
- VERSION = '2.0.0'.freeze
3
+ VERSION = '2.0.1'.freeze
4
4
  end
5
5
  end
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.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-10 00:00:00.000000000 Z
11
+ date: 2018-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord