graphiti_gql 0.2.2 → 0.2.5

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: 899580a6d3577a31dc7356d894eab26756c2c9876636db69ce5ece2d5eeee3ce
4
- data.tar.gz: faf261a7614a0565c9dc3e45b4a9d14d75962aa6665b7302752733584abb5bdc
3
+ metadata.gz: b9a1b76315488d9389a214fc640f6029d9e1a05a08182ebbdb9ed7958565e4aa
4
+ data.tar.gz: e4d6952a6929bf8310a99ba5b2d40e3821fe8bad51e89b3012c041fed9f1c47a
5
5
  SHA512:
6
- metadata.gz: becebe36d5aabea0a75ba3e617f9f62d0830d3f4a35e5becdacef1fbe10f4991621f061521f10bc4c0e527bdc834d5380e9f38ff0b9cc2388a66a7cf25bb33b8
7
- data.tar.gz: 37a9a4221261f112100e32f06832a948c30cbef5a06ffcb786f080538c1b9f3894bd3f7f8fc32394f0c2cb122e94c1b1e58cd6b8c38b6e0feab08a30d55bbfb5
6
+ metadata.gz: 30cb9c3f67aa059d0e68845fc64aee6313d9f0d14d36e19024357c619ca15e176d4178ae2836055c22b407a53187038b21b8447007e37fbed04ab70330c12ff4
7
+ data.tar.gz: 6ece60976b6d36294e9ef01e8521efdf26d9637b28c6fcfec833e3f2a81d493eece073137071e3373f2ac948c786c18e79734904b62e369a1cc0c0353860d71d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- graphiti_gql (0.2.1)
4
+ graphiti_gql (0.2.2)
5
5
  graphiti (~> 1.3.9)
6
6
  graphql (~> 2.0)
7
7
  graphql-batch (~> 0.5)
@@ -47,7 +47,7 @@ GEM
47
47
  jsonapi-serializable (~> 0.3.0)
48
48
  graphiti_errors (1.1.2)
49
49
  jsonapi-serializable (~> 0.1)
50
- graphql (2.0.7)
50
+ graphql (2.0.11)
51
51
  graphql-batch (0.5.1)
52
52
  graphql (>= 1.10, < 3)
53
53
  promise.rb (~> 0.7.2)
@@ -11,6 +11,27 @@ module GraphitiGql
11
11
  end
12
12
  end
13
13
 
14
+ def selections
15
+ return @selections if @selections
16
+ lookahead = context[:current_arguments]
17
+ .keyword_arguments[:lookahead]
18
+ nodes = lookahead.selection(:nodes)
19
+ if !nodes.selected?
20
+ nodes = lookahead
21
+ .selection(:edges)
22
+ .selection(:node)
23
+ end
24
+
25
+ if !nodes.selected?
26
+ nodes = lookahead
27
+ end
28
+
29
+ @selections = nodes
30
+ .selections
31
+ .map(&:name).map { |name| name.to_s.underscore.to_sym }
32
+ @selections
33
+ end
34
+
14
35
  class_methods do
15
36
  def attribute(*args)
16
37
  super(*args).tap do
@@ -159,7 +180,11 @@ module GraphitiGql
159
180
  end
160
181
 
161
182
  _in = definition.constructor do |input|
162
- Time.zone.parse(input)
183
+ if input.is_a?(ActiveSupport::TimeWithZone)
184
+ input = input.utc.round(10).iso8601(6)
185
+ else
186
+ Time.zone.parse(input)
187
+ end
163
188
  end
164
189
 
165
190
  # Register it with Graphiti
@@ -53,7 +53,8 @@ module GraphitiGql
53
53
  end
54
54
  else
55
55
  params = {filter: {id: {eq: ids.join(",")}}}
56
- records = @sideload.resource.class.all(params).data
56
+ resource = Schema.registry.get(@sideload.resource.class)[:resource]
57
+ records = resource.all(params).data
57
58
  map = records.index_by { |record| record.id }
58
59
  ids.each { |id| fulfill(id, map[id]) }
59
60
  end
@@ -1,12 +1,12 @@
1
1
  module GraphitiGql
2
2
  module Loaders
3
3
  class HasMany < Many
4
- def assign(ids, proxy)
4
+ def assign(parent_records, proxy)
5
5
  records = proxy.data
