graphql-connections 0.1.1 → 0.2.0

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: faf6bee6dc175eb574286710382830d67b0bafdc8c0c23b78606781987bff57f
4
- data.tar.gz: 78dfb742ac57250f12d80505225ed739fcde8fa35056f6338d673b04ae8c781f
3
+ metadata.gz: 17bf53a70dde489ced3c574a122728fd47122369a7e50ef8d45f5cf9c8743a09
4
+ data.tar.gz: 4f0b7158004251fced3b9989a0b0e1251e79f93208372b6c12aa60003816defd
5
5
  SHA512:
6
- metadata.gz: 74ce335eae78448a120893aa47700e2766b8c186f2be45dd13adf63d9c00269d52f0c47ca487f216dac83b55ff9dfe7a045675543e0d2c3de3a9149f1e5d0a8a
7
- data.tar.gz: 37c6bd839df707fb469d19519c7b9e07f33825bfc5802bf5ae4325b44e6f51d4cb2327627ed7f0c2e2b74dfa82a5cc3554071820603188dcdae0abd3598b500a
6
+ metadata.gz: 4d4e4beaa0f3124dfbfe6d99cfe1e911b38f24aee3fe6072aa69e38b9303042748f0ca18e4df973e67895901cf15978d5aabe4ad02396ae531cd1e1024df87d9
7
+ data.tar.gz: a981adbe63d036bf326a8ab6c0bd6a2fdf1b071f003f6665fab8302c48c9e69a1679353238c33d01f4a3469c3ae39363a2247b02bec703206ef4f30e457c8761
data/README.md CHANGED
@@ -31,6 +31,18 @@ def messages
31
31
  end
32
32
  ```
33
33
 
34
+ Records are sorted by model's primary key by default. You can change this behaviour by providing `primary_key` param:
35
+
36
+ ```ruby
37
+ GraphQL::Connections::Stable.new(Message.all, primary_key: :created_at)
38
+ ```
39
+
40
+ Also, you can disable opaque cursors by setting `opaque_cursor` param:
41
+
42
+ ```ruby
43
+ GraphQL::Connections::Stable.new(Message.all, opaque_cursor: false)
44
+ ```
45
+
34
46
  Or you can apply a stable connection to all Active Record relations returning by any field:
35
47
 
36
48
  ```ruby
@@ -12,8 +12,20 @@ module GraphQL
12
12
  # For more information see GraphQL Cursor Connections Specification
13
13
  # https://relay.dev/graphql/connections.htm
14
14
  class Stable < ::GraphQL::Pagination::Connection
15
+ attr_reader :opaque_cursor
16
+
15
17
  delegate :arel_table, to: :items
16
- delegate :primary_key, to: "items.model"
18
+
19
+ def initialize(*args, primary_key: nil, opaque_cursor: true, **kwargs)
20
+ @primary_key = primary_key
21
+ @opaque_cursor = opaque_cursor
22
+
23
+ super(*args, **kwargs)
24
+ end
25
+
26
+ def primary_key
27
+ @primary_key ||= items.model.primary_key
28
+ end
17
29
 
18
30
  def nodes
19
31
  @nodes ||= limited_relation
@@ -21,7 +33,7 @@ module GraphQL
21
33
 
22
34
  def has_previous_page # rubocop:disable Naming/PredicateName
23
35
  if last
24
- nodes.any? && items.where(arel_table[primary_key].lt(nodes.first.id)).exists?
36
+ nodes.any? && items.where(arel_table[primary_key].lt(nodes.first[primary_key])).exists?
25
37
  elsif after
26
38
  items.where(arel_table[primary_key].lteq(after_cursor)).exists?
27
39
  else
@@ -31,7 +43,7 @@ module GraphQL
31
43
 
32
44
  def has_next_page # rubocop:disable Naming/PredicateName
33
45
  if first
34
- nodes.any? && items.where(arel_table[primary_key].gt(nodes.last.id)).exists?
46
+ nodes.any? && items.where(arel_table[primary_key].gt(nodes.last[primary_key])).exists?
35
47
  elsif before
36
48
  items.where(arel_table[primary_key].gteq(before_cursor)).exists?
37
49
  else
@@ -40,11 +52,22 @@ module GraphQL
40
52
  end
41
53
 
42
54
  def cursor_for(item)
43
- encode(item.id.to_s)
55
+ cursor = serialize(item[primary_key])
56
+ cursor = encode(cursor) if opaque_cursor
57
+ cursor
44
58
  end
45
59
 
46
60
  private
47
61
 
62
+ def serialize(cursor)
63
+ case cursor
64
+ when Time, DateTime, Date
65
+ cursor.iso8601
66
+ else
67
+ cursor.to_s
68
+ end
69
+ end
70
+
48
71
  def limited_relation
49
72
  scope = sliced_relation
50
73
  nodes = []
@@ -73,11 +96,11 @@ module GraphQL
73
96
  end
74
97
 
75
98
  def after_cursor
76
- @after_cursor ||= decode(after)
99
+ @after_cursor ||= opaque_cursor ? decode(after) : after
77
100
  end
78
101
 
79
102
  def before_cursor
80
- @before_cursor ||= decode(before)
103
+ @before_cursor ||= opaque_cursor ? decode(before) : before
81
104
  end
82
105
  end
83
106
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module GraphQL
4
4
  module Paging
5
- VERSION = "0.1.1"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-connections
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Misha Merkushin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-06 00:00:00.000000000 Z
11
+ date: 2020-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -173,9 +173,9 @@ require_paths:
173
173
  - lib
174
174
  required_ruby_version: !ruby/object:Gem::Requirement
175
175
  requirements:
176
- - - ">="
176
+ - - ">"
177
177
  - !ruby/object:Gem::Version
178
- version: '0'
178
+ version: '2.5'
179
179
  required_rubygems_version: !ruby/object:Gem::Requirement
180
180
  requirements:
181
181
  - - ">="