graphql-extras 0.3.0 → 0.4.1

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: 29213c20e07ca49b31f25ee4c66a1b60537c746ac3a66030eee023eb33d35f65
4
- data.tar.gz: 45694df9fb4e00159e7394bcab9f9253de8dd949b891cc6b78a6e28fa548a99e
3
+ metadata.gz: 98e38072429fdb32cbd085ce64d1ac8fa5e295989580b8f2cab3008ef232a7bb
4
+ data.tar.gz: c7fa03896dd5f5122dff890f5c2e97fcf2d02450a3efd269e14582de4b402a6b
5
5
  SHA512:
6
- metadata.gz: 22ca80f894bdae011ee6707f35a43097b699534dfc4295e5369100f993dced3a6906df29518dff0729683320db7dd72d9186c0edc8d0378ffb2b5bee24941e0d
7
- data.tar.gz: e6df88738196ecf8b02b15fcad0da085ee6cfca9762a27dc06942458797b73a624fa300252338e3f04c92aff73a52a59543eeee29bc5f9ee2631c27563223423
6
+ metadata.gz: 962ff3bb31aaa27d4bac5028fa6dd2400e2330d51d0b2172abd3e7627bbcb206517bc962bc25fcb6687f5660f36403d91e7a0bb20ca0e13087c26ae968cda3e2
7
+ data.tar.gz: 263a13b184ff89f7f28f60d7b74ab0dc1a454fefb33f27f2ef6fe2e6dca0c2134f4ba828b65e9409f30a80ef3c2fdd692033dac42c333044afac73004dd87be4
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`.
@@ -10,10 +10,10 @@ Gem::Specification.new do |spec|
10
10
 
11
11
  spec.summary = %q{Utiltities for building GraphQL APIs.}
12
12
  spec.description = %q{A set of modules and types for buildign GraphQL APIs.}
13
- spec.homepage = "https://github.com/promptworks/graphql-extras"
13
+ spec.homepage = "https://github.com/rzane/graphql-extras"
14
14
 
15
15
  spec.metadata["homepage_uri"] = spec.homepage
16
- spec.metadata["source_code_uri"] = "https://github.com/promptworks/graphql-extras"
16
+ spec.metadata["source_code_uri"] = "https://github.com/rzane/graphql-extras"
17
17
 
18
18
  # Specify which files should be added to the gem when it is released.
19
19
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -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,6 +1,6 @@
1
1
  require "securerandom"
2
2
  require "active_support/inflector"
3
- require "active_support/core_ext/hash/deep_transform_values"
3
+ require "active_support/core_ext/hash"
4
4
  require "graphql/extras/test/loader"
5
5
  require "graphql/extras/test/response"
6
6
 
@@ -33,6 +33,10 @@ module GraphQL
33
33
  def __execute(schema, query, variables)
34
34
  uploads = {}
35
35
 
36
+ variables = variables.deep_transform_keys do |key|
37
+ key.to_s.camelize(:lower)
38
+ end
39
+
36
40
  variables = variables.deep_transform_values do |value|
37
41
  if value.respond_to? :tempfile
38
42
  id = SecureRandom.uuid
@@ -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 parsed by the `Decimal` library. The Decimal appears in a JSON
38
- response as a string to preserve precision.
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)
@@ -1,5 +1,5 @@
1
1
  module GraphQL
2
2
  module Extras
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.1"
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.0
4
+ version: 0.4.1
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-05 00:00:00.000000000 Z
11
+ date: 2021-08-13 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
@@ -177,11 +162,11 @@ files:
177
162
  - lib/graphql/extras/test/schema.rb
178
163
  - lib/graphql/extras/types.rb
179
164
  - lib/graphql/extras/version.rb
180
- homepage: https://github.com/promptworks/graphql-extras
165
+ homepage: https://github.com/rzane/graphql-extras
181
166
  licenses: []
182
167
  metadata:
183
- homepage_uri: https://github.com/promptworks/graphql-extras
184
- source_code_uri: https://github.com/promptworks/graphql-extras
168
+ homepage_uri: https://github.com/rzane/graphql-extras
169
+ source_code_uri: https://github.com/rzane/graphql-extras
185
170
  post_install_message:
186
171
  rdoc_options: []
187
172
  require_paths:
@@ -197,7 +182,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
197
182
  - !ruby/object:Gem::Version
198
183
  version: '0'
199
184
  requirements: []
200
- rubygems_version: 3.0.3
185
+ rubygems_version: 3.0.3.1
201
186
  signing_key:
202
187
  specification_version: 4
203
188
  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