6
6
  map = records.group_by { |record| record.send(@sideload.foreign_key) }
7
- ids.each do |id|
8
- data = [map[id] || [], proxy]
9
- fulfill(id, data)
7
+ parent_records.each do |pr|
8
+ data = [map[pr.send(@sideload.primary_key)] || [], proxy]
9
+ fulfill(pr, data)
10
10
  end
11
11
  end
12
12
  end
@@ -0,0 +1,15 @@
1
+ module GraphitiGql
2
+ module Loaders
3
+ class HasOne < Many
4
+ def assign(parent_records, proxy)
5
+ records = proxy.data
6
+ parent_records.each do |pr|
7
+ corresponding = records.find do |r|
8
+ r.send(@sideload.foreign_key) == pr.send(@sideload.primary_key)
9
+ end
10
+ fulfill(pr, corresponding)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -6,6 +6,8 @@ module GraphitiGql
6
6
  PolymorphicHasMany.for(sideload, params)
7
7
  elsif sideload.type == :many_to_many
8
8
  ManyToMany.for(sideload, params)
9
+ elsif sideload.type == :has_one
10
+ HasOne.for(sideload, params)
9
11
  else
10
12
  HasMany.for(sideload, params)
11
13
  end
@@ -16,13 +18,26 @@ module GraphitiGql
16
18
  @params = params
17
19
  end
18
20
 
19
- def perform(ids)
20
- raise ::Graphiti::Errors::UnsupportedPagination if paginating? && ids.length > 1
21
- raise Errors::UnsupportedStats if requesting_stats? && ids.length > 1 && !can_group?
21
+ def perform(parent_records)
22
+ raise ::Graphiti::Errors::UnsupportedPagination if paginating? && parent_records.length > 1
23
+ raise Errors::UnsupportedStats if requesting_stats? && parent_records.length > 1 && !can_group?
22
24
 
23
- build_params(ids)
24
- proxy = @sideload.resource.class.all(@params)
25
- assign(ids, proxy)
25
+ ids = parent_records.map do |pr|
26
+ pk = pr.send(@sideload.primary_key)
27
+ if @sideload.polymorphic_as
28
+ hash = {}
29
+ hash[@sideload.foreign_key] = pk
30
+ hash[:"#{@sideload.polymorphic_as}_type"] = pr.class.name
31
+ hash
32
+ else
33
+ pk
34
+ end
35
+ end
36
+
37
+ build_params(ids, parent_records)
38
+ resource = Schema.registry.get(@sideload.resource.class)[:resource]
39
+ proxy = resource.all(@params)
40
+ assign(parent_records, proxy)
26
41
  end
27
42
 
28
43
  def assign(ids, proxy)
@@ -31,7 +46,7 @@ module GraphitiGql
31
46
 
32
47
  private
33
48
 
34
- def build_params(ids)
49
+ def build_params(ids, parent_records)
35
50
  @params[:filter] ||= {}
36
51
 
37
52
  if @sideload.polymorphic_as
@@ -59,6 +74,10 @@ module GraphitiGql
59
74
  @params[:page] ||= {}
60
75
  @params[:page][:size] = 999
61
76
  end
77
+
78
+ if @sideload.params_proc
79
+ @sideload.params_proc.call(@params, parent_records)
80
+ end
62
81
  end
63
82
 
64
83
  def paginating?
@@ -1,16 +1,16 @@
1
1
  module GraphitiGql
2
2
  module Loaders
3
3
  class ManyToMany < Many
4
- def assign(ids, proxy)
4
+ def assign(parent_records, proxy)
5
5
  thru = @sideload.foreign_key.keys.first
6
6
  fk = @sideload.foreign_key[thru]
7
7
  add_join_table_magic(proxy)
8
8
  records = proxy.data
9
- ids.each do |id|
9
+ parent_records.each do |pr|
10
10
  corresponding = records.select do |record|
11
- record.send(:"_edge_#{fk}") == id
11
+ record.send(:"_edge_#{fk}") == pr.send(@sideload.primary_key)
12
12
  end
13
- fulfill(id, [corresponding, proxy])
13
+ fulfill(pr, [corresponding, proxy])
14
14
  end
15
15
  end
16
16
 
@@ -1,15 +1,18 @@
1
1
  module GraphitiGql
2
2
  module Loaders
