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 +4 -4
- data/README.md +93 -0
- data/lib/optimized_function.rb +1 -0
- data/rgraphql_preload_ar.gemspec +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aa2fc79bc75107460d053ba16b6910754a539be5eefa0c5273da0f3d45dc2526
|
4
|
+
data.tar.gz: 02deba4fd7f8afb67a0e84c6889094e0d71c1b14c308107a250d63eef5d2b02a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
+
```
|
data/lib/optimized_function.rb
CHANGED
@@ -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
|
data/rgraphql_preload_ar.gemspec
CHANGED
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.
|
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-
|
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
|