graphiti_gql 0.2.2 → 0.2.3

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: 5d9dda0537212e47943587af28d28226e8ee6a4ad0d91b65f5074003ab370f17
4
+ data.tar.gz: 222fb453e4f73c6e4d0de606bae88aea041b23c48ba28355f575025dff7e3c92
5
5
  SHA512:
6
- metadata.gz: becebe36d5aabea0a75ba3e617f9f62d0830d3f4a35e5becdacef1fbe10f4991621f061521f10bc4c0e527bdc834d5380e9f38ff0b9cc2388a66a7cf25bb33b8
7
- data.tar.gz: 37a9a4221261f112100e32f06832a948c30cbef5a06ffcb786f080538c1b9f3894bd3f7f8fc32394f0c2cb122e94c1b1e58cd6b8c38b6e0feab08a30d55bbfb5
6
+ metadata.gz: 8e5a6a8d98b058fb8237750190e6f051712d2f90f5bf49be615f68a594598589afb1811aec0f0fbe79bbcefd457d27a6cf78e84e8e90d539168da9ffd13b9a64
7
+ data.tar.gz: 6515ba7accff3edb9490028bc3545f649d2a33877f5d6eb77a32a9183e365241be56b3ba6b99b1c27095390c110ee3c3f7d6743844049dab6576461b22a64c7c
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)
@@ -1,3 +1,3 @@
1
1
  module GraphitiGql
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
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.3
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-06-26 00:00:00.000000000 Z
11
+ date: 2022-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql
@@ -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