3
3
  class PolymorphicHasMany < Many
4
- def assign(ids, proxy)
4
+ def assign(parent_records, proxy)
5
5
  records = proxy.data
6
- ids.each do |id|
6
+ parent_records.each do |pr|
7
7
  corresponding = records.select do |record|
8
- record.send("#{@sideload.polymorphic_as}_type") == id[:"#{@sideload.polymorphic_as}_type"] &&
9
- record.send(@sideload.foreign_key) == id[@sideload.foreign_key]
8
+ child_ft = record.send("#{@sideload.polymorphic_as}_type")
9
+ child_fk = record.send(@sideload.foreign_key)
10
+ parent_ft = pr.class.name
11
+ parent_fk = pr.send(@sideload.primary_key)
12
+ child_ft == parent_ft && child_fk == parent_fk
10
13
  end
11
14
  data = [corresponding || [], proxy]
12
- fulfill(id, data)
15
+ fulfill(pr, data)
13
16
  end
14
17
  end
15
18
  end
@@ -12,32 +12,31 @@ module GraphitiGql
12
12
  end
13
13
 
14
14
  def apply(type)
15
- field = type.field @sideload.name,
16
- @sideload_type.connection_type,
17
- null: false,
15
+ opts = {
16
+ null: has_one?,
18
17
  connection: false,
19
- extensions: [RelayConnectionExtension],
20
18
  extras: [:lookahead]
19
+ }
20
+ opts[:extensions] = [RelayConnectionExtension] unless has_one?
21
+ field_type = has_one? ? @sideload_type : @sideload_type.connection_type
22
+ field = type.field @sideload.name,
23
+ field_type,
24
+ **opts
21
25
  ListArguments.new(@sideload.resource.class, @sideload).apply(field)
22
26
  _sideload = @sideload
23
27
  type.define_method(@sideload.name) do |**arguments|
24
28
  Util.is_readable_sideload!(_sideload)
25
29
  params = Util.params_from_args(arguments)
26
- pk = object.send(_sideload.primary_key)
27
- id = if _sideload.polymorphic_as
28
- hash = {}
29
- hash[_sideload.foreign_key] = pk
30
- hash[:"#{_sideload.polymorphic_as}_type"] = object.class.name
31
- id = hash
32
- else
33
- id = pk
34
- end
35
- Loaders::Many.factory(_sideload, params).load(id)
30
+ Loaders::Many.factory(_sideload, params).load(object)
36
31
  end
37
32
  end
38
33
 
39
34
  private
40
35
 
36
+ def has_one?
37
+ @sideload.type == :has_one
38
+ end
39
+
41
40
  def customized_edge?
42
41
  @sideload.type == :many_to_many && @sideload.class.edge_resource
43
42
  end
@@ -19,7 +19,9 @@ module GraphitiGql
19
19
  if _sideload.type == :has_one
20
20
  id = object.send(_sideload.primary_key)
21
21
  params = { filter: { _sideload.foreign_key => { eq: id } } }
22
- return _sideload.resource.class.all(params).data[0]
22
+
23
+ resource = Schema.registry.get(@sideload.resource.class)[:resource]
24
+ return resource.all(params).data[0]
23
25
  end
24
26
 
25
27
  lookahead = arguments[:lookahead]
@@ -31,7 +31,7 @@ module GraphitiGql
31
31
 
32
32
  def add_relationships
33
33
  each_relationship do |type, sideload_type, sideload|
34
- if [:has_many, :many_to_many].include?(sideload.type)
34
+ if [:has_many, :many_to_many, :has_one].include?(sideload.type)
35
35
  Fields::ToMany.new(sideload, sideload_type).apply(type)
36
36
  else
37
37
  Fields::ToOne.new(sideload, sideload_type).apply(type)
@@ -2,6 +2,21 @@ module GraphitiGql
2
2
  module SpecHelper
3
3
  extend ActiveSupport::Concern
4
4
 
5
+ module ScopeTrackable
6
+ def self.prepended(klass)
7
+ klass.class_eval do
8
+ class << self
9
+ attr_accessor :resolved_scope
10
+ end
11
+ end
12
+ end
13
+
14
+ def resolve(scope)
15
+ self.class.resolved_scope = scope
16
+ super
17
+ end
18
+ end
19
+
5
20
  included do
6
21
  extend Forwardable
