graphql-connections 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a9c9ec5b3fb2eff5eb12781de11a726576630f03381b4d0d12f334bf1c0daad5
4
+ data.tar.gz: 57fc193b3559ae72cd6ffb75b14ceafe4e3e73371e09b6204f2ead111b9023a9
5
+ SHA512:
6
+ metadata.gz: 84d109d7e71943db1522c6d0beb12403a8c442dfd208e39048b175e9410f0d44cec75a1b77942fec35fcf1925532a6909ba706b80525c6d36c64f4d3bf426846
7
+ data.tar.gz: d5c7ebc8dd7d72b30069f619fedb548cf6b3828db686cefbe490d65b9ae0dd0e5454a83634386293d7c0a473969b93eccaf315d89740f061635046ff4ccba2f4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Misha Merkushin (aka bibendi)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,58 @@
1
+ [![Gem Version](https://badge.fury.io/rb/graphql-connections.svg)](https://badge.fury.io/rb/graphql-connections)
2
+ [![Build Status](https://travis-ci.org/bibendi/graphql-connections.svg?branch=master)](https://travis-ci.org/bibendi/graphql-connections)
3
+
4
+ # GraphQL::Connections
5
+
6
+ Cursor-based pagination to work with `ActiveRecord::Relation`s.
7
+
8
+ Implements [Relay specification](https://relay.dev/graphql/connections.htm) for serving stable connections based on column values.
9
+ If objects are created or destroyed during pagination, the list of items won’t be disrupted.
10
+
11
+ <a href="https://evilmartians.com/?utm_source=graphql-connections">
12
+ <img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54"></a>
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem "graphql-connections"
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ You can use a stable connection wrapper on a specific field:
25
+
26
+ ```ruby
27
+ field :messages, Types::Message.connection_type, null: false
28
+
29
+ def messages
30
+ GraphQL::Connections::Stable.new(Message.all)
31
+ end
32
+ ```
33
+
34
+ Or you can apply a stable connection to all Active Record relations returning by any field:
35
+
36
+ ```ruby
37
+ class ApplicationSchema < GraphQL::Schema
38
+ use GraphQL::Pagination::Connections
39
+
40
+ connections.add(ActiveRecord::Relation, GraphQL::Connections::Stable)
41
+ end
42
+ ```
43
+
44
+ **NOTE:** Don't use stable connections for relations whose ordering is too complicated for cursor generation.
45
+
46
+ ## Development
47
+
48
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
49
+
50
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bibendi/graphql-connections.
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+ require "graphql"
5
+
6
+ module GraphQL
7
+ module Connections
8
+ end
9
+ end
10
+
11
+ require "graphql/connections/stable"
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Connections
5
+ # Cursor-based pagination to work with `ActiveRecord::Relation`s.
6
+ # Implements a mechanism for serving stable connections based on column values.
7
+ # If objects are created or destroyed during pagination, the list of items won't be disrupted.
8
+ #
9
+ # Inspired by `GraphQL::Pro`s Stable Relation Connections
10
+ # https://graphql-ruby.org/pagination/stable_relation_connections.html
11
+ #
12
+ # For more information see GraphQL Cursor Connections Specification
13
+ # https://relay.dev/graphql/connections.htm
14
+ class Stable < ::GraphQL::Pagination::Connection
15
+ delegate :arel_table, to: :items
16
+ delegate :primary_key, to: "items.model"
17
+
18
+ def nodes
19
+ @nodes ||= limited_relation
20
+ end
21
+
22
+ def has_previous_page # rubocop:disable Naming/PredicateName
23
+ if last
24
+ nodes.any? && items.where(arel_table[primary_key].lt(nodes.first.id)).exists?
25
+ elsif after
26
+ items.where(arel_table[primary_key].lteq(after_cursor)).exists?
27
+ else
28
+ false
29
+ end
30
+ end
31
+
32
+ def has_next_page # rubocop:disable Naming/PredicateName
33
+ if first
34
+ nodes.any? && items.where(arel_table[primary_key].gt(nodes.last.id)).exists?
35
+ elsif before
36
+ items.where(arel_table[primary_key].gteq(before_cursor)).exists?
37
+ else
38
+ false
39
+ end
40
+ end
41
+
42
+ def cursor_for(item)
43
+ encode(item.id.to_s)
44
+ end
45
+
46
+ private
47
+
48
+ def limited_relation
49
+ scope = sliced_relation
50
+ nodes = []
51
+
52
+ if first
53
+ nodes |= scope.
54
+ reorder(arel_table[primary_key].asc).
55
+ limit(first).
56
+ to_a
57
+ end
58
+
59
+ if last
60
+ nodes |= scope.
61
+ reorder(arel_table[primary_key].desc).
62
+ limit(last).
63
+ to_a.reverse!
64
+ end
65
+
66
+ nodes
67
+ end
68
+
69
+ def sliced_relation
70
+ items.
71
+ yield_self { |s| after ? s.where(arel_table[primary_key].gt(after_cursor)) : s }.
72
+ yield_self { |s| before ? s.where(arel_table[primary_key].lt(before_cursor)) : s }
73
+ end
74
+
75
+ def after_cursor
76
+ @after_cursor ||= decode(after)
77
+ end
78
+
79
+ def before_cursor
80
+ @before_cursor ||= decode(before)
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQL
4
+ module Paging
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphql-connections
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Misha Merkushin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: graphql
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: combustion
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pg
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '13.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '13.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec-rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '4.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '4.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.58'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.58'
139
+ - !ruby/object:Gem::Dependency
140
+ name: test-prof
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.11'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.11'
153
+ description:
154
+ email:
155
+ - merkushin.m.s@gmail.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - LICENSE.txt
161
+ - README.md
162
+ - lib/graphql/connections.rb
163
+ - lib/graphql/connections/stable.rb
164
+ - lib/graphql/connections/version.rb
165
+ homepage: https://github.com/bibendi/graphql-connections
166
+ licenses:
167
+ - MIT
168
+ metadata:
169
+ allowed_push_host: https://rubygems.org
170
+ post_install_message:
171
+ rdoc_options: []
172
+ require_paths:
173
+ - lib
174
+ required_ruby_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ required_rubygems_version: !ruby/object:Gem::Requirement
180
+ requirements:
181
+ - - ">="
182
+ - !ruby/object:Gem::Version
183
+ version: '0'
184
+ requirements: []
185
+ rubygems_version: 3.1.2
186
+ signing_key:
187
+ specification_version: 4
188
+ summary: GraphQL cursor-based stable pagination to work with Active Record relations
189
+ test_files: []