graphiti_gql 0.2.22 → 0.2.23

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: 1a79d794e346a1f884170026503f4ca36a5c82529a47dd977b34335457d4a630
4
- data.tar.gz: 0ee188c2f278895a47b27c8dbe57b1117dfe352350a99bec8a908702cc782411
3
+ metadata.gz: c5dfc016180ec5fd536f236be63b24cad42471a74d44ab234dee1966f2de942e
4
+ data.tar.gz: f97bbe243fa4a205004abdbd522942005b09e87fa37ba1db465000b383e355d0
5
5
  SHA512:
6
- metadata.gz: d4eb9c8bb29c513cc9b8eed6e7dafffbefa1de88ab9e1dbc32d1f921b0664ee066c671d36c05eeb15614accded9ba56d4247010ea9c83bf55e0f76ae4d036b31
7
- data.tar.gz: 014ee03255a4c2b26bbe178d6c60f4f071955a288dac725aa75ae84dcd6624bde251442ea3f7e7811bae4ec9b7f463f024cdcc5c3c4f6f90ec8575e7eb071999
6
+ metadata.gz: '08c76642c2b6be4401a6efcce4c9070c52eee1661c333bcefc30a328826a0558aa6ed013028cb4d173bb2b47565aeeda18c2648a946d6ebe1dbcde2147325ced'
7
+ data.tar.gz: d16a38a32815b6df020817bb5797ac26ce6507a443375955aad3c2c1fe8fe29192dbf0f01a854d6d565946daae1fe9a713c67df050bbc230c320c282a35e339f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- graphiti_gql (0.2.21)
4
+ graphiti_gql (0.2.23)
5
5
  activemodel (> 6.0, < 8.0)
6
6
  graphiti (~> 1.3.9)
7
7
  graphql (~> 2.0)
@@ -48,13 +48,13 @@ GEM
48
48
  graphql-batch (0.5.1)
49
49
  graphql (>= 1.10, < 3)
50
50
  promise.rb (~> 0.7.2)
51
- i18n (1.10.0)
51
+ i18n (1.12.0)
52
52
  concurrent-ruby (~> 1.0)
53
53
  jsonapi-renderer (0.2.2)
54
54
  jsonapi-serializable (0.3.1)
55
55
  jsonapi-renderer (~> 0.2.0)
56
56
  method_source (1.0.0)
57
- minitest (5.15.0)
57
+ minitest (5.16.2)
58
58
  promise.rb (0.7.4)
59
59
  pry (0.13.1)
60
60
  coderay (~> 1.1)
@@ -76,7 +76,7 @@ GEM
76
76
  diff-lcs (>= 1.2.0, < 2.0)
77
77
  rspec-support (~> 3.11.0)
78
78
  rspec-support (3.11.0)
79
- tzinfo (2.0.4)
79
+ tzinfo (2.0.5)
80
80
  concurrent-ruby (~> 1.0)
81
81
 
82
82
  PLATFORMS
@@ -196,26 +196,14 @@ module GraphitiGql
196
196
  module ManyToManyExtras
197
197
  def self.prepended(klass)
198
198
  klass.class_eval do
199
- attr_reader :join_table_alias, :edge_magic
200
-
201
- class << self
202
- attr_reader :edge_resource
203
-
204
- def attribute(*args, &blk)
205
- @edge_resource ||= Class.new(Graphiti::Resource) do
206
- def self.abstract_class?
207
- true
208
- end
209
- end
210
- @edge_resource.attribute(*args, &blk)
211
- end
212
- end
199
+ attr_reader :join_table_alias, :edge_magic, :edge_resource
213
200
  end
214
201
  end
215
202
 
216
203
  def initialize(name, opts = {})
217
204
  @join_table_alias = opts[:join_table_alias]
218
205
  @edge_magic = opts[:edge_magic] == false ? false : true
206
+ @edge_resource = opts[:edge_resource]
219
207
  super
220
208
  end
221
209
 
@@ -16,13 +16,15 @@ module GraphitiGql
16
16
 
17
17
  private
18
18
 
19
+ def thru_model
20
+ thru = @sideload.foreign_key.keys.first
21
+ reflection = @sideload.parent_resource.model.reflect_on_association(thru)
22
+ reflection.klass
23
+ end
24
+
19
25
  def add_join_table_magic(proxy)
20
26
  return unless @sideload.edge_magic
21
27
  if defined?(ActiveRecord) && proxy.resource.model.ancestors.include?(ActiveRecord::Base)
22
- thru = @sideload.foreign_key.keys.first
23
- reflection = @sideload.parent_resource.model.reflect_on_association(thru)
24
- thru_model = reflection.klass
25
-
26
28
  thru_table_name = @sideload.join_table_alias || thru_model.table_name