7
22
  def_delegators :result,
@@ -11,13 +26,38 @@ module GraphitiGql
11
26
  :nodes,
12
27
  :stats
13
28
 
29
+ Graphiti::Resource.send(:prepend, ScopeTrackable)
30
+
14
31
  if defined?(RSpec)
15
32
  let(:params) { {} }
16
33
  let(:resource) { described_class }
17
34
  let(:ctx) { {} }
35
+ let(:only_fields) { [] }
36
+ let(:except_fields) { [] }
37
+
38
+ def self.only_fields(*fields)
39
+ let(:only_fields) { fields }
40
+ end
41
+
42
+ def self.except_fields(*fields)
43
+ let(:except_fields) { fields }
44
+ end
18
45
  end
19
46
  end
20
47
 
48
+ def fields
49
+ fields = []
50
+ resource.attributes.each_pair do |name, config|
51
+ (fields << name) if config[:readable]
52
+ end
53
+ if respond_to?(:only_fields) && only_fields.present?
54
+ fields.select! { |f| only_fields.include?(f) }
55
+ elsif respond_to?(:except_fields) && except_fields.present?
56
+ fields.reject! { |f| except_fields.include?(f) }
57
+ end
58
+ fields
59
+ end
60
+
21
61
  def gql_datetime(timestamp, precise = false)
22
62
  if precise
23
63
  timestamp.utc.round(10).iso8601(6)
@@ -26,9 +66,16 @@ module GraphitiGql
26
66
  end
27
67
  end
28
68
 
69
+ def proxy
70
+ resource.gql(params.merge(fields: fields), ctx)
71
+ end
72
+
73
+ def query
74
+ proxy.query
75
+ end
76
+
29
77
  def run
30
78
  lambda do
31
- proxy = resource.gql(params, ctx)
32
79
  proxy.to_h
33
80
  proxy
34
81
  end
@@ -1,3 +1,3 @@
1
1
  module GraphitiGql
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.5"
3
3
  end
data/lib/graphiti_gql.rb CHANGED
@@ -9,6 +9,7 @@ require "graphiti_gql/loaders/has_many"
9
9
  require "graphiti_gql/loaders/many_to_many"
10
10
  require "graphiti_gql/loaders/polymorphic_has_many"
11
11
  require "graphiti_gql/loaders/belongs_to"
12
+ require "graphiti_gql/loaders/has_one"
12
13
  require "graphiti_gql/response_shim"
13
14
  require "graphiti_gql/schema"
14
15
  require "graphiti_gql/schema/connection"
@@ -65,6 +66,9 @@ module GraphitiGql
65
66
  end
66
67
 
67
68
  def self.run(query_string, variables = {}, context = {})
69
+ if context.empty? && Graphiti.context[:object]
70
+ context = Graphiti.context[:object]
71
+ end
68
72
  Graphiti.with_context(context) do
69
73
  result = schema.execute query_string,
70
74
  variables: variables,
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.2
4
+ version: 0.2.5
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-06-26 00:00:00.000000000 Z
11
+ date: 2022-06-30 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: []
@@ -143,6 +143,7 @@ files:
143
143
  - lib/graphiti_gql/graphiti_hax.rb
144
144
  - lib/graphiti_gql/loaders/belongs_to.rb
145
145
  - lib/graphiti_gql/loaders/has_many.rb
146
+ - lib/graphiti_gql/loaders/has_one.rb
146
147
  - lib/graphiti_gql/loaders/many.rb
147
148
  - lib/graphiti_gql/loaders/many_to_many.rb
148
149
  - lib/graphiti_gql/loaders/polymorphic_has_many.rb
@@ -168,7 +169,7 @@ licenses:
168
169
  - MIT
169
170
  metadata:
170
171
  homepage_uri: https://www.graphiti.dev
171
- post_install_message:
172
+ post_install_message:
172
173
  rdoc_options: []
173
174
  require_paths:
174
175
  - lib
@@ -183,8 +184,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
183
184
  - !ruby/object:Gem::Version
184
185
  version: '0'
185
186
  requirements: []
186
- rubygems_version: 3.3.7
187
- signing_key:
187
+ rubygems_version: 3.0.3.1
188
+ signing_key:
188
189
  specification_version: 4
189
190
  summary: GraphQL support for Graphiti
190
191
  test_files: []