graphql-connections 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +12 -0
- data/lib/graphql/connections/stable.rb +29 -6
- data/lib/graphql/connections/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 17bf53a70dde489ced3c574a122728fd47122369a7e50ef8d45f5cf9c8743a09
|
4
|
+
data.tar.gz: 4f0b7158004251fced3b9989a0b0e1251e79f93208372b6c12aa60003816defd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
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
|
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
|
-
|
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
|
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.
|
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
|
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: '
|
178
|
+
version: '2.5'
|
179
179
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
180
180
|
requirements:
|
181
181
|
- - ">="
|