graphql_includable 0.1.5 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/graphql_includable.rb +25 -25
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 78dc263783852ec3090c807ab090253f08608de1
|
4
|
+
data.tar.gz: 46aec07790da54100a3787a6c45df8064c90beda
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c7ee1838d8167126aecad34b00eaf338ec7515812a2290d724a5f1f7e8eb65c5bcbef139eb0a09a59837938b9cc35030ef239258d584867f95a316d84c3e3255
|
7
|
+
data.tar.gz: fb5d17e8a29ecc47d8f819098573e25dfdd82250906273d105f2ae984303941ead7b0f97cf18b99057a11faea779901659b733c567fa018e22a0f3926ca67168
|
data/lib/graphql_includable.rb
CHANGED
@@ -1,15 +1,16 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'graphql'
|
2
|
+
require 'active_support/concern'
|
3
3
|
|
4
|
-
GraphQL::Field.accepts_definitions
|
4
|
+
GraphQL::Field.accepts_definitions(
|
5
|
+
includes: GraphQL::Define.assign_metadata_key(:includes)
|
6
|
+
)
|
5
7
|
|
6
8
|
module GraphQLIncludable
|
7
9
|
extend ActiveSupport::Concern
|
8
10
|
|
9
11
|
module ClassMethods
|
10
|
-
def includes_from_graphql(
|
11
|
-
generated_includes = GraphQLIncludable.generate_includes_from_graphql(
|
12
|
-
puts "final gen: #{generated_includes}"
|
12
|
+
def includes_from_graphql(ctx)
|
13
|
+
generated_includes = GraphQLIncludable.generate_includes_from_graphql(ctx, model_name.to_s)
|
13
14
|
includes(generated_includes)
|
14
15
|
end
|
15
16
|
|
@@ -26,11 +27,9 @@ module GraphQLIncludable
|
|
26
27
|
end
|
27
28
|
end
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
matching_node = GraphQLIncludable.find_child_returning_model_name(query_context.irep_node, model_name)
|
33
|
-
GraphQLIncludable.includes_from_graphql_field(matching_node)
|
30
|
+
def self.generate_includes_from_graphql(ctx, model_name)
|
31
|
+
matching_node = find_child_returning_model_name(ctx.irep_node, model_name)
|
32
|
+
includes_from_graphql_field(matching_node)
|
34
33
|
end
|
35
34
|
|
36
35
|
def self.find_child_returning_model_name(node, model_name)
|
@@ -39,7 +38,7 @@ module GraphQLIncludable
|
|
39
38
|
if return_type.to_s == model_name
|
40
39
|
matching_node = node
|
41
40
|
elsif node.respond_to? :scoped_children
|
42
|
-
node.scoped_children[return_type].each do |
|
41
|
+
node.scoped_children[return_type].each do |_child_name, child_node|
|
43
42
|
matching_node = find_child_returning_model_name(child_node, model_name)
|
44
43
|
break if matching_node
|
45
44
|
end
|
@@ -51,18 +50,16 @@ module GraphQLIncludable
|
|
51
50
|
includes = []
|
52
51
|
nested_includes = {}
|
53
52
|
|
54
|
-
return_type = node_return_type(node)
|
55
53
|
return_model = node_return_class(node)
|
56
|
-
return [] unless node &&
|
54
|
+
return [] unless node && return_model
|
57
55
|
|
58
|
-
node.scoped_children[
|
56
|
+
node.scoped_children[node_return_type(node)].each do |_child_name, child_node|
|
59
57
|
child_includes = includes_from_graphql_child(child_node, return_model)
|
60
58
|
if child_includes.is_a?(Hash)
|
61
59
|
nested_includes.merge!(child_includes)
|
62
|
-
|
60
|
+
else
|
61
|
+
child_includes = [child_includes] unless child_includes.is_a?(Array)
|
63
62
|
includes += child_includes
|
64
|
-
elsif child_includes
|
65
|
-
includes << child_includes
|
66
63
|
end
|
67
64
|
end
|
68
65
|
|
@@ -89,10 +86,12 @@ module GraphQLIncludable
|
|
89
86
|
end
|
90
87
|
|
91
88
|
def self.node_return_class(node)
|
89
|
+
# rubocop:disable Lint/HandleExceptions, Style/RedundantBegin
|
92
90
|
begin
|
93
91
|
Object.const_get(node_return_type(node).name)
|
94
92
|
rescue NameError
|
95
93
|
end
|
94
|
+
# rubocop:enable Lint/HandleExceptions, Style/RedundantBegin
|
96
95
|
end
|
97
96
|
|
98
97
|
def self.node_returns_active_record?(node)
|
@@ -110,7 +109,7 @@ module GraphQLIncludable
|
|
110
109
|
end
|
111
110
|
end
|
112
111
|
|
113
|
-
#
|
112
|
+
# get unwrapped return type from a field, stripping ListType / NonNullType wrappers
|
114
113
|
def self.node_return_type(node)
|
115
114
|
type = node.return_type
|
116
115
|
type = type.of_type while type.respond_to? :of_type
|
@@ -125,13 +124,14 @@ module GraphQLIncludable
|
|
125
124
|
# get a 1d array of the chain of delegated model names,
|
126
125
|
# so if model A delegates method B to model C, which delegates method B to model D,
|
127
126
|
# delegated_includes_chain(A, :B) => [:C, :D]
|
128
|
-
def self.delegated_includes_chain(
|
127
|
+
def self.delegated_includes_chain(base_model, method_name)
|
129
128
|
chain = []
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
129
|
+
method = method_name.to_sym
|
130
|
+
model_name = base_model.instance_variable_get('@delegate_cache').try(:[], method)
|
131
|
+
while model_name
|
132
|
+
chain << model_name
|
133
|
+
model = model_name_to_class(model_name)
|
134
|
+
model_name = model.instance_variable_get('@delegate_cache').try(:[], method)
|
135
135
|
end
|
136
136
|
chain
|
137
137
|
end
|