hq-graphql 2.0.10 → 2.0.11

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: fbfd2d5dad56008b05618a89354c42ced0ddac3ee44d292b96773303488171d8
4
- data.tar.gz: 862fee899d0964af9177327b73ad7befcc200985f4f6323272ebb9d9122f5a5a
3
+ metadata.gz: 40c69615fc4b2b0d14d3c510d5c4973aa5a0f7bf0285278ca98505af62d73fb0
4
+ data.tar.gz: 4f0926421e6e2f8658f9e6aaf0234756eaf66c6dbf126210f56a92f0d4701a8a
5
5
  SHA512:
6
- metadata.gz: d58e0f041362550a8aea5d3a0f03ca1ba0be02ad034b8c3def9d807f3c7045913c900ee79f3058bdf2d9396388d22a236b3ff6e368fb9d4758fcde60abb92ae5
7
- data.tar.gz: 305b4e50bbb1f025aa2a977e45eba2aa63091b322396ac0bcfb2ce3be3f9ba28dd166097fe594982bc705dc701220d4b5b924721eee1ed65bc18387a3c2529ac
6
+ metadata.gz: 945bad36e906b28a2c1444f7e87df594c306b817c61f3bdfa1176f061c645904e9206f663fe82861b0a44ee61c881d8899d6979dc57842b1920070d6a48fa6ff
7
+ data.tar.gz: 82cf817c3d801273c0cc4a099aa513585ba96220fc9b9ce84f996bd14cbe04d248a8d759d864fc964d7f07571bb11169c59cf670d7893ce1ea860a4d41dd86d8
data/lib/hq/graphql.rb CHANGED
@@ -59,12 +59,12 @@ module HQ
59
59
  end
60
60
 
61
61
  require "hq/graphql/active_record_extensions"
62
+ require "hq/graphql/association_loader"
62
63
  require "hq/graphql/scalars"
63
64
  require "hq/graphql/comparator"
64
65
  require "hq/graphql/enum"
65
66
  require "hq/graphql/inputs"
66
67
  require "hq/graphql/input_object"
67
- require "hq/graphql/loaders"
68
68
  require "hq/graphql/mutation"
69
69
  require "hq/graphql/object"
70
70
  require "hq/graphql/resource"
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HQ
4
+ module GraphQL
5
+ class AssociationLoader < ::GraphQL::Batch::Loader
6
+ def initialize(model, association_name)
7
+ @model = model
8
+ @association_name = association_name
9
+ validate
10
+ end
11
+
12
+ def load(record)
13
+ raise TypeError, "#{@model} loader can't load association for #{record.class}" unless record.is_a?(@model)
14
+ return Promise.resolve(read_association(record)) if association_loaded?(record)
15
+ super
16
+ end
17
+
18
+ # We want to load the associations on all records, even if they have the same id
19
+ def cache_key(record)
20
+ record.object_id
21
+ end
22
+
23
+ def perform(records)
24
+ preload_association(records)
25
+ records.each { |record| fulfill(record, read_association(record)) }
26
+ end
27
+
28
+ private
29
+
30
+ def validate
31
+ unless @model.reflect_on_association(@association_name)
32
+ raise ArgumentError, "No association #{@association_name} on #{@model}"
33
+ end
34
+ end
35
+
36
+ def preload_association(records)
37
+ ::ActiveRecord::Associations::Preloader.new.preload(records, @association_name)
38
+ end
39
+
40
+ def read_association(record)
41
+ record.public_send(@association_name)
42
+ end
43
+
44
+ def association_loaded?(record)
45
+ record.association(@association_name).loaded?
46
+ end
47
+ end
48
+ end
49
+ end
@@ -20,7 +20,7 @@ module HQ
20
20
 
21
21
  def resolve_field(object, args, ctx)
22
22
  if klass.present? && !!::GraphQL::Batch::Executor.current && object.object
23
- Loaders::Association.for(klass.constantize, original_name).load(object.object).then do
23
+ AssociationLoader.for(klass.constantize, original_name).load(object.object).then do
24
24
  super
25
25
  end
26
26
  else
@@ -2,6 +2,6 @@
2
2
 
3
3
  module HQ
4
4
  module GraphQL
5
- VERSION = "2.0.10"
5
+ VERSION = "2.0.11"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hq-graphql
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.10
4
+ version: 2.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Jones
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-13 00:00:00.000000000 Z
11
+ date: 2020-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -247,6 +247,7 @@ files:
247
247
  - lib/hq-graphql.rb
248
248
  - lib/hq/graphql.rb
249
249
  - lib/hq/graphql/active_record_extensions.rb
250
+ - lib/hq/graphql/association_loader.rb
250
251
  - lib/hq/graphql/comparator.rb
251
252
  - lib/hq/graphql/config.rb
252
253
  - lib/hq/graphql/engine.rb
@@ -254,8 +255,6 @@ files:
254
255
  - lib/hq/graphql/field.rb
255
256
  - lib/hq/graphql/input_object.rb
256
257
  - lib/hq/graphql/inputs.rb
257
- - lib/hq/graphql/loaders.rb
258
- - lib/hq/graphql/loaders/association.rb
259
258
  - lib/hq/graphql/mutation.rb
260
259
  - lib/hq/graphql/object.rb
261
260
  - lib/hq/graphql/resource.rb
@@ -1,3 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "hq/graphql/loaders/association"
@@ -1,51 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module HQ
4
- module GraphQL
5
- module Loaders
6
- class Association < ::GraphQL::Batch::Loader
7
- def initialize(model, association_name)
8
- @model = model
9
- @association_name = association_name
10
- validate
11
- end
12
-
13
- def load(record)
14
- raise TypeError, "#{@model} loader can't load association for #{record.class}" unless record.is_a?(@model)
15
- return Promise.resolve(read_association(record)) if association_loaded?(record)
16
- super
17
- end
18
-
19
- # We want to load the associations on all records, even if they have the same id
20
- def cache_key(record)
21
- record.object_id
22
- end
23
-
24
- def perform(records)
25
- preload_association(records)
26
- records.each { |record| fulfill(record, read_association(record)) }
27
- end
28
-
29
- private
30
-
31
- def validate
32
- unless @model.reflect_on_association(@association_name)
33
- raise ArgumentError, "No association #{@association_name} on #{@model}"
34
- end
35
- end
36
-
37
- def preload_association(records)
38
- ::ActiveRecord::Associations::Preloader.new.preload(records, @association_name)
39
- end
40
-
41
- def read_association(record)
42
- record.public_send(@association_name)
43
- end
44
-
45
- def association_loaded?(record)
46
- record.association(@association_name).loaded?
47
- end
48
- end
49
- end
50
- end
51
- end