graphql-activerecord 0.5.6 → 0.6.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
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 863565c48d4d2defa03c704b85a502cdadea56d2
|
4
|
+
data.tar.gz: 2e0b68206bce268981dd11d65ca1d520041ed74d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4b6ac2a65fb4c4e57f8c58c5a4a5f0039d7c7f767d58e8b6cac35cc178009a8aaa459777c55f9d48d13facc6526750dadb43703241b9f9609e66ae12d430d1e4
|
7
|
+
data.tar.gz: 280d8bc46fe2119be8597f241be32c3b35eb68725681b032bc6dcccbc8673eeb9ddbc2a997babc8d9bf15398aab1cfd0694e8f817de2d69eee7cf4e48526013a
|
data/lib/graphql/activerecord.rb
CHANGED
@@ -33,7 +33,7 @@ require 'graphql/models/mutator'
|
|
33
33
|
module GraphQL
|
34
34
|
module Models
|
35
35
|
class << self
|
36
|
-
attr_accessor :node_interface_proc, :model_from_id, :authorize
|
36
|
+
attr_accessor :node_interface_proc, :model_from_id, :authorize, :id_for_model
|
37
37
|
end
|
38
38
|
|
39
39
|
# Returns a promise that will traverse the associations and resolve to the model at the end of the path.
|
@@ -53,7 +53,11 @@ module GraphQL
|
|
53
53
|
fail ArgumentError.new("Association #{association} wasn't found on model #{model_type.name}") unless reflection
|
54
54
|
fail ArgumentError.new("Cannot include #{reflection.macro} association #{association} on model #{model_type.name} with has_one") unless [:has_one, :belongs_to].include?(reflection.macro)
|
55
55
|
|
56
|
-
|
56
|
+
# Define the field for the association itself
|
57
|
+
|
58
|
+
camel_name = options[:name] || association.to_s.camelize(:lower)
|
59
|
+
camel_name = camel_name.to_sym if camel_name.is_a?(String)
|
60
|
+
|
57
61
|
type_lambda = resolve_has_one_type(reflection)
|
58
62
|
|
59
63
|
DefinitionHelpers.register_field_metadata(graph_type, camel_name, {
|
@@ -77,6 +81,46 @@ module GraphQL
|
|
77
81
|
DefinitionHelpers.load_and_traverse(model, [association], context)
|
78
82
|
end
|
79
83
|
end
|
84
|
+
|
85
|
+
# Define the field for the associated model's ID
|
86
|
+
id_field_name = :"#{camel_name}Id"
|
87
|
+
|
88
|
+
DefinitionHelpers.register_field_metadata(graph_type, id_field_name, {
|
89
|
+
macro: :has_one,
|
90
|
+
macro_type: :association,
|
91
|
+
path: path,
|
92
|
+
association: association,
|
93
|
+
base_model_type: base_model_type,
|
94
|
+
model_type: model_type,
|
95
|
+
object_to_base_model: object_to_model
|
96
|
+
})
|
97
|
+
|
98
|
+
can_use_optimized = reflection.macro == :belongs_to
|
99
|
+
|
100
|
+
if !reflection.polymorphic? && reflection.klass.column_names.include?('type')
|
101
|
+
can_use_optimized = false
|
102
|
+
end
|
103
|
+
|
104
|
+
graph_type.fields[id_field_name.to_s] = GraphQL::Field.define do
|
105
|
+
name id_field_name.to_s
|
106
|
+
type types.ID
|
107
|
+
deprecation_reason options[:deprecation_reason] if options.include?(:deprecation_reason)
|
108
|
+
|
109
|
+
resolve -> (model, args, context) do
|
110
|
+
return nil unless model
|
111
|
+
|
112
|
+
if can_use_optimized
|
113
|
+
id = model.public_send(reflection.foreign_key)
|
114
|
+
return nil if id.nil?
|
115
|
+
|
116
|
+
type = model.association(association).klass.name
|
117
|
+
GraphQL::Models.id_for_model.call(type, id)
|
118
|
+
else
|
119
|
+
# We have to actually load the model and then get it's ID
|
120
|
+
DefinitionHelpers.load_and_traverse(model, [association], context).then(&:gid)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
80
124
|
end
|
81
125
|
|
82
126
|
def self.define_has_many_array(graph_type, base_model_type, model_type, path, association, object_to_model, options)
|
@@ -111,6 +155,32 @@ module GraphQL
|
|
111
155
|
end
|
112
156
|
end
|
113
157
|
end
|
158
|
+
|
159
|
+
# Define the field for the associated model's ID
|
160
|
+
id_field_name = :"#{camel_name.to_s.singularize}Ids"
|
161
|
+
|
162
|
+
DefinitionHelpers.register_field_metadata(graph_type, id_field_name, {
|
163
|
+
macro: :has_one,
|
164
|
+
macro_type: :association,
|
165
|
+
path: path,
|
166
|
+
association: association,
|
167
|
+
base_model_type: base_model_type,
|
168
|
+
model_type: model_type,
|
169
|
+
object_to_base_model: object_to_model
|
170
|
+
})
|
171
|
+
|
172
|
+
graph_type.fields[id_field_name.to_s] = GraphQL::Field.define do
|
173
|
+
name id_field_name.to_s
|
174
|
+
type types[!types.ID]
|
175
|
+
deprecation_reason options[:deprecation_reason] if options.include?(:deprecation_reason)
|
176
|
+
|
177
|
+
resolve -> (model, args, context) do
|
178
|
+
return nil unless model
|
179
|
+
DefinitionHelpers.load_and_traverse(model, [association], context).then do |result|
|
180
|
+
Array.wrap(result).map(&:gid)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
114
184
|
end
|
115
185
|
|
116
186
|
def self.define_has_many_connection(graph_type, base_model_type, model_type, path, association, object_to_model, options)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphql-activerecord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Foster
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: graphql
|