rgraphql_preload_ar 1.0.2 → 1.0.3

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: 2cdbecb9b1ecb068e3f59310259382760a58eadf091da2f554f7730357cdfe1e
4
- data.tar.gz: 9a31db9385e23ff3796f7e4ec6758b5f623e2adbb349547664b4c100380e7adb
3
+ metadata.gz: aa2fc79bc75107460d053ba16b6910754a539be5eefa0c5273da0f3d45dc2526
4
+ data.tar.gz: 02deba4fd7f8afb67a0e84c6889094e0d71c1b14c308107a250d63eef5d2b02a
5
5
  SHA512:
6
- metadata.gz: b76b4e3c2d94f4a11256b47473074aa46192173aac94d8c05ca463ee63f85c7df70f664f5c07252794b0df8035f0f5446890386ffe4bb13f2c2bad3264f66495
7
- data.tar.gz: 2aece3c7fbc039c575858d04835b738436719cc7c6375dfaafcde27ffde7eab7aa18f7a705a63bbfb4aa3cbbd160c7b4d0ac532966ce317680a50f2183fd3e79
6
+ metadata.gz: 116f582a484dbd1e3452a94b9790f9b5bbbfdfe39d5fd0ed52cde8b7e0a700cb9c70ef73a0687206512d00950fa0900b559d605430567ba3e6a35c74f98bc697
7
+ data.tar.gz: 8db5cd99ad9564ae6283042f9b9b27e9ce00b49a8a941f471feccc51aaf2876dac5ee3295b96263e94ff805bb6b95e3b905b1a1cfbc62de6005f5979a34cc20f
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ ## What's this gem do
2
+
3
+ preload ActiveRecord Associations when using graphql and ActiveRecord
4
+
5
+ ## Install
6
+
7
+ `gem rgraphql_preload_ar`
8
+
9
+ ## Scene
10
+
11
+ Your original code of resolver might look like this:
12
+
13
+ ```ruby
14
+ class Resolvers::ProfileResolver < GraphQL::Function
15
+ description 'User profile'
16
+
17
+ type Types::ProfileType
18
+
19
+ def _call(_, args, ctx)
20
+ ctx[:current_user]
21
+ end
22
+ end
23
+ ```
24
+
25
+ If the query like this:
26
+ ```
27
+ query{
28
+ profile{
29
+ id
30
+ works{
31
+ comments{
32
+ replyUser{
33
+ name
34
+ }
35
+ content
36
+ }
37
+ name
38
+ id
39
+ likes{
40
+ owner{
41
+ name
42
+ works{
43
+ name
44
+ likes{
45
+ owner{
46
+ name
47
+ }
48
+ }
49
+ }
50
+ }
51
+ }
52
+ }
53
+ name
54
+ }
55
+ }
56
+ ```
57
+
58
+ It will cause Multi-level nested N + 1 problem.
59
+
60
+ And you want to resolver it, you can write like this:
61
+
62
+ ```ruby
63
+ class Resolvers::ProfileResolver < OptimizedFunction
64
+ description 'User profile'
65
+
66
+ type Types::ProfileType
67
+
68
+ def _call(_, args, ctx)
69
+ User.includes([works: [comments: :reply_user, likes: [owner: [works: [likes: :owner]]]]]).find(ctx[:current_user].id)
70
+ end
71
+ end
72
+ ```
73
+
74
+ You will manually resolve N + 1 problem in every resolver.Worse, even if only one field is used, you need to request all the tables included.
75
+
76
+ ## Usage
77
+
78
+ Your Resolver inherits from `OptimizedFunction` instead of `GraphQL::Function`.
79
+ Defining `_call` method instead of `call`
80
+ Using `includes_klass(your_model_name)` instead of your original includes statement.
81
+
82
+
83
+ ```ruby
84
+ class Resolvers::ProfileResolver < OptimizedFunction
85
+ description 'User profile'
86
+
87
+ type Types::ProfileType
88
+
89
+ def _call(_, args, ctx)
90
+ includes_kclass(User).find(ctx[:current_user].id)
91
+ end
92
+ end
93
+ ```
@@ -33,6 +33,7 @@ class OptimizedFunction < GraphQL::Function
33
33
  tree.each do |k, v|
34
34
  if v.present? && klass.reflect_on_all_associations.map(&:name).include?(k)
35
35
  result_tree[k.to_sym] = {}
36
+ next if klass.reflections[k.to_s].options[:polymorphic]
36
37
  _klass = klass.reflections[k.to_s].class_name.constantize
37
38
  gen_result_tree _klass, tree[k], result_tree[k.to_sym]
38
39
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'rgraphql_preload_ar'
3
- s.version = "1.0.2"
3
+ s.version = "1.0.3"
4
4
  s.date = Time.now.strftime("%Y-%m-%d")
5
5
  s.summary = %q{rails graphql preload ar in resolver}
6
6
  s.files = `git ls-files`.split($/)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rgraphql_preload_ar
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - tinyfeng.hou
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-12 00:00:00.000000000 Z
11
+ date: 2019-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql
@@ -34,6 +34,7 @@ files:
34
34
  - ".gitignore"
35
35
  - Gemfile
36
36
  - Gemfile.lock
37
+ - README.md
37
38
  - lib/optimized_function.rb
38
39
  - lib/rgraphql_preload_ar.rb
39
40
  - rgraphql_preload_ar.gemspec