graphiti_gql 0.2.11 → 0.2.15

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: 71fdec4bf251efc22810991433ff68f9797cd347537e01a9cd309557c68567ea
4
- data.tar.gz: 9b65200d9eb293333b3cb4727389bc4c51ebc46da014d12f478f95c75fc48b0b
3
+ metadata.gz: 36cf98699fc608187e70b321c60b32fc68989b5db05704fbd0f3adcdc3e7c947
4
+ data.tar.gz: 1ee21e3fcce82d24027172bd106f395a797dbb4e9de8b52cbb18caf3bb41f474
5
5
  SHA512:
6
- metadata.gz: f6e7ad07688af72a5850d5a93338ebce34c224e54259fce30a32b51a5deb842a8e9b1f3f2779bcf21ca841bfc7e7a88206c63e5bd3cb56644c92612c626762ec
7
- data.tar.gz: a18337b399b74f1389e43becffddff4119a0c07b6c53ddfc84a0e2d925d7b5339206823d6f366adf24484838b79dda45298b11cee785d258455cebd802c46c1a
6
+ metadata.gz: ca86b7bba1a1c1b8e1a086872278ce730ad4e696002d45b7ebc519668294e1a0129c52da060d1a1a8673cf561f0d9d01e9c7c622b4f218316c1703fea554a5a6
7
+ data.tar.gz: 2f1081e4677027d6c6bdb703c8eb59a0f702fa6e07bd0249e6f4f4ecb103bc53ae794c9025c1b8634dcb445c476fd8ce04648f841cfcf4fd4ef04667cc19800a
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- graphiti_gql (0.2.9)
4
+ graphiti_gql (0.2.15)
5
5
  graphiti (~> 1.3.9)
6
6
  graphql (~> 2.0)
7
7
  graphql-batch (~> 0.5)
@@ -20,15 +20,11 @@ GEM
20
20
  coderay (1.1.3)
21
21
  concurrent-ruby (1.1.10)
22
22
  diff-lcs (1.5.0)
23
- dry-configurable (0.15.0)
23
+ dry-container (0.10.0)
24
24
  concurrent-ruby (~> 1.0)
25
- dry-core (~> 0.6)
26
- dry-container (0.9.0)
25
+ dry-core (0.8.0)
27
26
  concurrent-ruby (~> 1.0)
28
- dry-configurable (~> 0.13, >= 0.13.0)
29
- dry-core (0.7.1)
30
- concurrent-ruby (~> 1.0)
31
- dry-inflector (0.2.1)
27
+ dry-inflector (0.3.0)
32
28
  dry-logic (1.2.0)
33
29
  concurrent-ruby (~> 1.0)
34
30
  dry-core (~> 0.5, >= 0.5)
@@ -13,10 +13,14 @@ module GraphitiGql
13
13
  if (sideload = resource.sideload(key))
14
14
  if value.key?(:edges)
15
15
  @edges[key] = value[:edges].map do |edge|
16
- node_id = edge[:node][:id]
16
+ node_id = edge[:node][:id] if edge[:node]
17
17
  Node.new(edge.except(:node).merge(node_id: node_id))
18
18
  end
19
- hash[key] = value[:edges].map { |v| Node.new(v[:node], sideload.resource.class) }
19
+ if value[:edges].any? { |e| e[:node] }
20
+ hash[key] = value[:edges].map { |v| Node.new(v[:node], sideload.resource.class) }
21
+ else
22
+ hash[key] = value[:edges]
23
+ end
20
24
  elsif value.key?(:nodes)
21
25
  hash[key] = value[:nodes].map { |n| Node.new(n, sideload.resource.class) }
22
26
  else
@@ -47,7 +51,7 @@ module GraphitiGql
47
51
  end
48
52
 
49
53
  class Proxy
50
- def initialize(resource, params, ctx, query)
54
+ def initialize(resource, params, ctx, query, options = {})
51
55
  @query = query
52
56
  @resource = resource
53
57
  @ctx = ctx
@@ -56,17 +60,22 @@ module GraphitiGql
56
60
  sort[:att] = sort[:att].to_s.camelize(:lower)
57
61
  sort[:dir] = sort[:dir].to_s
58
62
  end
63
+ @with_pagination = !!options[:with_pagination]
59
64
  end
60
65
 
61
- def to_h(symbolize_keys: true)
66
+ def run!(symbolize_keys: true)
62
67
  result = GraphitiGql.run(query, @params, @ctx)
63
68
  result = result.deep_symbolize_keys if symbolize_keys
64
69
  @response = result
65
70
  result
66
71
  end
67
72
 
68
- def node(id)
69
- nodes.find { |n| n.id == id.to_s }
73
+ def node(id = nil)
74
+ if @resource.singular
75
+ Node.new(underscore(data[data.keys.first]), @resource)
76
+ else
77
+ nodes.find { |n| n.id == id.to_s }
78
+ end
70
79
  end
71
80
 
72
81
  def nodes
@@ -76,12 +85,12 @@ module GraphitiGql
76
85
  else
