graphql-preload 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/graphql/preload/instrument.rb +3 -4
- data/lib/graphql/preload/loader.rb +19 -12
- data/lib/graphql/preload/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6bbbde895d17e5914f30802362a2453cc4f3de89
|
4
|
+
data.tar.gz: b11f2782b0a1cb286d867e83fff3da24c1650c5c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2b6f8a34abdaf6ce6624d38cd230d49864dde3e2b3a93561019b23562d6870819605da476125512c70d621933af1019f0c508bee872baaf518308820b4854a4a
|
7
|
+
data.tar.gz: a89501116230eca584019915d5e66b2c4ef019ed70c12151cc7a474587e1189f3f6b584a8c739ef5887b0869be357524afc3525883e4689435654dec642ae11f
|
data/README.md
CHANGED
@@ -40,8 +40,8 @@ Call `preload` when defining your field:
|
|
40
40
|
# Post.includes(:comments, :authors)
|
41
41
|
preload [:comments, :authors]
|
42
42
|
|
43
|
-
# Post.includes(:comments, authors: [:
|
44
|
-
preload [:comments, { authors: [:
|
43
|
+
# Post.includes(:comments, authors: [:followers, :posts])
|
44
|
+
preload [:comments, { authors: [:followers, :posts] }]
|
45
45
|
|
46
46
|
resolve ->(obj, args, ctx) { obj.comments }
|
47
47
|
end
|
@@ -33,17 +33,17 @@ module GraphQL
|
|
33
33
|
promises << preload(record, sub_association)
|
34
34
|
end
|
35
35
|
when Hash
|
36
|
-
association.each do |sub_association,
|
36
|
+
association.each do |sub_association, nested_association|
|
37
37
|
promises << preload_single_association(record, sub_association).then do
|
38
38
|
associated_records = record.public_send(sub_association)
|
39
39
|
|
40
40
|
case associated_records
|
41
41
|
when ActiveRecord::Base
|
42
|
-
preload(associated_records,
|
42
|
+
preload(associated_records, nested_association)
|
43
43
|
else
|
44
44
|
Promise.all(
|
45
45
|
Array.wrap(associated_records).map do |associated_record|
|
46
|
-
preload(associated_record,
|
46
|
+
preload(associated_record, nested_association)
|
47
47
|
end
|
48
48
|
)
|
49
49
|
end
|
@@ -56,7 +56,6 @@ module GraphQL
|
|
56
56
|
end
|
57
57
|
|
58
58
|
private def preload_single_association(record, association)
|
59
|
-
return Promise.resolve(record) if record.association(association).loaded?
|
60
59
|
GraphQL::Preload::Loader.for(record.class, association).load(record)
|
61
60
|
end
|
62
61
|
end
|
@@ -4,6 +4,10 @@ module GraphQL
|
|
4
4
|
class Loader < GraphQL::Batch::Loader
|
5
5
|
attr_reader :association, :model
|
6
6
|
|
7
|
+
def cache_key(record)
|
8
|
+
record.object_id
|
9
|
+
end
|
10
|
+
|
7
11
|
def initialize(model, association)
|
8
12
|
@association = association
|
9
13
|
@model = model
|
@@ -13,38 +17,41 @@ module GraphQL
|
|
13
17
|
|
14
18
|
def load(record)
|
15
19
|
unless record.is_a?(model)
|
16
|
-
raise TypeError, "
|
20
|
+
raise TypeError, "Loader for #{model} can't load associations for #{record.class} objects"
|
17
21
|
end
|
18
22
|
|
19
|
-
|
20
|
-
|
21
|
-
else
|
22
|
-
super
|
23
|
-
end
|
23
|
+
return Promise.resolve(record) if association_loaded?(record)
|
24
|
+
super
|
24
25
|
end
|
25
26
|
|
26
27
|
def perform(records)
|
28
|
+
preload_association(records)
|
29
|
+
records.each { |record| fulfill(record, record) }
|
30
|
+
end
|
31
|
+
|
32
|
+
private def association_loaded?(record)
|
33
|
+
record.association(association).loaded?
|
34
|
+
end
|
35
|
+
|
36
|
+
private def preload_association(records)
|
27
37
|
if ActiveRecord::VERSION::MAJOR > 3
|
28
38
|
ActiveRecord::Associations::Preloader.new.preload(records, association)
|
29
39
|
else
|
30
40
|
ActiveRecord::Associations::Preloader.new(records, association).run
|
31
41
|
end
|
32
|
-
|
33
|
-
records.each { |record| fulfill(record, record) }
|
34
42
|
end
|
35
43
|
|
36
44
|
private def validate_association
|
37
45
|
unless association.is_a?(Symbol)
|
38
|
-
raise ArgumentError, '
|
46
|
+
raise ArgumentError, 'Association must be a Symbol object'
|
39
47
|
end
|
40
48
|
|
41
49
|
unless model < ActiveRecord::Base
|
42
|
-
raise ArgumentError, '
|
50
|
+
raise ArgumentError, 'Model must be an ActiveRecord::Base descendant'
|
43
51
|
end
|
44
52
|
|
45
53
|
return if model.reflect_on_association(association)
|
46
|
-
|
47
|
-
raise TypeError, "association :#{association} does not exist on #{model.name}"
|
54
|
+
raise TypeError, "Association :#{association} does not exist on #{model}"
|
48
55
|
end
|
49
56
|
end
|
50
57
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphql-preload
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Foster, Etienne Tripier
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-08-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|