27
29
  names = thru_model.column_names.map do |n|
28
30
  next if n == :id
@@ -3,12 +3,11 @@ module GraphitiGql
3
3
  module Fields
4
4
  class Attribute
5
5
  # If sideload is present, we're applying m2m metadata to an edge
6
- def initialize(resource, name, config, sideload = nil)
6
+ def initialize(resource, name, config)
7
7
  @resource = resource
8
8
  @config = config
9
9
  @name = name
10
10
  @alias = config[:alias]
11
- @sideload = sideload # is_edge: true
12
11
  end
13
12
 
14
13
  def apply(type)
@@ -16,13 +15,11 @@ module GraphitiGql
16
15
  _config = @config
17
16
  _name = @name
18
17
  _alias = @alias
19
- _sideload = @sideload
20
18
  opts = @config.slice(:null, :deprecation_reason)
21
19
  type.field(_name, field_type, **opts)
22
20
  type.define_method _name do
23
21
  if (readable = _config[:readable]).is_a?(Symbol)
24
22
  obj = object
25
- obj = object.node if _sideload
26
23
  resource = obj.instance_variable_get(:@__graphiti_resource)
27
24
  unless resource.send(readable)
28
25
  path = Graphiti.context[:object][:current_path].join(".")
@@ -30,25 +27,10 @@ module GraphitiGql
30
27
  end
31
28
  end
32
29
 
33
- edge_attrs = nil
34
- if _sideload
35
- edge_attrs = object.node.attributes
36
- .select { |k, v| k.to_s.starts_with?("_edge_") }
37
- edge_attrs.transform_keys! { |k| k.to_s.gsub("_edge_", "").to_sym }
38
- end
39
-
40
30
  value = if _config[:proc]
41
- if _sideload
42
- instance_exec(edge_attrs, object.node, &_config[:proc])
43
- else
44
- instance_eval(&_config[:proc])
45
- end
31
+ instance_eval(&_config[:proc])
46
32
  else
47
- if _sideload
48
- edge_attrs[_alias || _name]
49
- else
50
- object.send(_alias || _name)
51
- end
33
+ object.send(_alias || _name)
52
34
  end
53
35
  return if value.nil?
54
36
  Graphiti::Types[_config[:type]][:read].call(value)
@@ -35,7 +35,7 @@ module GraphitiGql
35
35
  end
36
36
 
37
37
  def customized_edge?
38
- @sideload.type == :many_to_many && @sideload.class.edge_resource
38
+ @sideload.type == :many_to_many && @sideload.edge_resource
39
39
  end
40
40
 
41
41
  def find_or_build_connection
@@ -59,21 +59,60 @@ module GraphitiGql
59
59
  end
60
60
 
61
61
  def build_edge_type_class(sideload_type)
62
- prior_edge_type_class = sideload_type.edge_type_class
63
- edge_type_class = Class.new(prior_edge_type_class)
64
- edge_resource = @sideload.class.edge_resource
65
- edge_resource.attributes.each_pair do |name, config|
66
- next if name == :id
67
- Schema::Fields::Attribute.new(edge_resource, name, config, @sideload).apply(edge_type_class)
62
+ klass = build_friendly_graphql_edge_type_class \
63
+ sideload_type.edge_type_class
64
+ name = edge_type_class_name(sideload_type)
65
+ klass.define_method(:graphql_name) { name }
66
+ klass.graphql_name(name)
67
+ edge_resource = @sideload.edge_resource
68
+ ResourceType.add_fields(klass, edge_resource, id: false)
69
+ ResourceType.add_relationships(edge_resource, klass)
70
+ klass
71
+ end
72
+
73
+ # Normally we reference 'object', but edges work differently
74
+ # This makes 'object' work everywhere
75
+ # Needed when evaluating fields/relationships for consistent interface
76
+ def build_friendly_graphql_edge_type_class(superklass)
77
+ klass = Class.new(superklass) do
78
+ alias :original_object :object
79
+ def object
80
+ return @_object if @_object # avoid conflict
81
+
82
+ node = original_object.node # the 'parent' record we joined with
83
+ edge_attrs = node.attributes.select { |k,v| k.to_s.starts_with?('_edge') }
84
+ edge_attrs.transform_keys! { |k| k.to_s.gsub('_edge_', '') }
85
+ edge_model = model.new(edge_attrs)
86
+ edge_model.instance_variable_set(:@__graphiti_resource, resource)
87
+ @_object = edge_model
88
+ @_object
89
+ end
90
+
91
+ def cursor
92
+ original_object.cursor
93
+ end
94
+
95
+ def node
96
+ original_object.node
97
+ end
68
98
  end
