graphql-active_record_batcher 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 987cfab30dfdc3481d7feff6ffe29273ed8de21c
4
+ data.tar.gz: 067a21a5f99cb3f68e6b1084a685e8839a82e442
5
+ SHA512:
6
+ metadata.gz: 3f68ca34876e1ef67ce416b428fafb11b648388b68e5c36d4f017163ebe5cd82482822014accc07bd77a981e6c873975b7b20e8c6a8f7560b55b85e3007b16f7
7
+ data.tar.gz: b536f7dc3d2ccb195ebc70e66fcf4326185236acebcf057c2dfcef4770a464c7fa66b9ffe7d54b1ec7549f62125141f4c5a0007ce9139133ab55f4b5211e8b58
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .byebug_history
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at mgiroux0@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in graphql-activerecord-preload.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 TODO: Write your name
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.
data/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # Graphql::ActiveRecordBatcher
2
+
3
+ `GraphQL::ActiveRecordBatcher` is a toolkit to batch record loading as well as preload
4
+ ActiveRecord association durings GraphQL Execution.
5
+
6
+ It is meant to be used with the `graphql` gem and uses `graphql-batch` under the hood to
7
+ preload and batch calls to your database.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'graphql-active_record_batcher'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install graphql-active_record_batcher
24
+
25
+ ## Usage
26
+
27
+ ### Preloading Associations
28
+
29
+ Using GraphQL without preloading associations results in under performant API and
30
+ too many queries to the database. Take for example a Movie type which has a `characters` associations, and this GraphQL query:
31
+
32
+ ```graphql
33
+ query {
34
+ movie(id: "1") {
35
+ characters {
36
+ name
37
+ }
38
+ }
39
+ movie(id: "2") {
40
+ characters {
41
+ name
42
+ }
43
+ }
44
+ }
45
+ ```
46
+
47
+ This query would result in 4 calls to your database:
48
+
49
+ ```sql
50
+ 1: SELECT "movies".* FROM "movies" WHERE "movies"."id" = ? LIMIT ? [["id", 1]
51
+ 2: SELECT "movies".* FROM "movies" WHERE "movies"."id" = ? LIMIT ? [["id", 2]
52
+ 3: SELECT "characters".* FROM "characters" WHERE "characters"."movie_id" = ? [["movie_id", 1]
53
+ 4: SELECT "characters".* FROM "characters" WHERE "characters"."movie_id" = ? [["movie_id", 2]
54
+ ```
55
+
56
+ `GraphQL::ActiveRecordBatcher` lets you preload associations by using the `preload` definition during field definition:
57
+
58
+ ```ruby
59
+ StarWarsMovie = GraphQL::ObjectType.define do
60
+ name "StarWarsMovie"
61
+ description "A StarWars Movie"
62
+
63
+ # Define which active record model represents
64
+ # the parent object
65
+ model Movie
66
+
67
+ field :characters, !types[!Dog] do
68
+ preloads(:characters)
69
+ resolve ->(movie, _, _) { movie.characters }
70
+ end
71
+ end
72
+ ```
73
+
74
+ Associations will now be preloaded and only 3 queries are used this time:
75
+
76
+ ```sql
77
+ 1: SELECT "movies".* FROM "movies" WHERE "movies"."id" = ? LIMIT ? [["id", 1]
78
+ 2: SELECT "movies".* FROM "movies" WHERE "movies"."id" = ? LIMIT ? [["id", 2]
79
+ 3: SELECT "characters".* FROM "characters" WHERE "characters"."movie_d" IN (1, 2)
80
+ ```
81
+
82
+ ### Schema config
83
+
84
+ ```ruby
85
+ Schema = GraphQL::Schema.define do
86
+ query Query
87
+ mutation Mutation
88
+
89
+ # GraphQL Batch setup. Handle Promise objects.
90
+ lazy_resolve(Promise, :sync)
91
+ instrument(:query, GraphQL::Batch::Setup)
92
+
93
+ # FieldInstrumenter takes care of preloading assocations you've
94
+ # marked using the `preloads` attribute
95
+ instrument(:field, GraphQL::ActiveRecordBatcher::FieldInstrumenter.new)
96
+ end
97
+ ```
98
+
99
+ ### TODO
100
+
101
+ - [ ] Expose a way to batch finds
102
+ - [ ] Expose a way or documentation on how to batch the `node` field
103
+ - [ ] Accept an array of preloads
104
+ - [ ] Accept nested preloads
105
+
106
+ ## License
107
+
108
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/_test_.db ADDED
Binary file
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "graphql/active_record_batcher"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'graphql/active_record_batcher/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "graphql-active_record_batcher"
8
+ spec.version = GraphQL::ActiveRecordBatcher::VERSION
9
+ spec.authors = ["Marc-Andre Giroux"]
10
+ spec.email = ["mgiroux0@gmail.com"]
11
+
12
+ spec.summary = %q{Association Preloading and Query Batching for GraphQL}
13
+ spec.description = %q{Association Preloading and Query Batching for GraphQL}
14
+ spec.homepage = "http://mgiroux.me"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest", "~> 5.10.1"
25
+ spec.add_development_dependency "minitest-focus", "~> 1.1"
26
+ spec.add_development_dependency "minitest-reporters", "~>1.0"
27
+ spec.add_development_dependency "activerecord"
28
+ spec.add_development_dependency "sqlite3"
29
+ spec.add_development_dependency "byebug"
30
+
31
+ spec.add_runtime_dependency "activerecord"
32
+ spec.add_runtime_dependency "graphql"
33
+ spec.add_runtime_dependency "graphql-batch"
34
+ spec.add_runtime_dependency "promise.rb"
35
+ end
@@ -0,0 +1,11 @@
1
+ require 'graphql/active_record_batcher/version'
2
+ require 'graphql/active_record_batcher/field_instrumenter'
3
+ require 'graphql/active_record_batcher/association_loader'
4
+
5
+ module GraphQL
6
+ module ActiveRecordBatcher
7
+ end
8
+ end
9
+
10
+ GraphQL::Field.accepts_definitions(preloads: GraphQL::Define.assign_metadata_key(:preloads))
11
+ GraphQL::ObjectType.accepts_definitions(model: GraphQL::Define.assign_metadata_key(:model))
@@ -0,0 +1,41 @@
1
+ require 'promise.rb'
2
+ require 'graphql/batch'
3
+
4
+ module GraphQL
5
+ module ActiveRecordBatcher
6
+ class AssociationLoader < GraphQL::Batch::Loader
7
+ def initialize(model, association)
8
+ @model = model
9
+ @association = association
10
+ end
11
+
12
+ def load(record)
13
+ raise TypeError, "#{@model} loader can't load association for #{record.class}" unless record.is_a?(@model)
14
+ return Promise.resolve(read_association(record)) if association_loaded?(record)
15
+ super
16
+ end
17
+
18
+ # We want to load the associations on all records, even if they have the same id
19
+ def cache_key(record)
20
+ record.object_id
21
+ end
22
+
23
+ def perform(records)
24
+ ::ActiveRecord::Associations::Preloader.new.preload(records, association)
25
+
26
+ records.each do |record|
27
+ association_result = record.public_send(association)
28
+ fulfill(record, association_result)
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ attr_reader :model, :association
35
+
36
+ def association_loaded?(record)
37
+ record.association(@association).loaded?
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,46 @@
1
+ module GraphQL
2
+ module ActiveRecordBatcher
3
+ class FieldInstrumenter
4
+ def instrument(type, field)
5
+ association_to_preload = field.metadata[:preloads]
6
+
7
+ if association_to_preload
8
+ # Model is needed to preload an association
9
+ unless model = type.metadata[:model]
10
+ message = "No ActiveRecord Model set on type #{type.name}'s metadata."\
11
+ " Use `model(YourActiveRecordModel)` inside the type's definition"
12
+ raise StandardError, message
13
+ end
14
+
15
+ # Make sure the association exists on the model
16
+ validate_preload(model, association_to_preload)
17
+
18
+ loader = GraphQL::ActiveRecordBatcher::AssociationLoader.new(
19
+ model,
20
+ association_to_preload
21
+ )
22
+
23
+ # "Wrap" the resolve proc with our own, which returns a promise
24
+ old_resolve_proc = field.resolve_proc
25
+ new_resolve_proc = ->(obj, args, ctx) do
26
+ loader.load(obj).then { old_resolve_proc.call(obj, args, ctx) }
27
+ end
28
+
29
+ field.redefine do
30
+ resolve(new_resolve_proc)
31
+ end
32
+ else
33
+ field
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def validate_preload(model, association_to_preload)
40
+ unless model.reflect_on_association(association_to_preload)
41
+ raise ArgumentError, "No association `#{association_to_preload}` on model `#{model}`"
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,5 @@
1
+ module GraphQL
2
+ module ActiveRecordBatcher
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,227 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphql-active_record_batcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marc-Andre Giroux
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 5.10.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 5.10.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-focus
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest-reporters
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activerecord
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: byebug
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: activerecord
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: graphql
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: graphql-batch
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: promise.rb
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :runtime
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ description: Association Preloading and Query Batching for GraphQL
182
+ email:
183
+ - mgiroux0@gmail.com
184
+ executables: []
185
+ extensions: []
186
+ extra_rdoc_files: []
187
+ files:
188
+ - ".gitignore"
189
+ - CODE_OF_CONDUCT.md
190
+ - Gemfile
191
+ - Gemfile.lock
192
+ - LICENSE.txt
193
+ - README.md
194
+ - Rakefile
195
+ - _test_.db
196
+ - bin/console
197
+ - bin/setup
198
+ - graphql-active_record_batcher.gemspec
199
+ - lib/graphql/active_record_batcher.rb
200
+ - lib/graphql/active_record_batcher/association_loader.rb
201
+ - lib/graphql/active_record_batcher/field_instrumenter.rb
202
+ - lib/graphql/active_record_batcher/version.rb
203
+ homepage: http://mgiroux.me
204
+ licenses:
205
+ - MIT
206
+ metadata: {}
207
+ post_install_message:
208
+ rdoc_options: []
209
+ require_paths:
210
+ - lib
211
+ required_ruby_version: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - ">="
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ required_rubygems_version: !ruby/object:Gem::Requirement
217
+ requirements:
218
+ - - ">="
219
+ - !ruby/object:Gem::Version
220
+ version: '0'
221
+ requirements: []
222
+ rubyforge_project:
223
+ rubygems_version: 2.6.10
224
+ signing_key:
225
+ specification_version: 4
226
+ summary: Association Preloading and Query Batching for GraphQL
227
+ test_files: []