graphql-extras 0.3.2 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/build.yml +1 -1
- data/README.md +15 -11
- data/graphql-extras.gemspec +1 -2
- data/lib/graphql/extras/preload.rb +27 -8
- data/lib/graphql/extras/types.rb +2 -2
- data/lib/graphql/extras/version.rb +1 -1
- data/lib/graphql/extras.rb +0 -1
- metadata +10 -19
- data/lib/graphql/extras/association_loader.rb +0 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f3927449d4c9cea85828d5df7ea64954fa489c602ba39e35f938248962d65da
|
4
|
+
data.tar.gz: 3b9564bc9e6d32832e868c983826d007ad99d490c9316ecd8b20ca6eef4ec121
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac5e8a5230c0543d7897ef73ee6634ef44b932adf7af91d6ce1e4f5711955965967ebed75d8b3c23ce97af9e2c24915dc423be3b32aa9af9219f9cd590183acd
|
7
|
+
data.tar.gz: 973a08ada1e4e313299c0f8724d338e2b2b405374e7fb91f7791a3a1d2ead7cf2788564367f747aef563b8be7a2e8e258f2eba7cc18acbbed2484637892626b0
|
data/.github/workflows/build.yml
CHANGED
data/README.md
CHANGED
@@ -14,8 +14,8 @@ A collection of utilities for building GraphQL APIs.
|
|
14
14
|
- [Installation](#installation)
|
15
15
|
- [Usage](#usage)
|
16
16
|
- [GraphQL::Extras::Controller](#graphqlextrascontroller)
|
17
|
-
- [GraphQL::Extras::AssociationLoader](#graphqlextrasassociationloader)
|
18
17
|
- [GraphQL::Extras::Preload](#graphqlextraspreload)
|
18
|
+
- [GraphQL::Extras::PreloadSource](#graphqlextraspreloadsource)
|
19
19
|
- [GraphQL::Extras::Types](#graphqlextrastypes)
|
20
20
|
- [Date](#date)
|
21
21
|
- [DateTime](#datetime)
|
@@ -53,21 +53,15 @@ class GraphqlController < ApplicationController
|
|
53
53
|
end
|
54
54
|
```
|
55
55
|
|
56
|
-
### GraphQL::Extras::AssociationLoader
|
57
|
-
|
58
|
-
This is a subclass of [`GraphQL::Batch::Loader`](https://github.com/Shopify/graphql-batch) that performs eager loading of Active Record associations.
|
59
|
-
|
60
|
-
```ruby
|
61
|
-
loader = GraphQL::Extras::AssociationLoader.for(:blog)
|
62
|
-
loader.load(Post.first)
|
63
|
-
loader.load_many(Post.all)
|
64
|
-
```
|
65
|
-
|
66
56
|
### GraphQL::Extras::Preload
|
67
57
|
|
68
58
|
This allows you to preload associations before resolving fields.
|
69
59
|
|
70
60
|
```ruby
|
61
|
+
class Schema < GraphQL::Schema
|
62
|
+
use GraphQL::Dataloader
|
63
|
+
end
|
64
|
+
|
71
65
|
class BaseField < GraphQL::Schema::Field
|
72
66
|
prepend GraphQL::Extras::Preload
|
73
67
|
end
|
@@ -87,6 +81,16 @@ class PostType < BaseObject
|
|
87
81
|
end
|
88
82
|
```
|
89
83
|
|
84
|
+
### GraphQL::Extras::PreloadSource
|
85
|
+
|
86
|
+
This is a subclass of [`GraphQL::Dataloader::Source`](https://graphql-ruby.org/dataloader/overview.html) that performs eager loading of Active Record associations.
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
loader = dataloader.with(GraphQL::Extras::PreloadSource, :blog)
|
90
|
+
loader.load(Post.first)
|
91
|
+
loader.load_many(Post.all)
|
92
|
+
```
|
93
|
+
|
90
94
|
### GraphQL::Extras::Types
|
91
95
|
|
92
96
|
In your base classes, you should include the `GraphQL::Extras::Types`.
|
data/graphql-extras.gemspec
CHANGED
@@ -25,8 +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", "
|
29
|
-
spec.add_dependency "graphql-batch", "~> 0.4"
|
28
|
+
spec.add_dependency "graphql", [">= 1.12", "< 3"]
|
30
29
|
|
31
30
|
spec.add_development_dependency "bundler", "~> 2.0"
|
32
31
|
spec.add_development_dependency "rake", "~> 13.0"
|
@@ -1,7 +1,26 @@
|
|
1
|
-
require "graphql/extras/association_loader"
|
2
|
-
|
3
1
|
module GraphQL
|
4
2
|
module Extras
|
3
|
+
class PreloadSource < GraphQL::Dataloader::Source
|
4
|
+
def initialize(preload)
|
5
|
+
@preload = preload
|
6
|
+
end
|
7
|
+
|
8
|
+
def fetch(records)
|
9
|
+
if ActiveRecord::VERSION::MAJOR >= 7
|
10
|
+
preloader = ActiveRecord::Associations::Preloader.new(
|
11
|
+
records: records,
|
12
|
+
associations: @preload
|
13
|
+
)
|
14
|
+
preloader.call
|
15
|
+
else
|
16
|
+
preloader = ActiveRecord::Associations::Preloader.new
|
17
|
+
preloader.preload(records, @preload)
|
18
|
+
end
|
19
|
+
|
20
|
+
records
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
5
24
|
module Preload
|
6
25
|
# @override
|
7
26
|
def initialize(*args, preload: nil, **opts, &block)
|
@@ -10,13 +29,13 @@ module GraphQL
|
|
10
29
|
end
|
11
30
|
|
12
31
|
# @override
|
13
|
-
def resolve(object, args,
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
loader.load(object.object).then do
|
18
|
-
super(object, args, ctx)
|
32
|
+
def resolve(object, args, context)
|
33
|
+
if @preload
|
34
|
+
loader = context.dataloader.with(PreloadSource, @preload)
|
35
|
+
loader.load(object.object)
|
19
36
|
end
|
37
|
+
|
38
|
+
super
|
20
39
|
end
|
21
40
|
end
|
22
41
|
end
|
data/lib/graphql/extras/types.rb
CHANGED
@@ -34,8 +34,8 @@ module GraphQL
|
|
34
34
|
class Decimal < GraphQL::Schema::Scalar
|
35
35
|
description <<~DESC
|
36
36
|
The `Decimal` scalar type represents signed double-precision fractional
|
37
|
-
values
|
38
|
-
|
37
|
+
values. The Decimal appears in a JSON response as a string to preserve
|
38
|
+
precision.
|
39
39
|
DESC
|
40
40
|
|
41
41
|
def self.coerce_input(value, _context)
|
data/lib/graphql/extras.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphql-extras
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ray Zane
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -28,30 +28,22 @@ dependencies:
|
|
28
28
|
name: graphql
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '1.12'
|
34
|
+
- - "<"
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '3'
|
34
37
|
type: :runtime
|
35
38
|
prerelease: false
|
36
39
|
version_requirements: !ruby/object:Gem::Requirement
|
37
40
|
requirements:
|
38
|
-
- - "
|
41
|
+
- - ">="
|
39
42
|
- !ruby/object:Gem::Version
|
40
43
|
version: '1.12'
|
41
|
-
-
|
42
|
-
name: graphql-batch
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0.4'
|
48
|
-
type: :runtime
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
44
|
+
- - "<"
|
53
45
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
46
|
+
version: '3'
|
55
47
|
- !ruby/object:Gem::Dependency
|
56
48
|
name: bundler
|
57
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -168,7 +160,6 @@ files:
|
|
168
160
|
- bin/setup
|
169
161
|
- graphql-extras.gemspec
|
170
162
|
- lib/graphql/extras.rb
|
171
|
-
- lib/graphql/extras/association_loader.rb
|
172
163
|
- lib/graphql/extras/controller.rb
|
173
164
|
- lib/graphql/extras/preload.rb
|
174
165
|
- lib/graphql/extras/test.rb
|
@@ -197,7 +188,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
197
188
|
- !ruby/object:Gem::Version
|
198
189
|
version: '0'
|
199
190
|
requirements: []
|
200
|
-
rubygems_version: 3.0.3
|
191
|
+
rubygems_version: 3.0.3.1
|
201
192
|
signing_key:
|
202
193
|
specification_version: 4
|
203
194
|
summary: Utiltities for building GraphQL APIs.
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require "graphql/batch"
|
2
|
-
|
3
|
-
module GraphQL
|
4
|
-
module Extras
|
5
|
-
class AssociationLoader < GraphQL::Batch::Loader
|
6
|
-
def initialize(preload)
|
7
|
-
@preload = preload
|
8
|
-
end
|
9
|
-
|
10
|
-
def cache_key(record)
|
11
|
-
record.object_id
|
12
|
-
end
|
13
|
-
|
14
|
-
def perform(records)
|
15
|
-
preloader = ActiveRecord::Associations::Preloader.new
|
16
|
-
preloader.preload(records, @preload)
|
17
|
-
|
18
|
-
records.each do |record|
|
19
|
-
fulfill(record, nil) unless fulfilled?(record)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|