graphql-extras 0.3.2 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb6a2324dfea95555ab147de0f4d79601603e21ca7dde008939795b12d40064c
4
- data.tar.gz: ef735334a0b5be7ac0286c45886aa60d507482b73a95c0af89a2d1b5dfae27cc
3
+ metadata.gz: 1ad1e3740a8a7ef06a6dad31a459153a121e254824225bf1a12c2718a59a699f
4
+ data.tar.gz: 36ce81c3f1f0888924668ccef5bde169958e45a97837cc669d94a416edc573de
5
5
  SHA512:
6
- metadata.gz: b412b43cd6d6c3865726770b1bd54d121e5a122fdafcd0f4f0e141edb6cd9e83ca13c47f73530025be3d16498144f97f3ddc40c02897049679bfebad8189ed56
7
- data.tar.gz: '018ccec04e7e67b563e460fcbe7d1649a097087846b488985c34072004bcd2839162e4a3f3131c76718ea99a1d15633bc5c9f7ec3190cef65de203eb6c533a4b'
6
+ metadata.gz: ba8b4d40badcfba328280f397f8fb7ab7f42ce5257478c0482e48c4f0abf533f4b170d51f507caf6378ed6239d51460b4fe0f4623a6cfb93e0b565bbbf81a80d
7
+ data.tar.gz: de3c6a7f726b0b9ca1311906c1c3d05c0896fe8ba1b64d9d059cc0b7c5d82c227ae7ec17611b999b666201b97818daea30d2f996fda5f9d9fefa849d00ec9fc2
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`.
@@ -26,7 +26,6 @@ Gem::Specification.new do |spec|
26
26
 
27
27
  spec.add_dependency "activesupport", ">= 5.2"
28
28
  spec.add_dependency "graphql", "~> 1.12"
29
- spec.add_dependency "graphql-batch", "~> 0.4"
30
29
 
31
30
  spec.add_development_dependency "bundler", "~> 2.0"
32
31
  spec.add_development_dependency "rake", "~> 13.0"
@@ -1,7 +1,6 @@
1
1
  require "graphql/extras/version"
2
2
  require "graphql/extras/types"
3
3
  require "graphql/extras/controller"
4
- require "graphql/extras/association_loader"
5
4
  require "graphql/extras/preload"
6
5
 
7
6
  module GraphQL
@@ -1,7 +1,17 @@
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
+ preloader = ActiveRecord::Associations::Preloader.new
10
+ preloader.preload(records, @preload)
11
+ records
12
+ end
13
+ end
14
+
5
15
  module Preload
6
16
  # @override
7
17
  def initialize(*args, preload: nil, **opts, &block)
@@ -10,13 +20,13 @@ module GraphQL
10
20
  end
11
21
 
12
22
  # @override
13
- def resolve(object, args, ctx)
14
- return super unless @preload
15
-
16
- loader = AssociationLoader.for(@preload)
17
- loader.load(object.object).then do
18
- super(object, args, ctx)
23
+ def resolve(object, args, context)
24
+ if @preload
25
+ loader = context.dataloader.with(PreloadSource, @preload)
26
+ loader.load(object.object)
19
27
  end
28
+
29
+ super
20
30
  end
21
31
  end
22
32
  end
@@ -1,5 +1,5 @@
1
1
  module GraphQL
2
2
  module Extras
3
- VERSION = "0.3.2"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
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.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ray Zane
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-19 00:00:00.000000000 Z
11
+ date: 2021-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.12'
41
- - !ruby/object:Gem::Dependency
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
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '0.4'
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: bundler
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -168,7 +154,6 @@ files:
168
154
  - bin/setup
169
155
  - graphql-extras.gemspec
170
156
  - lib/graphql/extras.rb
171
- - lib/graphql/extras/association_loader.rb
172
157
  - lib/graphql/extras/controller.rb
173
158
  - lib/graphql/extras/preload.rb
174
159
  - lib/graphql/extras/test.rb
@@ -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