69
- registered_parent = Schema.registry.get(@sideload.parent_resource.class)
99
+
100
+ # used in #object
101
+ thru = @sideload.foreign_key.keys.first
102
+ reflection = @sideload.parent_resource.model.reflect_on_association(thru)
103
+ thru_model = reflection.klass
104
+ edge_resource = @sideload.edge_resource.new
105
+ klass.define_method(:model) { thru_model }
106
+ klass.define_method(:resource) { edge_resource }
107
+
108
+ klass
109
+ end
110
+
111
+ def edge_type_class_name(sideload_type)
112
+ registered_parent = Schema.registry.get \
113
+ @sideload.parent_resource.class
70
114
  parent_name = registered_parent[:type].graphql_name
71
- edge_type_class_name = "#{parent_name}To#{sideload_type.graphql_name}Edge"
72
- edge_type_class.define_method :graphql_name do
73
- edge_type_class_name
74
- end
75
- edge_type_class.graphql_name(edge_type_class_name)
76
- edge_type_class
115
+ "#{parent_name}To#{sideload_type.graphql_name}Edge"
77
116
  end
78
117
  end
79
118
  end
@@ -30,30 +30,9 @@ module GraphitiGql
30
30
  end
31
31
 
32
32
  def add_relationships
33
- each_relationship do |type, sideload_type, sideload|
34
- if [:has_many, :many_to_many, :has_one].include?(sideload.type)
35
- Fields::ToMany.new(sideload, sideload_type).apply(type)
36
- else
37
- Fields::ToOne.new(sideload, sideload_type).apply(type)
38
- end
39
- end
40
- end
41
-
42
- def each_relationship
43
33
  registry.resource_types.each do |registered|
44
- registered[:resource].sideloads.each do |name, sl|
45
- next unless sl.readable?
46
-
47
- registered_sl = if sl.type == :polymorphic_belongs_to
48
- PolymorphicBelongsToInterface
49
- .new(registered[:resource], sl)
50
- .build
51
- else
52
- registry.get(sl.resource.class)
53
- end
54
-
55
- yield registered[:type], registered_sl[:type], sl
56
- end
34
+ resource, type = registered[:resource], registered[:type]
35
+ ResourceType.add_relationships(resource, type)
57
36
  end
58
37
  end
59
38
  end
@@ -14,6 +14,36 @@ module GraphitiGql
14
14
  end
15
15
  end
16
16
 
17
+ def self.add_fields(type, resource, id: true)
18
+ resource.attributes.each_pair do |name, config|
19
+ next if name == :id && id == false
20
+ if config[:readable]
21
+ Fields::Attribute.new(resource, name, config).apply(type)
22
+ end
23
+ end
24
+ end
25
+
26
+ def self.add_relationships(resource, type)
27
+ resource.sideloads.each do |name, sideload|
28
+ next unless sideload.readable?
29
+
30
+ registered_sl = if sideload.type == :polymorphic_belongs_to
31
+ PolymorphicBelongsToInterface
32
+ .new(resource, sideload)
33
+ .build
34
+ else
35
+ Schema.registry.get(sideload.resource.class)
36
+ end
37
+ sideload_type = registered_sl[:type]
38
+
39
+ if [:has_many, :many_to_many, :has_one].include?(sideload.type)
40
+ Fields::ToMany.new(sideload, sideload_type).apply(type)
41
+ else
42
+ Fields::ToOne.new(sideload, sideload_type).apply(type)
43
+ end
44
+ end
45
+ end
46
+
17
47
  def initialize(resource, implements: nil)
18
48
  @resource = resource
19
49
  @implements = implements
@@ -86,14 +116,10 @@ module GraphitiGql
86
116
  def name
87
117
  registry.key_for(@resource)
88
118
  end
89
-
119
+
90
120
  def add_fields(type, resource)
91
- resource.attributes.each_pair do |name, config|
92
- if config[:readable]
93
- Fields::Attribute.new(@resource, name, config).apply(type)
94
- end
95
- end
96
- end
121
+ self.class.add_fields(type, resource)
122
+ end
97
123
 
98
124
  def build_connection_class
99
125
  klass = Class.new(GraphQL::Types::Relay::BaseConnection)
@@ -1,3 +1,3 @@
1
1
  module GraphitiGql
2
- VERSION = "0.2.22"
2
+ VERSION = "0.2.23"
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.22
4
+ version: 0.2.23
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-09 00:00:00.000000000 Z
11
+ date: 2022-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql