graphiti_gql 0.2.31 → 0.2.32

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: ba3f305f68fc23193bb245eff566feb18eaec47a775c5f0fdab8177707b564f8
4
- data.tar.gz: 3f96b0cee52d9aa3c3968f125487d26f85f6bdb7acc13c2d457a2bc4feb65e11
3
+ metadata.gz: e8d0cda6314cfd3485283ae277bdaa56494a4b8eae9c62f9037b5b78f17b2825
4
+ data.tar.gz: 28fcbab36f65c81bb8a673aefaefe7bd54888f02ca741fcf5a889f2f858e094a
5
5
  SHA512:
6
- metadata.gz: c05e7c4dc7b7908a1b3f43b40cba6b5bcf6bbda4c2b26be4555783a4d27018e249e22d1e3feb8e1d0df09e35b62fc0f98759f4d0e9b16edc9725199dd79d19dc
7
- data.tar.gz: '049cb6c5201efa3a6921c5e964dbb7064dc70a7d5930c480bcf59b9466d839779a11c41a7d66cc7434d778ed200a94c0fc75a6736ff4b38817d453788cfc8318'
6
+ metadata.gz: 864e7b0fbd88fb01f3aa8bc0490ce144a4d62e02c9d9ce30dc032f6209b91cf9e5766b06bf85113cbdb9d4603dd397b8b831fcbc8f76ff123ff12307e82a4815
7
+ data.tar.gz: 61304ca30da99696a76c625c3737037429182df25ca6271c9f779bbbfa96740e2d1b3c792cbaaea560a31b52edc90f342b49a8aab12d607ece04804415e3d1e1
@@ -149,6 +149,7 @@ module GraphitiGql
149
149
 
150
150
  def value_object!
151
151
  config[:is_value_object] = true
152
+ self.graphql_entrypoint = false
152
153
  self.adapter = ::Graphiti::Adapters::Null
153
154
  config[:filters] = {}
154
155
  config[:stats] = {}
@@ -581,12 +582,6 @@ class Graphiti::ValueObjectAssociation
581
582
  @resource_class ||= Graphiti::Util::Class
582
583
  .infer_resource_class(@parent_resource_class, name)
583
584
  end
584
-
585
- def build_resource(parent)
586
- instance = resource_class.new
587
- instance.parent = parent
588
- instance
589
- end
590
585
  end
591
586
 
592
587
  module Graphiti
@@ -76,8 +76,14 @@ end|, :white, true)
76
76
  if response_errors
77
77
  Graphiti.info("❌🚨 Response contained errors!", :red, true)
78
78
  response_errors.each do |err|
79
- Graphiti.info("#{err['extensions']['code']} - #{err['message']}", :red, true)
80
- Graphiti.info("#{err['path'].join(".")}", :red, false) if err['path']
79
+ if err['extensions']
80
+ Graphiti.info("#{err['extensions']['code']} - #{err['message']}", :red, true)
81
+ else
82
+ Graphiti.info(err['message'], :red, true)
83
+ end
84
+ if err['path']
85
+ Graphiti.info("#{err['path'].join(".")}", :red, false) if err['path']
86
+ end
81
87
  end
82
88
  end
83
89
  else
@@ -38,7 +38,7 @@ module GraphitiGql
38
38
  end
39
39
 
40
40
  def add_value_objects
41
- registry.resource_types.each do |registered|
41
+ registry.resource_types(value_objects: false).each do |registered|
42
42
  resource, type = registered[:resource], registered[:type]
43
43
  ResourceType.add_value_objects(resource, type)
44
44
  end
@@ -50,10 +50,11 @@ module GraphitiGql
50
50
  end
51
51
 
52
52
  # When polymorphic parent, returns the Interface not the Class
53
- def resource_types
53
+ def resource_types(value_objects: true)
54
54
  values
55
55
  .select { |v| v.key?(:resource) }
56
- .reject { |v| v[:interface] || v[:resource].value_object? }
56
+ .reject { |v| v[:interface] }
57
+ .reject { |v| !value_objects && v[:resource].value_object? }
57
58
  .map { |registered| get(registered[:resource]) }
58
59
  end
59
60
 
@@ -6,8 +6,9 @@ module GraphitiGql
6
6
 
7
7
  definition_methods do
8
8
  # Optional: if this method is defined, it overrides `Schema.resolve_type`
9
- def resolve_type(object, context)
9
+ def resolve_type(object, _context)
10
10
  return object.type if object.is_a?(Loaders::FakeRecord)
11
+
11
12
  resource = object.instance_variable_get(:@__graphiti_resource)
12
13
  Registry.instance.get(resource.class)[:type]
13
14
  end
@@ -17,9 +18,8 @@ module GraphitiGql
17
18
  def self.add_fields(type, resource, id: true) # id: false for edges
18
19
  resource.attributes.each_pair do |name, config|
19
20
  next if name == :id && id == false
20
- if config[:readable]
21
- Fields::Attribute.new(resource, name, config).apply(type)
22
- end
21
+
22
+ Fields::Attribute.new(resource, name, config).apply(type) if config[:readable]
23
23
  end
24
24
  end
25
25
 
@@ -27,31 +27,31 @@ module GraphitiGql
27
27
  resource.config[:value_objects].each_pair do |name, vo_association|
28
28
  vo_resource_class = vo_association.resource_class
29
29
  value_object_type = Schema.registry.get(vo_resource_class)[:type]
30
- if vo_association.array?
31
- value_object_type = [value_object_type]
32
- end
30
+ value_object_type = [value_object_type] if vo_association.array?
33
31
 
34
32
  _array = vo_association.array?
35
33
  opts = { null: vo_association.null }
36
34
  opts[:deprecation_reason] = vo_association.deprecation_reason if vo_association.deprecation_reason