77
86
  data[data.keys.first][:nodes] || []
78
87
  end
79
- elements.map { |n| Node.new(underscore(n), @resource) }
88
+ elements.compact.map { |n| Node.new(underscore(n), @resource) }
80
89
  end
81
90
  alias :to_a :nodes
82
91
 
83
92
  def response
84
- @response ||= to_h
93
+ @response ||= run!
85
94
  end
86
95
 
87
96
  def data
@@ -207,15 +216,18 @@ module GraphitiGql
207
216
  end
208
217
  end
209
218
 
210
- q << %|
211
- }
212
- }
213
- pageInfo {
214
- startCursor
215
- endCursor
216
- hasNextPage
217
- hasPreviousPage
218
- }|
219
+ q << %|
220
+ }
221
+ }|
222
+ if @with_pagination
223
+ q << %|
224
+ pageInfo {
225
+ startCursor
226
+ endCursor
227
+ hasNextPage
228
+ hasPreviousPage
229
+ }|
230
+ end
219
231
 
220
232
  if @params[:stats]
221
233
  q << %|
@@ -251,8 +263,8 @@ module GraphitiGql
251
263
  end
252
264
 
253
265
  class_methods do
254
- def gql(params = {}, ctx = {}, query = nil)
255
- Proxy.new(self, params, ctx, query)
266
+ def gql(params = {}, ctx = {}, query = nil, opts = {})
267
+ Proxy.new(self, params, ctx, query, opts)
256
268
  end
257
269
  end
258
270
  end
@@ -18,7 +18,6 @@ module GraphitiGql
18
18
  end
19
19
 
20
20
  initializer "graphiti_gql.define_controller" do
21
- require "#{Rails.root}/app/controllers/application_controller"
22
21
  app_controller = GraphitiGql.config.application_controller || ::ApplicationController
23
22
  app_controller.send(:include, ControllerContext)
24
23
 
@@ -2,15 +2,40 @@
2
2
  # Ideally we eventually rip out the parts of Graphiti we need and roll this into
3
3
  # that effort.
4
4
  module GraphitiGql
5
+ module RunnerExtras
6
+ def jsonapi_resource
7
+ @jsonapi_resource ||= begin
8
+ r = @resource_class.new
9
+ r.instance_variable_set(:@params, @params)
10
+ r
11
+ end
12
+ end
13
+ end
14
+ Graphiti::Runner.send(:prepend, RunnerExtras)
15
+
5
16
  module ResourceExtras
6
17
  extend ActiveSupport::Concern
7
18
 
8
19
  included do
9
20
  class << self
10
- attr_accessor :graphql_name
21
+ attr_accessor :graphql_name, :singular
22
+ end
23
+ end
24
+
25
+ def filterings
26
+ @filterings ||= begin
27
+ if @params.key?(:filter)
28
+ @params[:filter].keys
29
+ else
30
+ []
31
+ end
11
32
  end
12
33
  end
13
34
 
35
+ def parent_field
36
+ context[:current_arguments][:lookahead].field.owner
37
+ end
38
+
14
39
  def selections
15
40
  return @selections if @selections
16
41
  lookahead = context[:current_arguments]
@@ -42,6 +67,14 @@ module GraphitiGql
42
67
  att[:name] = args.first # for easier lookup
43
68
  end
44
69
  end
70
+
71
+ def filter_group(filter_names, *args)
72
+ if filter_names.blank?
73
+ config[:grouped_filters] = {}
74
+ else
75
+ super
76
+ end
77
+ end
45
78
  end
46
79
  end
47
80
  Graphiti::Resource.send(:include, ResourceExtras)
@@ -145,8 +178,10 @@ module GraphitiGql
145
178
  module ManyToManyExtras
146
179
  def self.prepended(klass)
147
180
  klass.class_eval do
181
+ attr_reader :join_table_alias, :edge_magic
182
+
148
183
  class << self
149
- attr_accessor :edge_resource
184
+ attr_reader :edge_resource
150
185
 
151
186
  def attribute(*args, &blk)
152
187
  @edge_resource ||= Class.new(Graphiti::Resource) do
@@ -159,6 +194,12 @@ module GraphitiGql
159
194
  end
160
195
  end
161
196
  end
197
+
198
+ def initialize(name, opts = {})
199
+ @join_table_alias = opts[:join_table_alias]
200
+ @edge_magic = opts[:edge_magic] == false ? false : true
201
+ super
202
+ end
162
203
 
163
204
  def apply_belongs_to_many_filter
164
205
  super
@@ -53,11 +53,18 @@ module GraphitiGql
53
53
  fulfill(id, val[:data][0])
54
54
  end
55
55
  else
56
- params = {filter: {id: {eq: ids.join(",")}}}
57
56
  resource = Schema.registry.get(@sideload.resource.class)[:resource]
57
+ params = {}
58
+ unless resource.singular
59
+ params[:filter] = {id: { eq: ids.join(",") } }
60
+ end
58
61
  records = resource.all(params).data
59
- map = records.index_by { |record| record.id }
60
- ids.each { |id| fulfill(id, map[id]) }
62
+ if resource.singular
63
+ ids.each { |id| fulfill(id, records[0]) }
64
+ else
65
+ map = records.index_by { |record| record.id }
66
+ ids.each { |id| fulfill(id, map[id]) }
67
+ end
61
68
  end
62
69
  end
63
70
  end
@@ -3,7 +3,14 @@ module GraphitiGql
3
3
  class HasMany < Many
4
4
  def assign(parent_records, proxy)
5
5
  records = proxy.data
6
- map = records.group_by { |record| record.send(@sideload.foreign_key) }
6
+
7
+ foreign_key = @sideload.foreign_key
8
+ config = @sideload.resource.attributes[foreign_key]
9
+ if config && config[:alias]
10
+ foreign_key = config[:alias]
11
+ end
12
+
13
+ map = records.group_by { |record| record.send(foreign_key) }
7
14
  parent_records.each do |pr|
8
15
  data = [map[pr.send(@sideload.primary_key)] || [], proxy]
9
16
  fulfill(pr, data)
@@ -57,7 +57,7 @@ module GraphitiGql
57
57
  elsif @sideload.type == :many_to_many
58
58
  fk = @sideload.foreign_key.values.first
59
59
  @params[:filter].merge!(fk => { eq: ids.join(",") })
60
- else
60
+ elsif !@sideload.parent_resource.class.singular
61
61
  @params[:filter].merge!(@sideload.foreign_key => { eq: ids.join(",") })
62
62
  end
63
63
 
@@ -17,14 +17,16 @@ module GraphitiGql
17
17
  private
18
18
 
19
19
  def add_join_table_magic(proxy)
20
+ return unless @sideload.edge_magic
20
21
  if defined?(ActiveRecord) && proxy.resource.model.ancestors.include?(ActiveRecord::Base)
21
22
  thru = @sideload.foreign_key.keys.first
22
23
  reflection = @sideload.parent_resource.model.reflect_on_association(thru)
23
24
  thru_model = reflection.klass
24
25
 
26
+ thru_table_name = @sideload.join_table_alias || thru_model.table_name
25
27
  names = thru_model.column_names.map do |n|
26
28
  next if n == :id
27
- "#{thru_model.table_name}.#{n} as _edge_#{n}"
29
+ "#{thru_table_name}.#{n} as _edge_#{n}"
28
30
  end
29
31
  scope = proxy.scope.object
30
32
  scope = scope.select(["#{proxy.resource.model.table_name}.*"] + names)
@@ -11,7 +11,9 @@ module GraphitiGql
11
11
  @registered[:type],
12
12
  null: true,
13
13
  extras: [:lookahead]
14
- field.argument(:id, String, required: true)
14
+ unless @registered[:resource].singular
15
+ field.argument(:id, String, required: true)
16
+ end
15
17
  _registered = @registered
16
18
  query.define_method name do |**arguments|
17
19
  params = Util.params_from_args(arguments)
@@ -69,14 +69,16 @@ module GraphitiGql
69
69
 
70
70
  def proxy
71
71
  q = defined?(query) ? query : nil
72
- resource.gql(params.merge(fields: fields), ctx, q)
72
+ with_pagination = respond_to?(:with_pagination) ? send(:with_pagination) : false
73
+ opts = { with_pagination: with_pagination }
74
+ resource.gql(params.merge(fields: fields), ctx, q, opts)
73
75
  end
74
76
 
75
77
 
76
78
  def run
77
79
  lambda do
78
80
  instance = proxy
79
- instance.to_h
81
+ instance.run!
80
82
  instance
81
83
  end
82
84
  end
@@ -88,5 +90,9 @@ module GraphitiGql
88
90
  def result
89
91
  @result ||= run!
90
92
  end
93
+
94
+ def json
95
+ @json ||= result.response
96
+ end
91
97
  end
92
98
  end
@@ -1,3 +1,3 @@
1
1
  module GraphitiGql
2
- VERSION = "0.2.11"
2
+ VERSION = "0.2.15"
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.11
4
+ version: 0.2.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Richmond
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-16 00:00:00.000000000 Z
11
+ date: 2022-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql
@@ -108,7 +108,7 @@ dependencies:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: '7.0'
111
- description:
111
+ description:
112
112
  email:
113
113
  - richmolj@gmail.com
114
114
  executables: []
@@ -170,7 +170,7 @@ licenses:
170
170
  - MIT
171
171
  metadata:
172
172
  homepage_uri: https://www.graphiti.dev
173
- post_install_message:
173
+ post_install_message:
174
174
  rdoc_options: []
175
175
  require_paths:
176
176
  - lib
@@ -185,8 +185,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
185
185
  - !ruby/object:Gem::Version
186
186
  version: '0'
187
187
  requirements: []
188
- rubygems_version: 3.0.3.1
189
- signing_key:
188
+ rubygems_version: 3.3.7
189
+ signing_key:
190
190
  specification_version: 4
191
191
  summary: GraphQL support for Graphiti
192
192
  test_files: []