graphql_activerecord_autoselect 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fffb18c54e73d6cb101b8c56dc3bd5e5d1419230d69a6bad9c8d7ad3a88326f7
4
+ data.tar.gz: 651b2014ca1fc9c0396f37dd4b3e0ca641f2580a64f8dc9045e9c3799c732a2b
5
+ SHA512:
6
+ metadata.gz: ef4bed4908f6ccd253dcec80d9545e28df2ba967a88106d9ed97560d0570620411b87e2546174a89753fa3e25fc92cf27c89b420277567d154d6ba7d3c669a43
7
+ data.tar.gz: 7843c8142c0752bba929f1f71f961702c8abe7c11bbdd7238ac20331b638e3876215c02ad187a21f81c3b65688a68bc65c5c0a892ac5a7c4a96ecd0a1520de92
@@ -0,0 +1,3 @@
1
+ ## v1.0.0
2
+
3
+ * Initial release
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) Michael van Rooijen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,276 @@
1
+ # GraphQL ActiveRecord AutoSelect
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/graphql_activerecord_autoselect.svg)](https://badge.fury.io/rb/graphql_activerecord_autoselect)
4
+ [![Test Status](https://github.com/mrrooijen/graphql_activerecord_autoselect/workflows/Test/badge.svg)](https://github.com/mrrooijen/graphql_activerecord_autoselect/actions)
5
+
6
+ Automatic [ActiveRecord] column selection for [GraphQL (Ruby)] fields.
7
+
8
+ This library was developed for- and extracted from [HireFire].
9
+
10
+ The documentation can be found on [RubyDoc].
11
+
12
+
13
+ ### Compatibility
14
+
15
+ - Ruby 2.5+
16
+ - ActiveRecord 6.0+
17
+ - GraphQL (Ruby) 1.9+
18
+
19
+
20
+ ### Installation
21
+
22
+ Add the gem to your Gemfile and run `bundle`.
23
+
24
+ ```ruby
25
+ gem "graphql_activerecord_autoselect", "~> 1"
26
+ ```
27
+
28
+
29
+ ### Example
30
+
31
+ ```rb
32
+ class Types::Actor < Types::Base::Object
33
+ using GraphQLActiveRecordAutoSelect
34
+
35
+ field :organizations, Types::Organization, null: false, extras: [:lookahead]
36
+
37
+ def organizations(lookahead:)
38
+ object.organizations.autoselect(lookahead)
39
+ end
40
+
41
+ field :organization, Types::Organization, null: false, extras: [:lookahead] do
42
+ argument :id, ID, required: true
43
+ end
44
+
45
+ def organization(id:, lookahead:)
46
+ object.organizations.autoselect(lookahead).find(id)
47
+ end
48
+ end
49
+ ```
50
+
51
+ In this example an actor has many organizations. We allow the client to query all of the actor's organizations, or a specific one by id. We acquire the lookahead functionality provided by GraphQL Ruby and pass it to `autoselect`.
52
+
53
+ The `autoselect` method is made available to all instances of ActiveRecord::Base and ActiveRecord::Relations in classes where the GraphQLActiveRecordAutoSelect refinement is used (`using GraphQLActiveRecordAutoSelect`).
54
+
55
+ If you submit the following query to the server:
56
+
57
+ ```
58
+ query {
59
+ actor {
60
+ organizations {
61
+ name
62
+ time_zone
63
+ }
64
+ }
65
+ }
66
+ ```
67
+
68
+ It'll translate this:
69
+
70
+ ```rb
71
+ object.organizations.autoselect(lookahead)
72
+ ```
73
+
74
+ Into this:
75
+
76
+ ```rb
77
+ object.organizations.select(:id, :actor_id, :name, :time_zone)
78
+ ```
79
+
80
+ Notice that it didn't only select the `:name` and `:time_zone` fields, but `:id` and `:actor_id` as well. These kinds of identifiers are always selected in order to avoid potential lookup issues on other relations where they're mandatory.
81
+
82
+ The following fields are always selected:
83
+
84
+ * The primary key field (typically id)
85
+ * The type field
86
+ * Fields that end in _id
87
+ * Fields that end in _type
88
+
89
+ The example above builds a select on a has many relation, but the same works for belongs to.
90
+
91
+ ```rb
92
+ class Types::Organization < Types::Base::Object
93
+ using GraphQLActiveRecordAutoSelect
94
+
95
+ field :actor, Types::Actor, null: false, extras: [:lookahead]
96
+
97
+ def actor(lookahead:)
98
+ Actor.autoselect(lookahead).find(object.actor_id)
99
+ end
100
+ end
101
+ ```
102
+
103
+ And with this query:
104
+
105
+ ```
106
+ query {
107
+ organization {
108
+ actor {
109
+ email
110
+ }
111
+ }
112
+ }
113
+ ```
114
+
115
+ It'll translate this:
116
+
117
+ ```rb
118
+ Actor.autoselect(lookahead).find(object.actor_id)
119
+ ```
120
+
121
+ Into this:
122
+
123
+ ```rb
124
+ Actor.select(:id, :email).find(object.actor_id)
125
+ ```
126
+
127
+
128
+ #### Dependents
129
+
130
+ You might have fields or methods that depend on the presence of one or more fields. You can specify dependents to ensure that the requested field or method will be able to successfully compute:
131
+
132
+ ```rb
133
+ class Actor < ActiveRecord::Base
134
+ def secret
135
+ decrypt(secret_digest)
136
+ end
137
+
138
+ private
139
+
140
+ def decrypt(column)
141
+ # ...
142
+ end
143
+ end
144
+
145
+ class Types::Actor < Types::Base::Object
146
+ # ...
147
+
148
+ field :email, String, null: false
149
+ field :full_name, String, null: false
150
+ field :secret, String, null: false
151
+
152
+ def self.dependents
153
+ {
154
+ :full_name => [:first_name, :last_name],
155
+ :secret => [:secret_digest],
156
+ }
157
+ end
158
+
159
+ def full_name
160
+ "#{object.first_name} #{object.last_name}"
161
+ end
162
+
163
+ # ...
164
+ end
165
+
166
+ class Types::Organization < Types::Base::Object
167
+ using GraphQLActiveRecordAutoSelect
168
+
169
+ field :actor, Types::Actor, null: false, extras: [:lookahead]
170
+
171
+ def actor(lookahead:)
172
+ Actor.autoselect(lookahead, Types::Actor.dependents).find(object.actor_id)
173
+ end
174
+ end
175
+ ```
176
+
177
+ There's a few things to note here.
178
+
179
+ 1. We've defined the `secret` method in the Actor model which decrypts and returns the encrypted data stored in the `secret_digest` column.
180
+ 2. We've defined `self.dependents` on the `Types::Actor` class which describes which fields depend on what columns.
181
+ 3. We've passed in that dependent configuration to the second argument of `autoselect`.
182
+
183
+ We're essentially telling autoselect that if the client selects the `full_name` field, that it has to select the `first_name` and `last_name` columns on the model. Likewise, when selecting the `secret` field, it'll select the `secret_digest` column on the model so that it has the encrypted data it needs to decrypt and return.
184
+
185
+ ```
186
+ query {
187
+ organization {
188
+ actor {
189
+ email
190
+ full_name
191
+ secret
192
+ }
193
+ }
194
+ }
195
+ ```
196
+
197
+ It'll translates this:
198
+
199
+ ```rb
200
+ Actor.autoselect(lookahead, Actor.dependents).find(object.actor_id)
201
+ ```
202
+
203
+ Into this:
204
+
205
+ ```rb
206
+ Actor.select(:id, :email, :first_name, :last_name, :secret_digest).find(object.actor_id)
207
+ ```
208
+
209
+ Since we've selected the `full_name` and `secret` fields, autoselect selects the `first_name`, `last_name`, and `secret_digest` columns, as these are necessary in order to compute `full_name` and `secret`.
210
+
211
+
212
+ ### Contributing
213
+
214
+ Bug reports and pull requests are welcome on GitHub at:
215
+
216
+ https://github.com/mrrooijen/graphql_activerecord_autoselect
217
+
218
+ To install the dependencies:
219
+
220
+ ```
221
+ $ bundle
222
+ ```
223
+
224
+ To open an interactive console:
225
+
226
+ ```
227
+ $ bundle console
228
+ ```
229
+
230
+ To run the tests:
231
+
232
+ ```
233
+ $ bundle exec rake
234
+ ```
235
+
236
+ To view the code coverage (generated after each test run):
237
+
238
+ ```
239
+ $ open coverage/index.html
240
+ ```
241
+
242
+ To run the local documentation server:
243
+
244
+ ```
245
+ $ bundle exec rake doc
246
+ ```
247
+
248
+ To build a gem:
249
+
250
+ ```
251
+ $ bundle exec rake build
252
+ ```
253
+
254
+ To build and install a gem on your local machine:
255
+
256
+ ```
257
+ $ bundle exec rake install
258
+ ```
259
+
260
+ For a list of available tasks:
261
+
262
+ ```
263
+ $ bundle exec rake --tasks
264
+ ```
265
+
266
+
267
+ ### Author / License
268
+
269
+ Released under the [MIT License] by [Michael van Rooijen].
270
+
271
+ [Michael van Rooijen]: https://michael.vanrooijen.io
272
+ [HireFire]: https://www.hirefire.io
273
+ [RubyDoc]: https://rubydoc.info/gems/graphql_activerecord_autoselect
274
+ [MIT License]: https://github.com/mrrooijen/graphql_activerecord_autoselect/blob/master/LICENSE.txt
275
+ [GraphQL (Ruby)]: https://github.com/rmosolgo/graphql-ruby
276
+ [ActiveRecord]: https://github.com/rails/rails/tree/master/activerecord
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "graphql_activerecord_autoselect/version"
4
+ require "active_record"
5
+
6
+ module GraphQLActiveRecordAutoSelect
7
+ extend self
8
+
9
+ refine ::ActiveRecord::Base.singleton_class do
10
+ def autoselect(lookahead, dependents = {})
11
+ select(
12
+ GraphQLActiveRecordAutoSelect.call(
13
+ :model => self,
14
+ :lookahead => lookahead,
15
+ :dependents => dependents,
16
+ )
17
+ )
18
+ end
19
+ end
20
+
21
+ refine ::ActiveRecord::Relation do
22
+ def autoselect(lookahead, dependents = {})
23
+ select(
24
+ GraphQLActiveRecordAutoSelect.call(
25
+ :model => model,
26
+ :lookahead => lookahead,
27
+ :dependents => dependents
28
+ )
29
+ )
30
+ end
31
+ end
32
+
33
+ def call(model:, lookahead:, dependents:)
34
+ primary_key = model.primary_key
35
+ columns = model.column_names
36
+ fields = get_fields(lookahead)
37
+
38
+ Array.new.tap do |selection|
39
+ selection.concat filter_by_columns(fields, columns)
40
+ selection.concat include_identifier_columns(primary_key, columns)
41
+ selection.concat include_dependents(fields, dependents)
42
+ selection.compact!
43
+ selection.uniq!
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def filter_by_columns(fields, columns)
50
+ fields.select { |name| columns.include?(name) }
51
+ end
52
+
53
+ def include_identifier_columns(primary_key, columns)
54
+ columns.select do |name|
55
+ name == primary_key || name == "type" || name.end_with?("_type") || name.end_with?("_id")
56
+ end
57
+ end
58
+
59
+ def include_dependents(fields, dependents)
60
+ dependents.reduce([]) do |selection, (field, columns)|
61
+ if fields.include?(field.to_s)
62
+ selection + columns.map(&:to_s)
63
+ else
64
+ selection
65
+ end
66
+ end
67
+ end
68
+
69
+ def get_fields(lookahead)
70
+ get_selections(lookahead).map(&:name).map(&:to_s)
71
+ end
72
+
73
+ def get_selections(lookahead)
74
+ if lookahead.selection(:edges).selects?(:node)
75
+ lookahead.selection(:edges).selection(:node).selections
76
+ elsif lookahead.selects?(:nodes)
77
+ lookahead.selection(:nodes).selections
78
+ else
79
+ lookahead.selections
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQLActiveRecordAutoSelect
4
+ VERSION = "1.0.0"
5
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphql_activerecord_autoselect
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael van Rooijen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-06-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: 7.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 6.0.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: 7.0.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: graphql
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 1.9.0
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: 2.0.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 1.9.0
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: 2.0.0
53
+ - !ruby/object:Gem::Dependency
54
+ name: rake
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - '='
58
+ - !ruby/object:Gem::Version
59
+ version: 12.3.3
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '='
65
+ - !ruby/object:Gem::Version
66
+ version: 12.3.3
67
+ - !ruby/object:Gem::Dependency
68
+ name: yard
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '='
72
+ - !ruby/object:Gem::Version
73
+ version: 0.9.25
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '='
79
+ - !ruby/object:Gem::Version
80
+ version: 0.9.25
81
+ - !ruby/object:Gem::Dependency
82
+ name: minitest
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '='
86
+ - !ruby/object:Gem::Version
87
+ version: 5.14.1
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '='
93
+ - !ruby/object:Gem::Version
94
+ version: 5.14.1
95
+ - !ruby/object:Gem::Dependency
96
+ name: simplecov
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '='
100
+ - !ruby/object:Gem::Version
101
+ version: 0.18.5
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '='
107
+ - !ruby/object:Gem::Version
108
+ version: 0.18.5
109
+ - !ruby/object:Gem::Dependency
110
+ name: sqlite3
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - '='
114
+ - !ruby/object:Gem::Version
115
+ version: 1.4.2
116
+ type: :development
117
+ prerelease: false
118
+ version_requirements: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - '='
121
+ - !ruby/object:Gem::Version
122
+ version: 1.4.2
123
+ description:
124
+ email:
125
+ - michael@vanrooijen.io
126
+ executables: []
127
+ extensions: []
128
+ extra_rdoc_files: []
129
+ files:
130
+ - CHANGELOG.md
131
+ - LICENSE.txt
132
+ - README.md
133
+ - lib/graphql_activerecord_autoselect.rb
134
+ - lib/graphql_activerecord_autoselect/version.rb
135
+ homepage: https://github.com/mrrooijen/graphql_activerecord_autoselect
136
+ licenses:
137
+ - MIT
138
+ metadata:
139
+ homepage_uri: https://github.com/mrrooijen/graphql_activerecord_autoselect
140
+ source_code_uri: https://github.com/mrrooijen/graphql_activerecord_autoselect
141
+ changelog_uri: https://github.com/mrrooijen/graphql_activerecord_autoselect/blob/master/CHANGELOG.md
142
+ bug_tracker_uri: https://github.com/mrrooijen/graphql_activerecord_autoselect/issues
143
+ documentation_uri: https://rubydoc.info/gems/graphql_activerecord_autoselect
144
+ post_install_message:
145
+ rdoc_options: []
146
+ require_paths:
147
+ - lib
148
+ required_ruby_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: 2.5.0
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ requirements: []
159
+ rubygems_version: 3.1.2
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: Automatic ActiveRecord column selection for GraphQL (Ruby) fields.
163
+ test_files: []