graphiti_gql 0.2.12 → 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: 738c0d01e7e5491204399c4ddf9b0d346943555c49a1b64937e62c4503a23d9b
4
- data.tar.gz: 80837b0265bcfda82c95bb498e262f52208a75a06b61670df342aea53d16ceb5
3
+ metadata.gz: 5a4faaf029a4624626f9bfa68815734bc6571381fa6b2d153830df1825c2d61f
4
+ data.tar.gz: 2f73a19e8b84280c83bc7d6ab0f574ffb5d4b2fb6b7328ca75de6d47a145ecc0
5
5
  SHA512:
6
- metadata.gz: 3ad649a5f015c967529be572ad5f586edbeeb83205aaf4421c0f8906099213565492ba54f871e141aee37a668d5255cfa7188429cbbb15484d11af20c2b70cbe
7
- data.tar.gz: b1b6812f8e4f350682124de0dfefde997602ed41f6d7b5e704ae640d2e59636c789e1ba54c75a51ef5beea41fbd76d3213424736d7159f455aad27bd6323361d
6
+ metadata.gz: d00b4b5b4e2314efd6e1d4ff6b72a357ba582a045dcab28214596130054fc8e15b22b8267fa1157e1746ddceda7c0b25602a0441ccaa82b4bda29fa27aab412c
7
+ data.tar.gz: 7a6d2a5ee610af959d292e87c492aa770ff2ab2d0546339dc3f30236811a3b44ab7edf9110773c457707d19bb0d06c9b62884a69ea94c13ad90d227eb8540bce
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- graphiti_gql (0.2.11)
4
+ graphiti_gql (0.2.9)
5
5
  graphiti (~> 1.3.9)
6
6
  graphql (~> 2.0)
7
7
  graphql-batch (~> 0.5)
@@ -20,11 +20,15 @@ GEM
20
20
  coderay (1.1.3)
21
21
  concurrent-ruby (1.1.10)
22
22
  diff-lcs (1.5.0)
23
- dry-container (0.10.0)
23
+ dry-configurable (0.15.0)
24
24
  concurrent-ruby (~> 1.0)
25
- dry-core (0.8.0)
25
+ dry-core (~> 0.6)
26
+ dry-container (0.9.0)
26
27
  concurrent-ruby (~> 1.0)
27
- dry-inflector (0.3.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)
28
32
  dry-logic (1.2.0)
29
33
  concurrent-ruby (~> 1.0)
30
34
  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
@@ -59,15 +63,19 @@ module GraphitiGql
59
63
  @with_pagination = !!options[:with_pagination]
60
64
  end
61
65
 
62
- def to_h(symbolize_keys: true)
66
+ def run!(symbolize_keys: true)
63
67
  result = GraphitiGql.run(query, @params, @ctx)
64
68
  result = result.deep_symbolize_keys if symbolize_keys
65
69
  @response = result
66
70
  result
67
71
  end
68
72
 
69
- def node(id)
70
- 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
71
79
  end
72
80
 
73
81
  def nodes
@@ -82,7 +90,7 @@ module GraphitiGql
82
90
  alias :to_a :nodes
83
91
 
84
92
  def response
85
- @response ||= to_h
93
+ @response ||= run!
86
94
  end
87
95
 
88
96
  def data
@@ -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)
@@ -78,7 +78,7 @@ module GraphitiGql
78
78
  def run
79
79
  lambda do
80
80
  instance = proxy
81
- instance.to_h
81
+ instance.run!
82
82
  instance
83
83
  end
84
84
  end
@@ -90,5 +90,9 @@ module GraphitiGql
90
90
  def result
91
91
  @result ||= run!
92
92
  end
93
+
94
+ def json
95
+ @json ||= result.response
96
+ end
93
97
  end
94
98
  end
@@ -1,3 +1,3 @@
1
1
  module GraphitiGql
2
- VERSION = "0.2.12"
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.12
4
+ version: 0.2.14
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-17 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
@@ -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: []