graphiti_gql 0.2.12 → 0.2.16

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: 535af0267b130ee480022a9058880a17f742ec6aa2209cc2b1d9991a82f9afbc
4
+ data.tar.gz: 43c9d8f5c3ff6147776d71741a2a24996b300fa75c3c495f2a4cb00fb11a14ca
5
5
  SHA512:
6
- metadata.gz: 3ad649a5f015c967529be572ad5f586edbeeb83205aaf4421c0f8906099213565492ba54f871e141aee37a668d5255cfa7188429cbbb15484d11af20c2b70cbe
7
- data.tar.gz: b1b6812f8e4f350682124de0dfefde997602ed41f6d7b5e704ae640d2e59636c789e1ba54c75a51ef5beea41fbd76d3213424736d7159f455aad27bd6323361d
6
+ metadata.gz: f5a0ea00caf0626444fd452c6502c07367cca9f579b561fd97ef2f615a6467fb415c82de9782f8d2640b567f9aeea90495e5cebb19533b564b2282afed25846b
7
+ data.tar.gz: 7cdf9e78135dd6257ec9226256d7242af74347762a4d22e4b7f1563f330fd0245b0326e164177fea524338d6c7b95f63f6c29bf13fa7e8e267aabe12affc325a
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.15)
5
5
  graphiti (~> 1.3.9)
6
6
  graphql (~> 2.0)
7
7
  graphql-batch (~> 0.5)
@@ -43,7 +43,7 @@ GEM
43
43
  jsonapi-serializable (~> 0.3.0)
44
44
  graphiti_errors (1.1.2)
45
45
  jsonapi-serializable (~> 0.1)
46
- graphql (2.0.11)
46
+ graphql (2.0.12)
47
47
  graphql-batch (0.5.1)
48
48
  graphql (>= 1.10, < 3)
49
49
  promise.rb (~> 0.7.2)
@@ -13,15 +13,21 @@ 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
23
27
  hash[key] = Node.new(value, sideload.resource.class)
24
28
  end
29
+ else
30
+ hash[key] = Node.new(value)
25
31
  end
26
32
  end
27
33
  end
@@ -59,15 +65,19 @@ module GraphitiGql
59
65
  @with_pagination = !!options[:with_pagination]
60
66
  end
61
67
 
62
- def to_h(symbolize_keys: true)
68
+ def run!(symbolize_keys: true)
63
69
  result = GraphitiGql.run(query, @params, @ctx)
64
70
  result = result.deep_symbolize_keys if symbolize_keys
65
71
  @response = result
66
72
  result
67
73
  end
68
74
 
69
- def node(id)
70
- nodes.find { |n| n.id == id.to_s }
75
+ def node(id = nil)
76
+ if @resource.singular
77
+ Node.new(underscore(data[data.keys.first]), @resource)
78
+ else
79
+ nodes.find { |n| n.id == id.to_s }
80
+ end
71
81
  end
72
82
 
73
83
  def nodes
@@ -82,7 +92,7 @@ module GraphitiGql
82
92
  alias :to_a :nodes
83
93
 
84
94
  def response
85
- @response ||= to_h
95
+ @response ||= run!
86
96
  end
87
97
 
88
98
  def data
@@ -9,35 +9,20 @@ module GraphitiGql
9
9
  GraphitiGql.schema!
10
10
  end
11
11
 
12
- module ControllerContext
13
- def graphql_context
14
- ctx = { controller: self }
15
- ctx[:current_user] = current_user if respond_to?(:current_user)
16
- ctx
17
- end
18
- end
19
-
20
12
  initializer "graphiti_gql.define_controller" do
21
- require "#{Rails.root}/app/controllers/application_controller"
22
13
  app_controller = GraphitiGql.config.application_controller || ::ApplicationController
23
- app_controller.send(:include, ControllerContext)
24
14
 
25
15
  # rubocop:disable Lint/ConstantDefinitionInBlock(Standard)
26
16
  class GraphitiGql::ExecutionController < app_controller
27
17
  def execute
28
18
  params = request.params # avoid strong_parameters
29
19
  variables = params[:variables] || {}
20
+ ctx = respond_to?(:graphql_context) ? graphql_context : {}
30
21
  result = GraphitiGql.run params[:query],
31
22
  params[:variables],
32
- graphql_context
23
+ ctx
33
24
  render json: result
34
25
  end
35
-
36
- private
37
-
38
- def default_context
39
- defined?(:current_user)
40
- end
41
26
  end
42
27
  end
43
28
  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,22 @@ module GraphitiGql
42
67
  att[:name] = args.first # for easier lookup
43
68
  end
44
69
  end
70
+
71
+ def filter(name, *args, &blk)
72
+ super
73
+ opts = args.extract_options!
74
+ if opts[:if]
75
+ attributes[name][:filterable] = opts[:if]
76
+ end
77
+ end
78
+
79
+ def filter_group(filter_names, *args)
80
+ if filter_names.blank?
81
+ config[:grouped_filters] = {}
82
+ else
83
+ super
84
+ end
85
+ end
45
86
  end
46
87
  end
47
88
  Graphiti::Resource.send(:include, ResourceExtras)
@@ -145,8 +186,10 @@ module GraphitiGql
145
186
  module ManyToManyExtras
146
187
  def self.prepended(klass)
147
188
  klass.class_eval do
189
+ attr_reader :join_table_alias, :edge_magic
190
+
148
191
  class << self
149
- attr_accessor :edge_resource
192
+ attr_reader :edge_resource
150
193
 
151
194
  def attribute(*args, &blk)
152
195
  @edge_resource ||= Class.new(Graphiti::Resource) do
@@ -159,6 +202,12 @@ module GraphitiGql
159
202
  end
160
203
  end
161
204
  end
205
+
206
+ def initialize(name, opts = {})
207
+ @join_table_alias = opts[:join_table_alias]
208
+ @edge_magic = opts[:edge_magic] == false ? false : true
209
+ super
210
+ end
162
211
 
163
212
  def apply_belongs_to_many_filter
164
213
  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)
@@ -22,7 +22,7 @@ module GraphitiGql
22
22
 
23
23
  def define_entrypoints
24
24
  registry.resource_types.each do |registered|
25
- if GraphitiGql.entrypoint?(registered[:resource])
25
+ if registered[:resource].graphql_entrypoint
26
26
  Fields::Index.new(registered).apply(@query_class)
27
27
  Fields::Show.new(registered).apply(@query_class)
28
28
  end
@@ -78,17 +78,22 @@ 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
85
85
 
86
86
  def run!
87
+ @json = nil
87
88
  @result = run.call
88
89
  end
89
90
 
90
91
  def result
91
92
  @result ||= run!
92
93
  end
94
+
95
+ def json
96
+ @json ||= result.response
97
+ end
93
98
  end
94
99
  end
@@ -1,3 +1,3 @@
1
1
  module GraphitiGql
2
- VERSION = "0.2.12"
2
+ VERSION = "0.2.16"
3
3
  end
data/lib/graphiti_gql.rb CHANGED
@@ -53,16 +53,8 @@ module GraphitiGql
53
53
  yield config
54
54
  end
55
55
 
56
- def self.entrypoints=(val)
57
- @entrypoints = val
58
- end
59
-
60
- def self.entrypoints
61
- @entrypoints
62
- end
63
-
64
56
  def self.entrypoint?(resource)
65
- @entrypoints.nil? || @entrypoints.include?(resource)
57
+ !!resource.graphql_entrypoint
66
58
  end
67
59
 
68
60
  def self.run(query_string, variables = {}, context = {})
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.16
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-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: []