graphiti_gql 0.2.23 → 0.2.24

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: c5dfc016180ec5fd536f236be63b24cad42471a74d44ab234dee1966f2de942e
4
- data.tar.gz: f97bbe243fa4a205004abdbd522942005b09e87fa37ba1db465000b383e355d0
3
+ metadata.gz: 696f200f2ac54b136b21b4b8f237cddfddf742d73da51b73c2513dbb7c0cc60a
4
+ data.tar.gz: 914867efbf89a58b33de2e28b65c253577852cedf146e8f2050e2330d9534cdf
5
5
  SHA512:
6
- metadata.gz: '08c76642c2b6be4401a6efcce4c9070c52eee1661c333bcefc30a328826a0558aa6ed013028cb4d173bb2b47565aeeda18c2648a946d6ebe1dbcde2147325ced'
7
- data.tar.gz: d16a38a32815b6df020817bb5797ac26ce6507a443375955aad3c2c1fe8fe29192dbf0f01a854d6d565946daae1fe9a713c67df050bbc230c320c282a35e339f
6
+ metadata.gz: cfa86ca259696ca32ba303b5bf4abb8064c7d60b179ef106a136da1a292d0721ec81a12242d9a0ba993c69dae93c0367350354475b13132aead992380bf129af
7
+ data.tar.gz: 56a9db45d32242003eb8a9e9cb74f6efc96c92f9889abc93c8b038f6e07a3d5dbe836bfd84d4c4e053d3ff46bfbacf5676408ba95c10b357463a5e6034ac51c2
@@ -25,6 +25,10 @@ module GraphitiGql
25
25
  end
26
26
  end
27
27
 
28
+ def value_object?
29
+ self.class.value_object?
30
+ end
31
+
28
32
  def filterings
29
33
  @filterings ||= begin
30
34
  if @params.key?(:filter)
@@ -67,6 +71,11 @@ module GraphitiGql
67
71
  end
68
72
 
69
73
  class_methods do
74
+ def config
75
+ return @config if @config
76
+ super
77
+ @config = @config.merge(value_objects: {}, is_value_object: false)
78
+ end
70
79
 
71
80
  def attribute(*args)
72
81
  super(*args).tap do
@@ -93,6 +102,40 @@ module GraphitiGql
93
102
  super
94
103
  end
95
104
  end
105
+
106
+ def value_object?
107
+ !!config[:is_value_object]
108
+ end
109
+
110
+ def value_object!
111
+ config[:is_value_object] = true
112
+ self.adapter = ::Graphiti::Adapters::Null
113
+ config[:filters] = {}
114
+ config[:stats] = {}
115
+ config[:sorts] = {}
116
+ config[:attributes].delete(:id)
117
+ define_method :base_scope do
118
+ {}
119
+ end
120
+ define_method :resolve do |parent|
121
+ [parent]
122
+ end
123
+ end
124
+
125
+ def value_object(name, opts = {})
126
+ opts[:array] ||= false
127
+ opts[:null] ||= true
128
+ config[:value_objects][name] = Graphiti::ValueObjectAssociation.new(
129
+ name,
130
+ parent_resource_class: self,
131
+ resource_class: opts[:resource],
132
+ _alias: opts[:alias],
133
+ is_array: opts[:array],
134
+ null: opts[:null],
135
+ readable: opts[:readable],
136
+ deprecation_reason: opts[:deprecation_reason]
137
+ )
138
+ end
96
139
  end
97
140
  end
98
141
  Graphiti::Resource.send(:prepend, ResourceExtras)
@@ -264,6 +307,20 @@ module GraphitiGql
264
307
  description: 'Datetime with milliseconds'
265
308
  }
266
309
 
310
+ [:string, :integer, :float, :datetime, :precise_datetime].each do |kind|
311
+ duped_hash = Graphiti::Util::Hash.deep_dup(Graphiti::Types[:hash])
312
+ type = Graphiti::Types[:"#{kind}_range"] = duped_hash
313
+ type[:canonical_name] = :"#{kind}_range"
314
+ Graphiti::Types[:"array_of_#{kind}_ranges"] = {
315
+ canonical_name: :"#{kind}_range",
316
+ params: Dry::Types["strict.array"].of(type[:params]),
317
+ read: Dry::Types["strict.array"].of(type[:read]),
318
+ write: Dry::Types["strict.array"].of(type[:write]),
319
+ kind: "array",
320
+ description: "Base Type."
321
+ }
322
+ end
323
+
267
324
  module ActiveRecordAdapterExtras
268
325
  extend ActiveSupport::Concern
269
326
 
@@ -363,6 +420,15 @@ module GraphitiGql
363
420
 
364
421
  Graphiti::Query.send(:prepend, QueryExtras)
365
422
  module ScopeExtras
423
+ def initialize(object, resource, query, opts = {})
424
+ if resource.value_object?
425
+ object = query.params[:parent]
426
+ super(object, resource, query, opts)
427
+ else
428
+ super
429
+ end
430
+ end
431
+
366
432
  def resolve(*args)
367
433
  results = super
368
434
  results.reverse! if @query.hash[:reverse]
@@ -383,4 +449,48 @@ module GraphitiGql
383
449
  ::Graphiti::Adapters::ActiveRecord::ManyToManySideload
384
450
  .send(:prepend, ActiveRecordManyToManyExtras)
385
451
  end
452
+ end
453
+
454
+ class Graphiti::ValueObjectAssociation
455
+ attr_reader :name,
456
+ :parent_resource_class,
457
+ :alias,
458
+ :readable,
459
+ :null,
460
+ :deprecation_reason
461
+
462
+ def initialize(
463
+ name,
464
+ parent_resource_class:,
465
+ resource_class:,
466
+ is_array: false,
467
+ readable: nil,
468
+ null: true,
469
+ _alias: nil,
470
+ deprecation_reason: nil
471
+ )
472
+ @name = name
473
+ @parent_resource_class = parent_resource_class
474
+ @resource_class = resource_class
475
+ @readable = readable
476
+ @array = is_array
477
+ @alias = _alias
478
+ @null = null
479
+ @deprecation_reason = deprecation_reason
480
+ end
481
+
482
+ def array?
483
+ !!@array
484
+ end
485
+
486
+ def resource_class
487
+ @resource_class ||= Graphiti::Util::Class
488
+ .infer_resource_class(@parent_resource_class, name)
489
+ end
490
+
491
+ def build_resource(parent)
492
+ instance = resource_class.new
493
+ instance.parent = parent
494
+ instance
495
+ end
386
496
  end
@@ -30,7 +30,11 @@ module GraphitiGql
30
30
  value = if _config[:proc]
31
31
  instance_eval(&_config[:proc])
32
32
  else
33
- object.send(_alias || _name)
33
+ if object.is_a?(Hash)
34
+ object[_name] || object[_name.to_s]
35
+ else
36
+ object.send(_alias || _name)
37
+ end
34
38
  end
35
39
  return if value.nil?
36
40
  Graphiti::Types[_config[:type]][:read].call(value)
@@ -67,6 +67,7 @@ module GraphitiGql
67
67
  edge_resource = @sideload.edge_resource
68
68
  ResourceType.add_fields(klass, edge_resource, id: false)
69
69
  ResourceType.add_relationships(edge_resource, klass)
70
+ ResourceType.add_value_objects(edge_resource, klass)
70
71
  klass
71
72
  end
72
73
 
@@ -10,6 +10,7 @@ module GraphitiGql
10
10
  def build
11
11
  @resources.each { |resource| ResourceType.new(resource).build }
12
12
  define_entrypoints
13
+ add_value_objects
13
14
  add_relationships
14
15
  @query_class
15
16
  end
@@ -35,6 +36,13 @@ module GraphitiGql
35
36
  ResourceType.add_relationships(resource, type)
36
37
  end
37
38
  end
39
+
40
+ def add_value_objects
41
+ registry.resource_types.each do |registered|
42
+ resource, type = registered[:resource], registered[:type]
43
+ ResourceType.add_value_objects(resource, type)
44
+ end
45
+ end
38
46
  end
39
47
  end
40
48
  end
@@ -52,7 +52,8 @@ module GraphitiGql
52
52
  # When polymorphic parent, returns the Interface not the Class
53
53
  def resource_types
54
54
  values
55
- .select { |v| v.key?(:resource) && !v[:interface] }
55
+ .select { |v| v.key?(:resource) }
56
+ .reject { |v| v[:interface] || v[:resource].value_object? }
56
57
  .map { |registered| get(registered[:resource]) }
