graphql-activerecord 0.5.6 → 0.6.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: daaabd69e2bf2900bf933c0925ac6b2372f2bd02
4
- data.tar.gz: ccba786159faaad1cd884bf2ee72da23d47ad45f
3
+ metadata.gz: 863565c48d4d2defa03c704b85a502cdadea56d2
4
+ data.tar.gz: 2e0b68206bce268981dd11d65ca1d520041ed74d
5
5
  SHA512:
6
- metadata.gz: 1a27c8559b95c44529659e156c99f39ef8e3e28a4816a9a05413a1ff19965a910277d257e52c371137a64214d6280bef1e2aa2993dd83024309c69d6a64b3a47
7
- data.tar.gz: 0600db6e7d05dcf42f4e6e6b3fafd6a327296616a2e368a57c44499b053a4c005198acb5487da807c442294921b63460474e6c1f07016b4820185c5172d9e268
6
+ metadata.gz: 4b6ac2a65fb4c4e57f8c58c5a4a5f0039d7c7f767d58e8b6cac35cc178009a8aaa459777c55f9d48d13facc6526750dadb43703241b9f9609e66ae12d430d1e4
7
+ data.tar.gz: 280d8bc46fe2119be8597f241be32c3b35eb68725681b032bc6dcccbc8673eeb9ddbc2a997babc8d9bf15398aab1cfd0694e8f817de2d69eee7cf4e48526013a
@@ -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
- camel_name = options[:name] || association.to_s.camelize(:lower).to_sym
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)
@@ -1,5 +1,5 @@
1
1
  module GraphQL
2
2
  module Models
3
- VERSION = "0.5.6"
3
+ VERSION = "0.6.0"
4
4
  end
5
5
  end
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.5.6
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-19 00:00:00.000000000 Z
11
+ date: 2016-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql