graphiti_gql 0.2.10 → 0.2.14

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: c8778ea5c62ff7c2eb9f32d674ece0a9b0c87ee30f57417fe02e61e2ed8c75e8
4
- data.tar.gz: a80c2443b2c13cbd0d36d7160f8b2e3bde75ac97d87b4d58278440fab0e8426e
3
+ metadata.gz: 5a4faaf029a4624626f9bfa68815734bc6571381fa6b2d153830df1825c2d61f
4
+ data.tar.gz: 2f73a19e8b84280c83bc7d6ab0f574ffb5d4b2fb6b7328ca75de6d47a145ecc0
5
5
  SHA512:
6
- metadata.gz: cad6708ae17b85ec5010245978d2f1dae7077fbbcc7d1303241ac1798a110b58638e68ab4d66ffab73a285f5a6ea5d8bc92b2cc347e2bd49c87d3b74fe96dcd7
7
- data.tar.gz: '09d2dc8810af44fca96160ab941cca950dd35779b7659b30882cd3e2ec3a40a9551055c5cc585ff6c9514a3c3952491c986225f8ede57e915dc64af0901df739'
6
+ metadata.gz: d00b4b5b4e2314efd6e1d4ff6b72a357ba582a045dcab28214596130054fc8e15b22b8267fa1157e1746ddceda7c0b25602a0441ccaa82b4bda29fa27aab412c
7
+ data.tar.gz: 7a6d2a5ee610af959d292e87c492aa770ff2ab2d0546339dc3f30236811a3b44ab7edf9110773c457707d19bb0d06c9b62884a69ea94c13ad90d227eb8540bce
@@ -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
@@ -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,13 +69,17 @@ 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
 
77
+
75
78
  def run
76
79
  lambda do
77
- proxy.to_h
78
- proxy
80
+ instance = proxy
81
+ instance.run!
82
+ instance
79
83
  end
80
84
  end
81
85
 
@@ -86,5 +90,9 @@ module GraphitiGql
86
90
  def result
87
91
  @result ||= run!
88
92
  end
93
+
94
+ def json
95
+ @json ||= result.response
96
+ end
89
97
  end
90
98
  end
@@ -1,3 +1,3 @@
1
1
  module GraphitiGql
2
- VERSION = "0.2.10"
2
+ VERSION = "0.2.14"
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.10
4
+ version: 0.2.14
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-07-13 00:00:00.000000000 Z
11
+ date: 2022-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql