graphql-eager_load 0.2.2 → 0.2.3

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
  SHA256:
3
- metadata.gz: a51028f140032e8b436a06fa19ad0d5fe1ad5e16ac2f0881acabaa3738e287bc
4
- data.tar.gz: cb1cf7507e675e881699e679d6a9f248ce1a7bcd0697e6f1d90020325b1482cd
3
+ metadata.gz: c1f55715d1944da41fec7fd847f763d0ccf050171ec4549882ea960929d6a225
4
+ data.tar.gz: 6a7be6628e2a82112a28791a607bb4440fd940df7735ce786ed6c6d15b8a6587
5
5
  SHA512:
6
- metadata.gz: 29058b322738f586de82e832722625fa5c1c09ce7716cc2377cfe30b1a7e0dbf21655221d9977135f683938241a1b2141632d6ded7affe4dd86e82c1db1c6ee0
7
- data.tar.gz: 1ca0c090115b58c18bd01826c20f93c6019e53bba375ff970b3b8406223b16c2b2b4a3b57b1314620fbc099d692784924bc154dfd75a3f114c265cdc294b758a
6
+ metadata.gz: 1f3d89d38f9a040add5556cb2aa69bdeebb18004fdfc8e632cde8d1ed6e52dbb1fcc164a40a313ab37d945403502659f11411133b9920e942386c4a1c585e9b7
7
+ data.tar.gz: 839bcb503679904200af7cb0b1b397b8779c5d438ab5adb4e326354d779a0115e8476ad61b6bb961c4eaceb3dc70de627b9bb81c0991bbaa9530bbdb7e5e72ee
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- graphql-eager_load (0.2.2)
4
+ graphql-eager_load (0.2.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -67,16 +67,16 @@ GEM
67
67
  activesupport (>= 3.0.0)
68
68
  railties (>= 3.0.0)
69
69
  thor (>= 0.14.6)
70
- concurrent-ruby (1.1.6)
70
+ concurrent-ruby (1.1.9)
71
71
  crass (1.0.6)
72
72
  diff-lcs (1.4.4)
73
- erubi (1.9.0)
74
- globalid (0.4.2)
75
- activesupport (>= 4.2.0)
73
+ erubi (1.10.0)
74
+ globalid (0.5.2)
75
+ activesupport (>= 5.0)
76
76
  graphql (1.11.1)
77
- i18n (1.8.5)
77
+ i18n (1.8.10)
78
78
  concurrent-ruby (~> 1.0)
79
- loofah (2.6.0)
79
+ loofah (2.12.0)
80
80
  crass (~> 1.0.2)
81
81
  nokogiri (>= 1.5.9)
82
82
  mail (2.7.1)
@@ -84,16 +84,20 @@ GEM
84
84
  marcel (0.3.3)
85
85
  mimemagic (~> 0.3.2)
86
86
  method_source (1.0.0)
87
- mimemagic (0.3.5)
87
+ mimemagic (0.3.10)
88
+ nokogiri (~> 1)
89
+ rake
88
90
  mini_mime (1.0.2)
89
- mini_portile2 (2.4.0)
90
- minitest (5.14.1)
91
+ mini_portile2 (2.6.1)
92
+ minitest (5.14.4)
91
93
  nio4r (2.5.2)
92
- nokogiri (1.10.10)
93
- mini_portile2 (~> 2.4.0)
94
+ nokogiri (1.12.4)
95
+ mini_portile2 (~> 2.6.1)
96
+ racc (~> 1.4)
94
97
  parallel (1.19.2)
95
98
  parser (2.7.1.4)
96
99
  ast (~> 2.4.1)
100
+ racc (1.5.2)
97
101
  rack (2.2.3)
98
102
  rack-test (1.1.0)
99
103
  rack (>= 1.0, < 3)
@@ -115,7 +119,7 @@ GEM
115
119
  rails-dom-testing (2.0.3)
116
120
  activesupport (>= 4.2.0)
117
121
  nokogiri (>= 1.6)
118
- rails-html-sanitizer (1.3.0)
122
+ rails-html-sanitizer (1.4.2)
119
123
  loofah (~> 2.3)
120
124
  railties (6.0.3.2)
121
125
  actionpack (= 6.0.3.2)
@@ -174,13 +178,13 @@ GEM
174
178
  sqlite3 (1.4.2)
175
179
  thor (1.0.1)
176
180
  thread_safe (0.3.6)
177
- tzinfo (1.2.7)
181
+ tzinfo (1.2.9)
178
182
  thread_safe (~> 0.1)
179
183
  unicode-display_width (1.7.0)
180
184
  websocket-driver (0.7.3)
181
185
  websocket-extensions (>= 0.1.0)
182
186
  websocket-extensions (0.1.5)
183
- zeitwerk (2.4.0)
187
+ zeitwerk (2.4.2)
184
188
 
185
189
  PLATFORMS
186
190
  ruby
@@ -199,4 +203,4 @@ DEPENDENCIES
199
203
  sqlite3
200
204
 
201
205
  BUNDLED WITH
202
- 2.1.4
206
+ 2.2.11
data/README.md CHANGED
@@ -1,7 +1,9 @@
1
1
  # Graphql::EagerLoad
2
2
 
3
- This gem traverses your graphql query looking for fields on types that match
4
- associations on those types. For each field found to be an association, this
3
+ This gem assumes you are using [Rails](http://rubyonrails.org) and [ActiveRecord](https://guides.rubyonrails.org/active_record_querying.html). And that your GraphQL types and fields map as closely to your data model as possible. It uses those assumptions, or conventions, to handle N+1 prevention for you.
4
+
5
+ It does so by traversing your graphql query looking for fields on types that match
6
+ associations on those types' corresponding models. For each field found to be an association, this
5
7
  gem adds those associations to an [ActiveRecord::QueryMethods#includes](https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-includes) hash. That hash can then be used to
6
8
  eager load the associations of records returned in your graphql resolver.
7
9
 
@@ -35,6 +37,64 @@ end
35
37
 
36
38
  The `.eager_load_model` and `#associations_to_eager_load` methods are provided by this gem.
37
39
 
40
+
41
+ If your type defines a custom method for a field that maps to an ActiveRecord association, this gem will ignore that association and let your method handle it. If you still want to eager load the association in that case you need to let the gem know.
42
+
43
+ ```ruby
44
+ module Types
45
+ class User < Types::Base
46
+ field :estimates, [Types::Estimate], null: false
47
+ field :profile_photo, Types::File, null: true
48
+
49
+ def profile_photo
50
+ object.profile_photo if object.profile_photo.attached?
51
+ end
52
+
53
+ def self.allow_include_builder_fields
54
+ [:profile_photo]
55
+ end
56
+ end
57
+ end
58
+ ```
59
+
60
+ Given this query:
61
+
62
+ ```javascript
63
+ users {
64
+ id
65
+ estimates {
66
+ id
67
+ }
68
+ profilePhoto {
69
+ redirectUrl
70
+ filename
71
+ }
72
+ }
73
+ ```
74
+
75
+ The output of the `#associations_to_eager_load` helper method would be `{estimates: {}, profile_photo: {blob: {}}`. Without the `.allow_include_builder_fields` class method the output would be `{estimates: {}}`.
76
+
77
+ If you have a field that is derived from an association, but the association is not included in the query, you can define a `#custom_associations_for_fields` method to specify which associations to include for a specific field.
78
+
79
+ ```ruby
80
+ module Types
81
+ class User < Types::Base
82
+ field :estimates, [Types::Estimate], null: false
83
+ field :org_names, [String], null: true
84
+
85
+ def org_names
86
+ object.orgs.map(&:name)
87
+ end
88
+
89
+ def self.custom_associations_for_fields
90
+ {
91
+ org_names: [:org]
92
+ }
93
+ end
94
+ end
95
+ end
96
+ ```
97
+
38
98
  ## Development
39
99
 
40
100
  After checking out the repo, run `bundle` to install dependencies. Then, run `bundle exec rake spec` to run the tests. You can also run `bundle exec bin/console` for an interactive prompt that will allow you to experiment.
@@ -42,6 +42,8 @@ module Graphql
42
42
  else
43
43
  includes.merge!(builder.includes)
44
44
  end
45
+
46
+ includes.deep_merge!(builder.custom_associations_for_selection)
45
47
  end
46
48
  end
47
49
 
@@ -66,6 +68,16 @@ module Graphql
66
68
  model.reflect_on_attachment(field_name).present?
67
69
  end
68
70
 
71
+ def custom_associations_for_selection
72
+ return {} unless field_owner.respond_to?(:custom_associations_for_fields)
73
+
74
+ custom_associations = field_owner.custom_associations_for_fields[field_name.to_sym]
75
+
76
+ return {} unless custom_associations
77
+
78
+ custom_associations.map { |association| [association, {}] }.to_h
79
+ end
80
+
69
81
  private
70
82
 
71
83
  attr_reader :selection, :model
@@ -12,7 +12,7 @@ module Graphql
12
12
  graphql_eager_load_options(model: self.class.class_variable_get(:@@eager_load_model))
13
13
  end
14
14
 
15
- def graphql_eager_load_options(selections: context.query.lookahead.selections, model:)
15
+ def graphql_eager_load_options(model:, selections: context.query.lookahead.selections)
16
16
  Builder.call(selections: selections, model: model)
17
17
  end
18
18
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Graphql
4
4
  module EagerLoad
5
- VERSION = '0.2.2'
5
+ VERSION = '0.2.3'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-eager_load
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elise Wood
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-11 00:00:00.000000000 Z
11
+ date: 2022-11-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: ''
14
14
  email:
@@ -57,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
57
  - !ruby/object:Gem::Version
58
58
  version: '0'
59
59
  requirements: []
60
- rubygems_version: 3.1.2
60
+ rubygems_version: 3.2.11
61
61
  signing_key:
62
62
  specification_version: 4
63
63
  summary: Generate ActiveRecord eager loading options from GraphQL Queries.