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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 798b3acf2885ab4505348ce82a903491dcf69f9bb938f6d14f6123f7e15e7828
4
- data.tar.gz: f09ba6716261367885bb2d59a3cfc5d8795d37ad815f8f97b6531bbd4b9c650a
3
+ metadata.gz: 79aec0f7de63c3a1efa653093be5a38b2138e6887019f1cf2c7412117c423c51
4
+ data.tar.gz: 2e8bad4be442f7b922cec0583eea4ee4afd1976f3298823457b29720bb55d826
5
5
  SHA512:
6
- metadata.gz: 9f10d3c82db22907b693d047e64f3e635eb178aa3397ff2ff3a905f19a16fa3e9ea4994497ecd33f113a814aacbbac1145c621096d8e3cc8dd682bf662b4ccdd
7
- data.tar.gz: c6d548c8fcc787c40b1825bfdaa270d7e41a0d5c9077c29be9ce4720b715175a4a048e227874f5f9a4b3fcc27ef57f8a57d5ce59d8a47037e0f730b64a6412cb
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`.
@@ -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
@@ -1,6 +1,7 @@
1
1
  require "graphql/extras/version"
2
2
  require "graphql/extras/types"
3
3
  require "graphql/extras/controller"
4
+ require "graphql/extras/batch"
4
5
 
5
6
  module GraphQL
6
7
  module Extras
@@ -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
@@ -1,5 +1,5 @@
1
1
  module GraphQL
2
2
  module Extras
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  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.1.1
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