graphql-extras 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +28 -0
- data/graphql-extras.gemspec +3 -0
- data/lib/graphql/extras.rb +1 -0
- data/lib/graphql/extras/batch.rb +47 -0
- data/lib/graphql/extras/version.rb +1 -1
- metadata +44 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 79aec0f7de63c3a1efa653093be5a38b2138e6887019f1cf2c7412117c423c51
|
4
|
+
data.tar.gz: 2e8bad4be442f7b922cec0583eea4ee4afd1976f3298823457b29720bb55d826
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5bd25a61e755ccdd182f786706ef5190e55979310f5c2cd602adbfa6d8b389e20a2c42e2e0e5c6838d592f9e957f55498a6a271404299e1e33112b1ec44e979d
|
7
|
+
data.tar.gz: 5e3d5b584e670997f1a430b631eff44abdd5fcb289cac51d3945ae58fc9440eaffadf3048ab5a180de94974b0560e83f3227faa223d594d309881d5ca52ec342
|
data/README.md
CHANGED
@@ -30,6 +30,34 @@ class GraphqlController < ApplicationController
|
|
30
30
|
end
|
31
31
|
```
|
32
32
|
|
33
|
+
### GraphQL::Extras::Batch::AssociationLoader
|
34
|
+
|
35
|
+
This is a subclass of [`GraphQL::Batch::Loader`](https://github.com/Shopify/graphql-batch) that performs eager loading of Active Record associations.
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
loader = GraphQL::Extras::Batch::AssociationLoader.for(:blog)
|
39
|
+
loader.load(Post.first)
|
40
|
+
loader.load_many(Post.all)
|
41
|
+
```
|
42
|
+
|
43
|
+
### GraphQL::Extras::Batch::Resolvers
|
44
|
+
|
45
|
+
This includes a set of convenience methods for query batching.
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
class Post < GraphQL::Schema::Object
|
49
|
+
include GraphQL::Extras::Batch::Resolver
|
50
|
+
|
51
|
+
field :blog, BlogType, resolve: association(:blog), null: false
|
52
|
+
field :comments, [CommentType], resolve: association(:comments, preload: { comments: :user }), null: false
|
53
|
+
field :blog_title, String, null: false
|
54
|
+
|
55
|
+
def blog_title
|
56
|
+
association(object, :blog).then(&:title)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
33
61
|
### GraphQL::Extras::Types
|
34
62
|
|
35
63
|
In your base classes, you should include the `GraphQL::Extras::Types`.
|
data/graphql-extras.gemspec
CHANGED
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.require_paths = ["lib"]
|
26
26
|
|
27
27
|
spec.add_dependency "activesupport", "~> 5.2"
|
28
|
+
spec.add_dependency "graphql-batch", "~> 0.4"
|
28
29
|
|
29
30
|
spec.add_development_dependency "bundler", "~> 2.0"
|
30
31
|
spec.add_development_dependency "rake", "~> 10.0"
|
@@ -32,4 +33,6 @@ Gem::Specification.new do |spec|
|
|
32
33
|
spec.add_development_dependency "graphql", "~> 1.9"
|
33
34
|
spec.add_development_dependency "rspec-rails", "~> 3.8"
|
34
35
|
spec.add_development_dependency "actionpack", "~> 5.2"
|
36
|
+
spec.add_development_dependency "activerecord", "~> 5.2"
|
37
|
+
spec.add_development_dependency "sqlite3", "~> 1.4"
|
35
38
|
end
|
data/lib/graphql/extras.rb
CHANGED
@@ -0,0 +1,47 @@
|
|
1
|
+
require "graphql/batch"
|
2
|
+
|
3
|
+
module GraphQL
|
4
|
+
module Extras
|
5
|
+
module Batch
|
6
|
+
class AssociationLoader < GraphQL::Batch::Loader
|
7
|
+
def initialize(name, preload: name)
|
8
|
+
@name = name
|
9
|
+
@preload = preload
|
10
|
+
end
|
11
|
+
|
12
|
+
def cache_key(record)
|
13
|
+
record.object_id
|
14
|
+
end
|
15
|
+
|
16
|
+
def perform(records)
|
17
|
+
preloader = ActiveRecord::Associations::Preloader.new
|
18
|
+
preloader.preload(records, @preload)
|
19
|
+
|
20
|
+
records.each do |record|
|
21
|
+
fulfill(record, record.public_send(@name))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module Resolvers
|
27
|
+
def self.included(base)
|
28
|
+
base.extend ClassMethods
|
29
|
+
end
|
30
|
+
|
31
|
+
module ClassMethods
|
32
|
+
def association(name)
|
33
|
+
lambda do |record, _args, _ctx|
|
34
|
+
loader = AssociationLoader.for(name)
|
35
|
+
loader.load(record)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def association(record, name)
|
41
|
+
loader = AssociationLoader.for(name)
|
42
|
+
loader.load(record)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphql-extras
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ray Zane
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '5.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: graphql-batch
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.4'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.4'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -108,6 +122,34 @@ dependencies:
|
|
108
122
|
- - "~>"
|
109
123
|
- !ruby/object:Gem::Version
|
110
124
|
version: '5.2'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: activerecord
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '5.2'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '5.2'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: sqlite3
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '1.4'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '1.4'
|
111
153
|
description: A set of modules and types for buildign GraphQL APIs.
|
112
154
|
email:
|
113
155
|
- raymondzane@gmail.com
|
@@ -125,6 +167,7 @@ files:
|
|
125
167
|
- bin/setup
|
126
168
|
- graphql-extras.gemspec
|
127
169
|
- lib/graphql/extras.rb
|
170
|
+
- lib/graphql/extras/batch.rb
|
128
171
|
- lib/graphql/extras/controller.rb
|
129
172
|
- lib/graphql/extras/rspec.rb
|
130
173
|
- lib/graphql/extras/types.rb
|