37
35
  type.field name, value_object_type, **opts
38
36
  type.define_method name do
39
- if (method_name = vo_association.readable)
40
- unless vo_association.parent_resource_class.new.send(method_name)
41
- raise ::Graphiti::Errors::UnreadableAttribute
42
- .new(vo_association.parent_resource_class, name)
43
- end
37
+ if (method_name = vo_association.readable) && !vo_association.parent_resource_class.new.send(method_name)
38
+ raise ::Graphiti::Errors::UnreadableAttribute
39
+ .new(vo_association.parent_resource_class, name)
44
40
  end
45
41
 
46
42
  result = vo_resource_class.all({ parent: object }).to_a
47
43
  default_behavior = result == [object]
48
- result = result.first if !_array
44
+ result = result.first unless _array
49
45
  if default_behavior
50
46
  method_name = vo_association.alias.presence || name
51
47
  result = object.send(method_name)
52
- if _array && !result.is_a?(Array)
53
- raise Graphiti::Errors::InvalidValueObject.new(resource, name, result)
54
- end
48
+ raise Graphiti::Errors::InvalidValueObject.new(resource, name, result) if _array && !result.is_a?(Array)
49
+ end
50
+ # For polymorphic value objects
51
+ if result.is_a?(Array)
52
+ result.each { |r| r.instance_variable_set(:@__parent, object) }
53
+ else
54
+ result.instance_variable_set(:@__parent, object) if result
55
55
  end
56
56
  result
57
57
  end
@@ -59,19 +59,19 @@ module GraphitiGql
59
59
  end
60
60
 
61
61
  def self.add_relationships(resource, type)
62
- resource.sideloads.each do |name, sideload|
62
+ resource.sideloads.each do |_name, sideload|
63
63
  next unless sideload.readable?
64
64
 
65
65
  registered_sl = if sideload.type == :polymorphic_belongs_to
66
- PolymorphicBelongsToInterface
67
- .new(resource, sideload)
68
- .build
69
- else
70
- Schema.registry.get(sideload.resource.class)
71
- end
66
+ PolymorphicBelongsToInterface
67
+ .new(resource, sideload)
68
+ .build
69
+ else
70
+ Schema.registry.get(sideload.resource.class)
71
+ end
72
72
  sideload_type = registered_sl[:type]
73
73
 
74
- if [:has_many, :many_to_many, :has_one].include?(sideload.type)
74
+ if %i[has_many many_to_many has_one].include?(sideload.type)
75
75
  Fields::ToMany.new(sideload, sideload_type).apply(type)
76
76
  else
77
77
  Fields::ToOne.new(sideload, sideload_type).apply(type)
@@ -86,6 +86,7 @@ module GraphitiGql
86
86
 
87
87
  def build
88
88
  return registry.get(@resource)[:type] if registry.get(@resource)
89
+
89
90
  type = build_base_type
90
91
  registry_name = registry.key_for(@resource, interface: poly_parent?)
91
92
  type.connection_type_class(build_connection_class)
@@ -108,6 +109,7 @@ module GraphitiGql
108
109
  # Define the actual class that implements the interface
109
110
  registry.set(@resource, type, interface: false)
110
111
  @resource.children.each do |child|
112
+ registry.get(child)
111
113
  if (registered = registry.get(child))
112
114
  registered[:type].implements(interface_type)
113
115
  else
@@ -128,12 +130,14 @@ module GraphitiGql
128
130
  klass.send(:include, BaseInterface)
129
131
  ctx = nil
130
132
  klass.definition_methods { ctx = self }
131
- ctx.define_method :resolve_type do |object, context|
132
- resource = object.instance_variable_get(:@__graphiti_resource)
133
- registry_name = Registry.instance.key_for(resource.class)
134
- if resource.polymorphic?
135
- resource = resource.class.resource_for_model(object)
136
- registry_name = Registry.instance.key_for(resource)
133
+ _resource = @resource
134
+ ctx.define_method :resolve_type do |object, _context|
135
+ registry_name = Registry.instance.key_for(_resource)
136
+ if _resource.polymorphic?
137
+ parent = object.instance_variable_get(:@__parent)
138
+ # pass parent for polymorphic value objects
139
+ _resource = _resource.resource_for_model(parent || object)
140
+ registry_name = Registry.instance.key_for(_resource)
137
141
  end
138
142
  Registry.instance[registry_name][:type]
139
143
  end
@@ -141,8 +145,15 @@ module GraphitiGql
141
145
  klass = Class.new(Schema.base_object)
142
146
  end
143
147
 
148
+ if @resource.value_object?
149
+ klass.define_method :parent do
150
+ object.instance_variable_get(:@__parent)
151
+ end
152
+ end
153
+
144
154
  klass.define_method :resource do
145
155
  return @resource if @resource
156
+
146
157
  resource = object.instance_variable_get(:@__graphiti_resource)
147
158
  resource_class = resource.class
148
159
  if resource_class.polymorphic? && !resource_class.polymorphic_child?
@@ -165,7 +176,7 @@ module GraphitiGql
165
176
 
166
177
  def add_fields(type, resource)
167
178
  self.class.add_fields(type, resource)
168
- end
179
+ end
169
180
 
170
181
  def build_connection_class
171
182
  klass = Class.new(GraphQL::Types::Relay::BaseConnection)
@@ -174,4 +185,4 @@ module GraphitiGql
174
185
  end
175
186
  end
176
187
  end
177
- end
188
+ end
@@ -1,3 +1,3 @@
1
1
  module GraphitiGql
2
- VERSION = "0.2.31"
2
+ VERSION = "0.2.32"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphiti_gql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.31
4
+ version: 0.2.32
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Richmond
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-31 00:00:00.000000000 Z
11
+ date: 2022-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql