graphql-sources 0.4.0 → 0.5.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: c950f4402666974cd24dd441f3b51caac7d337429518753e6886cecc9d9a46dd
4
- data.tar.gz: 88194279dc3f9ea1f3a1e47349cc5620b7cf550badbe6c0720fa35fcbf20a340
3
+ metadata.gz: e99e1d718952f3ee55b0a0b37c6f4f9f99789f4740243a6dd7995bee2b5fa6a4
4
+ data.tar.gz: 4f082d61109e02db43b06b2ff345dd2fdc697f8a6dc30336b7e6226e352a5e79
5
5
  SHA512:
6
- metadata.gz: 4f63d94f097e6c72665be2086ee3736ba11c87ceb41935afcfcefb1fbb4b64d9b0fe71f93eea66bdc0ddb8f4846e637b320e6f1117631d2a6522638869fc2df2
7
- data.tar.gz: 620fe9b524a7a0af4a67dbdfcc1f558f419977c25116830b3c46c42ed32f1fc86b1eba92c6a857c5ffc4d8814781cc989dca5b78519eefe6e04ef0b08f0c6b6a
6
+ metadata.gz: 58202b58608b54a184ed5831672ebafbd39b216cee19908139b4c9ee6e2874197ed5609fcb2bcd592074e101e77ddce8b1e250db5777c55427a37e7f19abc7be
7
+ data.tar.gz: ee4949a68c77cf7ea0b945f4f79d4a5ba6de9e491c64abb472f8091b01c8181512beafbaa16ca66bbecd8cc64b33c8891d2b3d2589659610ad0149c29dfdf6e0
data/README.md CHANGED
@@ -151,9 +151,23 @@ WHERE "likes"."post_id" IN (1, 2, 3, ...)
151
151
  GROUP BY "likes"."post_id"
152
152
  ```
153
153
 
154
+ ### Loading with `Rails.cache`
155
+
156
+ ```ruby
157
+ class UserType < GraphQL::Schema::Object
158
+ field :location, String, null: false
159
+
160
+ def location
161
+ dataloader
162
+ .with(GraphQL::Sources::RailsCache)
163
+ .load(key: "geocode:#{object.latest_ip}", fallback: -> { Geocode.for(object.latest_ip) })
164
+ end
165
+ end
166
+ ```
167
+
154
168
  ## Status
155
169
 
156
- [![CircleCI](https://circleci.com/gh/ksylvest/graphql-sources.svg?style=svg)](https://circleci.com/gh/ksylvest/rhino)
170
+ [![CircleCI](https://circleci.com/gh/ksylvest/graphql-sources.svg?style=svg)](https://circleci.com/gh/ksylvest/graphql-sources)
157
171
  [![Maintainability](https://api.codeclimate.com/v1/badges/bc301cb72712637e67dd/maintainability)](https://codeclimate.com/github/ksylvest/graphql-sources/maintainability)
158
172
  [![Test Coverage](https://api.codeclimate.com/v1/badges/bc301cb72712637e67dd/test_coverage)](https://codeclimate.com/github/ksylvest/graphql-sources/test_coverage)
159
173
 
@@ -31,6 +31,8 @@ module GraphQL
31
31
  # WHERE "comments"."user_id" IN (...)
32
32
  # ORDER BY "comments"."id"
33
33
  class ActiveRecordCollection < ActiveRecordBase
34
+ # @param keys [Array] an array of keys
35
+ # @return [Array] grouped records mirroring the keys
34
36
  def fetch(keys)
35
37
  models = models(keys: keys).order(:id).load_async
36
38
  dataloader.yield
@@ -31,6 +31,8 @@ module GraphQL
31
31
  # WHERE "likes"."post_id" IN (1, 2, 3, ...)
32
32
  # GROUP BY "likes"."post_id"
33
33
  class ActiveRecordCount < ActiveRecordBase
34
+ # @param keys [Array] an array of keys
35
+ # @return [Array] grouped counts for the keys
34
36
  def fetch(keys)
35
37
  map = models(keys: keys).group(@key).count
36
38
  keys.map { |key| map[key] || 0 }
@@ -31,6 +31,8 @@ module GraphQL
31
31
  # WHERE "profiles"."user_id" IN (1, 2, 3, ...)
32
32
  # ORDER BY "profiles"."id"
33
33
  class ActiveRecordObject < ActiveRecordBase
34
+ # @param keys [Array] an array of keys
35
+ # @return [Array] indexed records mirroring the keys
34
36
  def fetch(keys)
35
37
  models = models(keys: keys).order(:id).load_async
36
38
  dataloader.yield
@@ -28,6 +28,8 @@ module GraphQL
28
28
  # AND "active_storage_attachments"."record_type" = 'User'
29
29
  # AND "active_storage_attachments"."record_id" IN (...)
30
30
  class ActiveStorageHasManyAttached < ActiveStorageBase
31
+ # @param records [Array<ActiveRecord::Base>] an array of records
32
+ # @return [Array] grouped attachments mirroring the keys
31
33
  def fetch(records)
32
34
  attachments = attachments(records: records).load_async
33
35
  dataloader.yield
@@ -28,6 +28,8 @@ module GraphQL
28
28
  # AND "active_storage_attachments"."record_type" = 'User'
29
29
  # AND "active_storage_attachments"."record_id" IN (...)
30
30
  class ActiveStorageHasOneAttached < ActiveStorageBase
31
+ # @param records [Array<ActiveRecord::Base>] an array of records
32
+ # @return [Array] indexed attachments mirroring the keys
31
33
  def fetch(records)
32
34
  attachments = attachments(records: records).load_async
33
35
  dataloader.yield
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Sources
5
+ # A class for loading with Rails.cache.
6
+ #
7
+ # class UserType < GraphQL::Schema::Object
8
+ # field :location, String, null: false
9
+ #
10
+ # def location
11
+ # dataloader
12
+ # .with(GraphQL::Sources::RailsCache)
13
+ # .load(key: "geocode:#{object.latest_ip}", fallback: -> { Geocode.for(object.latest_ip) })
14
+ # end
15
+ # end
16
+ class RailsCache < GraphQL::Dataloader::Source
17
+ # @param operations [Array<Hash>] an array of key and fallback hashes
18
+ def fetch(operations)
19
+ keys = operations.pluck(:key)
20
+ fallbacks = operations.to_h { |operation| [operation[:key], operation[:fallback]] }
21
+ results = Rails.cache.fetch_multi(*keys) { |key| fallbacks[key].call }
22
+ keys.map { |key| results[key] }
23
+ end
24
+ end
25
+ end
26
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module GraphQL
4
4
  module Sources
5
- VERSION = '0.4.0'
5
+ VERSION = '0.5.0'
6
6
  end
7
7
  end
@@ -7,6 +7,7 @@ require_relative './sources/active_record_collection'
7
7
  require_relative './sources/active_record_object'
8
8
  require_relative './sources/active_storage_has_many_attached'
9
9
  require_relative './sources/active_storage_has_one_attached'
10
+ require_relative './sources/rails_cache'
10
11
 
11
12
  module GraphQL
12
13
  # A collection of common GraphQL dataloader classes.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-sources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Sylvestre
@@ -182,6 +182,7 @@ files:
182
182
  - lib/graphql/sources/active_storage_base.rb
183
183
  - lib/graphql/sources/active_storage_has_many_attached.rb
184
184
  - lib/graphql/sources/active_storage_has_one_attached.rb
185
+ - lib/graphql/sources/rails_cache.rb
185
186
  - lib/graphql/sources/version.rb
186
187
  homepage: https://github.com/ksylvest/graphql-sources
187
188
  licenses: