graphql-eager_loader 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +63 -0
- data/Rakefile +37 -0
- data/lib/graphql/eager_loader.rb +9 -0
- data/lib/graphql/eager_loader/engine.rb +13 -0
- data/lib/graphql/eager_loader/lookahead_loader.rb +60 -0
- data/lib/graphql/eager_loader/version.rb +7 -0
- data/lib/tasks/graphql/eager_loader_tasks.rake +6 -0
- metadata +79 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b4310412bb795a5c7f8361aea24e5354a1b5b776738f9e94047f64c8f54aa008
|
4
|
+
data.tar.gz: 06673a81f83d39de76f9a61760c1d53c1984ab77f23be2e2f33c97aed7cff40e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 80fc6c5f93c1731537004f33c9eaa39b1e2d807042ff54d5dfb4be6b1639f43e4d1a8885b4ff93db5e64cbd774735521dd3ec8324a8b7132bf710a23acec5731
|
7
|
+
data.tar.gz: 4483b2f95611ad2ecdc26d042e9226a6e42deef850cdc395c0056bdfa211c8e741c57011e9aff19d10bd8432a1870e250d34c6ad3e3b7f16004b9759e0ff3f48
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2019 Viktor Fonic
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Graphql::EagerLoader
|
2
|
+
|
3
|
+
Eager-load your Active Record associations automagically.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Call this method like this:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
Graphql::EagerLoader::LookaheadLoader.call(Project, lookahead)
|
11
|
+
```
|
12
|
+
|
13
|
+
Here's a full example with 'graphql' field:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
field :projects, Types::ProjectType, null: true, extras: [:lookahead]
|
17
|
+
def projects(lookahead:)
|
18
|
+
Graphql::EagerLoader::LookaheadLoader.call(Project, lookahead).all # or: .where(published: true)
|
19
|
+
end
|
20
|
+
```
|
21
|
+
|
22
|
+
This method allows you to add additional functionality while eager loading app's models.
|
23
|
+
For example you can do stuff like (notice the location of `.all` below):
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
field :projects, Types::ProjectType, null: true, extras: [:lookahead]
|
27
|
+
def projects(lookahead:)
|
28
|
+
Graphql::EagerLoader::LookaheadLoader.call(Project, lookahead) do |eager_loaded_model, associations|
|
29
|
+
associations.values.each do |association|
|
30
|
+
if association.instance_of?(ActiveRecord::Reflection::HasManyReflection)
|
31
|
+
eager_loaded_model = eager_loaded_model.merge(association.klass.where(published: true))
|
32
|
+
end
|
33
|
+
end
|
34
|
+
eager_loaded_model
|
35
|
+
end.all
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
## Installation
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
gem 'graphql-eager_loader'
|
43
|
+
```
|
44
|
+
|
45
|
+
And then execute:
|
46
|
+
|
47
|
+
```bash
|
48
|
+
$ bundle install
|
49
|
+
```
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
Contributions welcome!
|
54
|
+
|
55
|
+
## Special Thanks
|
56
|
+
|
57
|
+
Thank you to [@colinjfw](https://github.com/colinjfw) for the great [GraphQL-Api](https://github.com/colinjfw/graphql-api) gem that inspired this.
|
58
|
+
|
59
|
+
Thank you to [@rmosolgo](https://github.com/rmosolgo) for the incredible [GraphQL](https://github.com/rmosolgo/graphql-ruby) gem.
|
60
|
+
|
61
|
+
## License
|
62
|
+
|
63
|
+
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,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'bundler/setup'
|
5
|
+
rescue LoadError
|
6
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'rdoc/task'
|
10
|
+
|
11
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
12
|
+
rdoc.rdoc_dir = 'rdoc'
|
13
|
+
rdoc.title = 'Graphql::EagerLoader'
|
14
|
+
rdoc.options << '--line-numbers'
|
15
|
+
rdoc.rdoc_files.include('README.md')
|
16
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
17
|
+
end
|
18
|
+
|
19
|
+
APP_RAKEFILE = File.expand_path('spec/dummy/Rakefile', __dir__)
|
20
|
+
load 'rails/tasks/engine.rake'
|
21
|
+
|
22
|
+
load 'rails/tasks/statistics.rake'
|
23
|
+
|
24
|
+
require 'bundler/gem_tasks'
|
25
|
+
|
26
|
+
require 'rake/testtask'
|
27
|
+
|
28
|
+
Rake::TestTask.new(:test) do |t|
|
29
|
+
t.libs << 'lib'
|
30
|
+
t.libs << 'spec'
|
31
|
+
t.pattern = 'spec/**/*_spec.rb'
|
32
|
+
t.verbose = false
|
33
|
+
end
|
34
|
+
|
35
|
+
task default: :test
|
36
|
+
|
37
|
+
require 'stylecheck/rake_tasks'
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/engine'
|
4
|
+
require 'action_dispatch/railtie'
|
5
|
+
|
6
|
+
module Graphql
|
7
|
+
module EagerLoader
|
8
|
+
class Engine < ::Rails::Engine
|
9
|
+
engine_name 'graphql-eager_loader'
|
10
|
+
isolate_namespace Graphql::EagerLoader
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Graphql::EagerLoader
|
4
|
+
class LookaheadLoader
|
5
|
+
# Call this method like this:
|
6
|
+
# Graphql::EagerLoader::LookaheadLoader.call(Project, lookahead)
|
7
|
+
#
|
8
|
+
# Here's a full example with 'graphql' field:
|
9
|
+
#
|
10
|
+
# field :projects, Types::ProjectType, null: true, extras: [:lookahead]
|
11
|
+
# def projects(lookahead:)
|
12
|
+
# Graphql::EagerLoader::LookaheadLoader.call(Project, lookahead).all # or: .where(published: true)
|
13
|
+
# end
|
14
|
+
def self.call(model_class, lookahead, &block)
|
15
|
+
associations = self.associations(model_class, lookahead)
|
16
|
+
eager_load_associations(model_class, associations, &block)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.associations(model_class, lookahead)
|
20
|
+
self.selections(lookahead).map(&:name).map(&:to_s).map do |selection|
|
21
|
+
model_class.reflections.find do |name, _association|
|
22
|
+
name == selection
|
23
|
+
end
|
24
|
+
end.compact.to_h
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.selections(lookahead)
|
28
|
+
selection_names = lookahead.selections.map(&:name)
|
29
|
+
if selection_names == %i[edges nodes] || selection_names == [:nodes]
|
30
|
+
lookahead.selections.find { |lookahead| lookahead.name == :nodes }.selections
|
31
|
+
else
|
32
|
+
lookahead.selections
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# This method allows you to add additional functionality while eager loading app's models.
|
37
|
+
# For example you can do stuff like (notice the location of `.all` below):
|
38
|
+
#
|
39
|
+
# field :projects, Types::ProjectType, null: true, extras: [:lookahead]
|
40
|
+
# def projects(lookahead:)
|
41
|
+
# Graphql::EagerLoader::LookaheadLoader.call(Project, lookahead) do |eager_loaded_model, associations|
|
42
|
+
# associations.values.each do |association|
|
43
|
+
# if association.instance_of?(ActiveRecord::Reflection::HasManyReflection)
|
44
|
+
# eager_loaded_model = eager_loaded_model.merge(association.klass.where(published: true))
|
45
|
+
# end
|
46
|
+
# end
|
47
|
+
# eager_loaded_model
|
48
|
+
# end.all
|
49
|
+
# end
|
50
|
+
def self.eager_load_associations(model_class, associations, &block)
|
51
|
+
return model_class if associations.blank?
|
52
|
+
|
53
|
+
eager_loaded_model = model_class.eager_load(*associations.keys)
|
54
|
+
|
55
|
+
eager_loaded_model = yield eager_loaded_model, associations if block_given?
|
56
|
+
|
57
|
+
eager_loaded_model
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: graphql-eager_loader
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Viktor Fonic
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-09-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- viktor.fonic@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- lib/graphql/eager_loader.rb
|
52
|
+
- lib/graphql/eager_loader/engine.rb
|
53
|
+
- lib/graphql/eager_loader/lookahead_loader.rb
|
54
|
+
- lib/graphql/eager_loader/version.rb
|
55
|
+
- lib/tasks/graphql/eager_loader_tasks.rake
|
56
|
+
homepage: https://github.com/vfonic/graphql-eager_loader
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubygems_version: 3.0.4
|
76
|
+
signing_key:
|
77
|
+
specification_version: 4
|
78
|
+
summary: Eager load Active Record relationships in your GraphQL queries
|
79
|
+
test_files: []
|