graphql-preload 1.0.1
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 +7 -0
- data/.gitignore +9 -0
- data/.ruby-version +1 -0
- data/.yardopts +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +56 -0
- data/Rakefile +10 -0
- data/bin/console +7 -0
- data/bin/setup +8 -0
- data/graphql-preload.gemspec +34 -0
- data/lib/graphql/preload.rb +25 -0
- data/lib/graphql/preload/instrument.rb +64 -0
- data/lib/graphql/preload/loader.rb +51 -0
- data/lib/graphql/preload/version.rb +5 -0
- metadata +196 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 412060a07e4a7640f360fd987b130eff199b5abb
|
4
|
+
data.tar.gz: 7ee60048595cd21c4f3a3b75e7eb60acba2c2633
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c4d07b403576d3ac174cf54d864ffc28e0549735625f43addc80ce4f6e7a4b32d98078d29c34acd3ddbbb45c8b46123ba6594680782a7786c8a3ab33b47de28d
|
7
|
+
data.tar.gz: cabf3cb48477c1f66ece36283817fd6af855e1eeb8305cdb685471f60ddd15be25627e43e91f0794da163a4a3146c48ed099287a33dc98f6424e3078ca80d22c
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.5
|
data/.yardopts
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Grand Rounds
|
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,56 @@
|
|
1
|
+
# Graphql::Preload
|
2
|
+
|
3
|
+
Provides a DSL for the [`graphql` gem](https://github.com/rmosolgo/graphql-ruby) that allows ActiveRecord associations to be preloaded in field definitions. Based on a [gist](https://gist.github.com/theorygeek/a1a59a2bf9c59e4b3706ac68d12c8434) by @theorygeek.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'graphql-preload'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install graphql-preload
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
First, enable preloading in your `GraphQL::Schema`:
|
24
|
+
|
25
|
+
Schema = GraphQL::Schema.define do
|
26
|
+
use GraphQL::Batch
|
27
|
+
|
28
|
+
enable_preloading
|
29
|
+
end
|
30
|
+
|
31
|
+
Call `preload` when defining your field:
|
32
|
+
|
33
|
+
PostType = GraphQL::ObjectType.define do
|
34
|
+
name 'Post'
|
35
|
+
|
36
|
+
field :comments, !types[!CommentType] do
|
37
|
+
# Post.includes(:comments)
|
38
|
+
preload :comments
|
39
|
+
|
40
|
+
resolve ->(obj, args, ctx) { obj.comments }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
## Development
|
45
|
+
|
46
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
47
|
+
|
48
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ConsultingMD/graphql-preload.
|
53
|
+
|
54
|
+
## License
|
55
|
+
|
56
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'graphql/preload/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'graphql-preload'
|
9
|
+
spec.version = GraphQL::Preload::VERSION
|
10
|
+
spec.authors = ['Ryan Foster, Etienne Tripier']
|
11
|
+
spec.email = ['etienne.tripier@grandrounds.com']
|
12
|
+
|
13
|
+
spec.summary = 'Preload ActiveRecord associations with graphql-batch'
|
14
|
+
spec.homepage = 'https://github.com/ConsultingMD/graphql-preload'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = 'exe'
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_runtime_dependency 'activerecord', '>= 3.2', '<= 5'
|
25
|
+
spec.add_runtime_dependency 'graphql', '>= 1.5', '< 2'
|
26
|
+
spec.add_runtime_dependency 'graphql-batch', '~> 0.3'
|
27
|
+
spec.add_runtime_dependency 'promise.rb', '~> 0.7'
|
28
|
+
|
29
|
+
spec.add_development_dependency 'bundler', '~> 1.15'
|
30
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
31
|
+
spec.add_development_dependency 'pry', '~> 0.10'
|
32
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
33
|
+
spec.add_development_dependency 'yard', '~> 0.9'
|
34
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'graphql'
|
2
|
+
require 'graphql/batch'
|
3
|
+
require 'promise.rb'
|
4
|
+
|
5
|
+
GraphQL::Field.accepts_definitions(
|
6
|
+
preload: ->(type, *args) do
|
7
|
+
type.metadata[:preload] ||= []
|
8
|
+
type.metadata[:preload].concat(args)
|
9
|
+
end
|
10
|
+
)
|
11
|
+
|
12
|
+
GraphQL::Schema.accepts_definitions(
|
13
|
+
enable_preloading: ->(schema) do
|
14
|
+
schema.instrument(:field, GraphQL::Preload::Instrument.new)
|
15
|
+
end
|
16
|
+
)
|
17
|
+
|
18
|
+
module GraphQL
|
19
|
+
# Provides a GraphQL::Field definition to preload ActiveRecord::Associations
|
20
|
+
module Preload
|
21
|
+
autoload :Instrument, 'graphql/preload/instrument'
|
22
|
+
autoload :Loader, 'graphql/preload/loader'
|
23
|
+
autoload :VERSION, 'graphql/preload/version'
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module GraphQL
|
2
|
+
module Preload
|
3
|
+
# Provides an instrument for the GraphQL::Field :preload definition
|
4
|
+
class Instrument
|
5
|
+
def instrument(_type, field)
|
6
|
+
return field unless field.metadata.include?(:preload)
|
7
|
+
|
8
|
+
old_resolver = field.resolve_proc
|
9
|
+
new_resolver = ->(obj, args, ctx) do
|
10
|
+
return old_resolver.call(obj, args, ctx) unless obj
|
11
|
+
|
12
|
+
preload(obj, field.metadata[:preload]).then do
|
13
|
+
old_resolver.call(obj, args, ctx)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
field.redefine do
|
18
|
+
resolve(new_resolver)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private def preload(record, associations)
|
23
|
+
return preload_single_association(record, associations) if associations.is_a?(Symbol)
|
24
|
+
|
25
|
+
promises = []
|
26
|
+
|
27
|
+
Array.wrap(associations).each do |association|
|
28
|
+
case association
|
29
|
+
when Symbol
|
30
|
+
promises << preload_single_association(record, association)
|
31
|
+
when Array
|
32
|
+
association.each do |sub_association|
|
33
|
+
promises << preload(record, sub_association)
|
34
|
+
end
|
35
|
+
when Hash
|
36
|
+
association.each do |sub_association, property|
|
37
|
+
promises << preload_single_association(record, sub_association).then do
|
38
|
+
associated_records = record.public_send(sub_association)
|
39
|
+
|
40
|
+
case associated_records
|
41
|
+
when ActiveRecord::Base
|
42
|
+
preload(associated_records, property)
|
43
|
+
else
|
44
|
+
Promise.all(
|
45
|
+
Array.wrap(associated_records).map do |associated_record|
|
46
|
+
preload(associated_record, property)
|
47
|
+
end
|
48
|
+
)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
Promise.all(promises)
|
56
|
+
end
|
57
|
+
|
58
|
+
private def preload_single_association(record, association)
|
59
|
+
return Promise.resolve(record) if record.association(association).loaded?
|
60
|
+
GraphQL::Preload::Loader.for(record.class, association).load(record)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module GraphQL
|
2
|
+
module Preload
|
3
|
+
# Preloads ActiveRecord::Associations when called from the Preload::Instrument
|
4
|
+
class Loader < GraphQL::Batch::Loader
|
5
|
+
attr_reader :association, :model
|
6
|
+
|
7
|
+
def initialize(model, association)
|
8
|
+
@association = association
|
9
|
+
@model = model
|
10
|
+
|
11
|
+
validate_association
|
12
|
+
end
|
13
|
+
|
14
|
+
def load(record)
|
15
|
+
unless record.is_a?(model)
|
16
|
+
raise TypeError, "loader for #{model.name} can't load associations for #{record.class.name} objects"
|
17
|
+
end
|
18
|
+
|
19
|
+
if record.association(association).loaded?
|
20
|
+
Promise.resolve(record)
|
21
|
+
else
|
22
|
+
super
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def perform(records)
|
27
|
+
if ActiveRecord::VERSION::MAJOR > 3
|
28
|
+
ActiveRecord::Associations::Preloader.new.preload(records, association)
|
29
|
+
else
|
30
|
+
ActiveRecord::Associations::Preloader.new(records, association).run
|
31
|
+
end
|
32
|
+
|
33
|
+
records.each { |record| fulfill(record, record) }
|
34
|
+
end
|
35
|
+
|
36
|
+
private def validate_association
|
37
|
+
unless association.is_a?(Symbol)
|
38
|
+
raise ArgumentError, 'association must be a Symbol object'
|
39
|
+
end
|
40
|
+
|
41
|
+
unless model < ActiveRecord::Base
|
42
|
+
raise ArgumentError, 'model must be an ActiveRecord::Base descendant'
|
43
|
+
end
|
44
|
+
|
45
|
+
return if model.reflect_on_association(association)
|
46
|
+
|
47
|
+
raise TypeError, "association :#{association} does not exist on #{model.name}"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,196 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: graphql-preload
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Foster, Etienne Tripier
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-07-13 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: '3.2'
|
20
|
+
- - "<="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.2'
|
30
|
+
- - "<="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5'
|
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.5'
|
40
|
+
- - "<"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '2'
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '1.5'
|
50
|
+
- - "<"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '2'
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: graphql-batch
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0.3'
|
60
|
+
type: :runtime
|
61
|
+
prerelease: false
|
62
|
+
version_requirements: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - "~>"
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0.3'
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
name: promise.rb
|
69
|
+
requirement: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - "~>"
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0.7'
|
74
|
+
type: :runtime
|
75
|
+
prerelease: false
|
76
|
+
version_requirements: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - "~>"
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0.7'
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: bundler
|
83
|
+
requirement: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - "~>"
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '1.15'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - "~>"
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '1.15'
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
name: minitest
|
97
|
+
requirement: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - "~>"
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '5.0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - "~>"
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '5.0'
|
109
|
+
- !ruby/object:Gem::Dependency
|
110
|
+
name: pry
|
111
|
+
requirement: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - "~>"
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0.10'
|
116
|
+
type: :development
|
117
|
+
prerelease: false
|
118
|
+
version_requirements: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - "~>"
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0.10'
|
123
|
+
- !ruby/object:Gem::Dependency
|
124
|
+
name: rake
|
125
|
+
requirement: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - "~>"
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '10.0'
|
130
|
+
type: :development
|
131
|
+
prerelease: false
|
132
|
+
version_requirements: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - "~>"
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '10.0'
|
137
|
+
- !ruby/object:Gem::Dependency
|
138
|
+
name: yard
|
139
|
+
requirement: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - "~>"
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0.9'
|
144
|
+
type: :development
|
145
|
+
prerelease: false
|
146
|
+
version_requirements: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - "~>"
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0.9'
|
151
|
+
description:
|
152
|
+
email:
|
153
|
+
- etienne.tripier@grandrounds.com
|
154
|
+
executables: []
|
155
|
+
extensions: []
|
156
|
+
extra_rdoc_files: []
|
157
|
+
files:
|
158
|
+
- ".gitignore"
|
159
|
+
- ".ruby-version"
|
160
|
+
- ".yardopts"
|
161
|
+
- Gemfile
|
162
|
+
- LICENSE.txt
|
163
|
+
- README.md
|
164
|
+
- Rakefile
|
165
|
+
- bin/console
|
166
|
+
- bin/setup
|
167
|
+
- graphql-preload.gemspec
|
168
|
+
- lib/graphql/preload.rb
|
169
|
+
- lib/graphql/preload/instrument.rb
|
170
|
+
- lib/graphql/preload/loader.rb
|
171
|
+
- lib/graphql/preload/version.rb
|
172
|
+
homepage: https://github.com/ConsultingMD/graphql-preload
|
173
|
+
licenses:
|
174
|
+
- MIT
|
175
|
+
metadata: {}
|
176
|
+
post_install_message:
|
177
|
+
rdoc_options: []
|
178
|
+
require_paths:
|
179
|
+
- lib
|
180
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
181
|
+
requirements:
|
182
|
+
- - ">="
|
183
|
+
- !ruby/object:Gem::Version
|
184
|
+
version: '0'
|
185
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
186
|
+
requirements:
|
187
|
+
- - ">="
|
188
|
+
- !ruby/object:Gem::Version
|
189
|
+
version: '0'
|
190
|
+
requirements: []
|
191
|
+
rubyforge_project:
|
192
|
+
rubygems_version: 2.6.12
|
193
|
+
signing_key:
|
194
|
+
specification_version: 4
|
195
|
+
summary: Preload ActiveRecord associations with graphql-batch
|
196
|
+
test_files: []
|