57
58
  end
58
59
 
@@ -14,7 +14,7 @@ module GraphitiGql
14
14
  end
15
15
  end
16
16
 
17
- def self.add_fields(type, resource, id: true)
17
+ def self.add_fields(type, resource, id: true) # id: false for edges
18
18
  resource.attributes.each_pair do |name, config|
19
19
  next if name == :id && id == false
20
20
  if config[:readable]
@@ -23,6 +23,39 @@ module GraphitiGql
23
23
  end
24
24
  end
25
25
 
26
+ def self.add_value_objects(resource, type)
27
+ resource.config[:value_objects].each_pair do |name, vo_association|
28
+ vo_resource_class = vo_association.resource_class
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
33
+
34
+ _array = vo_association.array?
35
+ opts = { null: vo_association.null }
36
+ opts[:deprecation_reason] = vo_association.deprecation_reason if vo_association.deprecation_reason
37
+ type.field name, value_object_type, **opts
38
+ 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
44
+ end
45
+
46
+ result = vo_resource_class.all({ parent: object }).to_a
47
+ if result.is_a?(Array) && !_array
48
+ result = result.first
49
+ end
50
+ if result == object || result == [object]
51
+ method_name = vo_association.alias.presence || name
52
+ result = object.send(method_name)
53
+ end
54
+ result
55
+ end
56
+ end
57
+ end
58
+
26
59
  def self.add_relationships(resource, type)
27
60
  resource.sideloads.each do |name, sideload|
28
61
  next unless sideload.readable?
@@ -4,6 +4,31 @@ module GraphitiGql
4
4
  self.time_precision = 6
5
5
  end
6
6
 
7
+ class DatetimeRange < GraphQL::Schema::Object
8
+ field :from, GraphQL::Types::ISO8601DateTime
9
+ field :to, GraphQL::Types::ISO8601DateTime
10
+ end
11
+
12
+ class PreciseDatetimeRange < GraphQL::Schema::Object
13
+ field :from, PreciseDatetime
14
+ field :to, PreciseDatetime
15
+ end
16
+
17
+ class StringRange < GraphQL::Schema::Object
18
+ field :from, String
19
+ field :to, String
20
+ end
21
+
22
+ class IntegerRange < GraphQL::Schema::Object
23
+ field :from, Integer
24
+ field :to, Integer
25
+ end
26
+
27
+ class FloatRange < GraphQL::Schema::Object
28
+ field :from, Float
29
+ field :to, Float
30
+ end
31
+
7
32
  GQL_TYPE_MAP = {
8
33
  integer_id: String,
9
34
  string: String,
@@ -16,13 +41,23 @@ module GraphitiGql
16
41
  datetime: GraphQL::Types::ISO8601DateTime,
17
42
  precise_datetime: PreciseDatetime,
18
43
  hash: GraphQL::Types::JSON,
44
+ string_range: StringRange,
45
+ integer_range: IntegerRange,
46
+ float_range: FloatRange,
47
+ datetime_range: DatetimeRange,
48
+ precise_datetime_range: PreciseDatetimeRange,
19
49
  array: [GraphQL::Types::JSON],
20
50
  array_of_strings: [String],
21
51
  array_of_integers: [Integer],
22
52
  array_of_floats: [Float],
23
53
  array_of_dates: [GraphQL::Types::ISO8601Date],
24
54
  array_of_datetimes: [GraphQL::Types::ISO8601DateTime],
25
- array_of_precise_datetimes: [PreciseDatetime]
55
+ array_of_precise_datetimes: [PreciseDatetime],
56
+ array_of_string_ranges: [StringRange],
57
+ array_of_integer_ranges: [IntegerRange],
58
+ array_of_float_ranges: [FloatRange],
59
+ array_of_datetime_ranges: [DatetimeRange],
60
+ array_of_precise_datetime_ranges: [PreciseDatetimeRange]
26
61
  }
27
62
 
28
63
  class RelayConnectionExtension < GraphQL::Schema::Field::ConnectionExtension
@@ -1,3 +1,3 @@
1
1
  module GraphitiGql
2
- VERSION = "0.2.23"
2
+ VERSION = "0.2.24"
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.23
4
+ version: 0.2.24
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-10 00:00:00.000000000 Z
11
+ date: 2022-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql