graphql-page_cursors 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ff87f78e643f6c03c976c91e44d541ccfdb3f9dd1f45614bf9113e048f7125c2
4
+ data.tar.gz: 1d0ee957ef5f769cf006e06f88a89fd0481aa5842b6830d1d70dcc0c8fdedf3f
5
+ SHA512:
6
+ metadata.gz: 9334ae540c56fc0ee2cc9e928e1e17b45f65bf54660162746d1fab70e07a89b1cab4e21122a0d9526575366748765cfe0079b6146d38a2f2f3b517fb92107fcd
7
+ data.tar.gz: 00ec1948134ea669e920e57e33d958d2455980c445e51a3e78c68e6ee6d8c58b4606d11aab0ac2855cf55caae87006624e2a8fe8eaba3303da8b540cd90eadcf
@@ -0,0 +1,85 @@
1
+ version: 2
2
+
3
+ defaults: &defaults
4
+ working_directory: ~/graphql-page_cursors
5
+ docker:
6
+ - image: ruby:2.7.1
7
+
8
+ jobs:
9
+ build:
10
+ <<: *defaults
11
+ steps:
12
+ - checkout
13
+
14
+ - run:
15
+ name: Gem install bundler
16
+ command: gem update --system && gem install bundler
17
+
18
+ - restore_cache:
19
+ name: Restore bundler cache
20
+ key: bundler-{{ checksum "Gemfile.lock" }}
21
+
22
+ - run:
23
+ name: Bundle install
24
+ command: bundle install --jobs=4 --retry=3 --path vendor/bundle
25
+
26
+ - save_cache:
27
+ name: Save bundler cache
28
+ key: bundler-{{ checksum "Gemfile.lock" }}
29
+ paths:
30
+ - vendor/bundle
31
+
32
+ test:
33
+ <<: *defaults
34
+ steps:
35
+ - checkout
36
+
37
+ - run:
38
+ name: Gem install bundler
39
+ command: gem update --system && gem install bundler
40
+
41
+ - restore_cache:
42
+ name: Restore bundler cache
43
+ key: bundler-{{ checksum "Gemfile.lock" }}
44
+
45
+ - run:
46
+ name: Bundle install
47
+ command: bundle install --jobs=4 --retry=3 --path vendor/bundle
48
+
49
+ - run:
50
+ name: Run tests
51
+ command: bundle exec rake
52
+
53
+ deploy:
54
+ <<: *defaults
55
+ steps:
56
+ - checkout
57
+
58
+ - run:
59
+ name: Setup Rubygems
60
+ command: bash .circleci/setup-rubygems
61
+
62
+ - run:
63
+ name: Build gem
64
+ command: gem build graphql-page_cursors.gemspec --output graphql-page_cursors.gem
65
+
66
+ - run:
67
+ name: Publish gem
68
+ command: gem push graphql-page_cursors.gem
69
+
70
+ workflows:
71
+ version: 2
72
+ default:
73
+ jobs:
74
+ - build
75
+ - test:
76
+ requires:
77
+ - build
78
+ release:
79
+ jobs:
80
+ - deploy:
81
+ filters:
82
+ tags:
83
+ only: /^v.*/
84
+ branches:
85
+ ignore: /.*/
@@ -0,0 +1,2 @@
1
+ ---
2
+ :rubygems_api_key: REPLACE_ME
@@ -0,0 +1,6 @@
1
+ #! /bin/sh
2
+
3
+ mkdir -p ~/.gem
4
+ cp ./.circleci/gem_credentials ~/.gem/credentials
5
+ chmod 0600 ~/.gem/credentials
6
+ sed -i "s/REPLACE_ME/${RUBYGEMS_API_KEY}/g" ~/.gem/credentials
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --order random
@@ -0,0 +1,8 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ AllCops:
4
+ NewCops: enable
5
+ TargetRubyVersion: 2.7.1
6
+
7
+ Style/Documentation:
8
+ Enabled: false
@@ -0,0 +1,14 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2020-11-10 16:50:29 UTC using RuboCop version 1.2.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 1
10
+ # Configuration parameters: Include.
11
+ # Include: **/*.gemspec
12
+ Gemspec/RequiredRubyVersion:
13
+ Exclude:
14
+ - 'graphql-page_cursors.gemspec'
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in graphql-page_cursors.gemspec
8
+ gemspec
@@ -0,0 +1,58 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ graphql-page_cursors (0.0.1)
5
+ graphql
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.1)
11
+ diff-lcs (1.4.4)
12
+ graphql (1.11.6)
13
+ parallel (1.20.0)
14
+ parser (2.7.2.0)
15
+ ast (~> 2.4.1)
16
+ rainbow (3.0.0)
17
+ rake (13.0.1)
18
+ regexp_parser (1.8.2)
19
+ rexml (3.2.4)
20
+ rspec (3.10.0)
21
+ rspec-core (~> 3.10.0)
22
+ rspec-expectations (~> 3.10.0)
23
+ rspec-mocks (~> 3.10.0)
24
+ rspec-core (3.10.0)
25
+ rspec-support (~> 3.10.0)
26
+ rspec-expectations (3.10.0)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.10.0)
29
+ rspec-mocks (3.10.0)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.10.0)
32
+ rspec-support (3.10.0)
33
+ rubocop (1.2.0)
34
+ parallel (~> 1.10)
35
+ parser (>= 2.7.1.5)
36
+ rainbow (>= 2.2.2, < 4.0)
37
+ regexp_parser (>= 1.8)
38
+ rexml
39
+ rubocop-ast (>= 1.0.1)
40
+ ruby-progressbar (~> 1.7)
41
+ unicode-display_width (>= 1.4.0, < 2.0)
42
+ rubocop-ast (1.1.1)
43
+ parser (>= 2.7.1.5)
44
+ ruby-progressbar (1.10.1)
45
+ unicode-display_width (1.7.0)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ bundler
52
+ graphql-page_cursors!
53
+ rake
54
+ rspec
55
+ rubocop
56
+
57
+ BUNDLED WITH
58
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Jon Allured
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,39 @@
1
+ # Graphql::PageCursors
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/graphql/page_cursors`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'graphql-page_cursors'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install graphql-page_cursors
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jonallured/graphql-page_cursors.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+
7
+ desc 'Run RuboCop'
8
+ RuboCop::RakeTask.new(:rubocop)
9
+
10
+ RSpec::Core::RakeTask.new(:spec)
11
+
12
+ task default: %i[rubocop spec]
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'graphql/page_cursors'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,16 @@
1
+ #! /bin/sh
2
+ set -ex
3
+
4
+ old_version=$1
5
+ new_version=$2
6
+
7
+ version_files="lib/page_cursors/version.rb"
8
+ commit_message="Bumping version numbers for $new_version"
9
+ tag_message="Tagging version $new_version"
10
+
11
+ sed -i "" "s/$old_version/$new_version/g" $version_files
12
+ git add .
13
+ git commit --message "$commit_message"
14
+ git push upstream main
15
+ git tag --annotate v$new_version --message "$tag_message"
16
+ git push upstream --tags
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'page_cursors/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'graphql-page_cursors'
9
+ spec.version = PageCursors::VERSION
10
+ spec.authors = ['Jon Allured']
11
+ spec.email = ['jon.allured@gmail.com']
12
+
13
+ spec.summary = 'Add page cursors to your Rails GQL schema'
14
+ spec.description = 'Add page cursors to your Rails GQL schema'
15
+ spec.homepage = 'https://github.com/artsy/graphql-page_cursors'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'graphql'
26
+
27
+ spec.add_development_dependency 'bundler'
28
+ spec.add_development_dependency 'rake'
29
+ spec.add_development_dependency 'rspec'
30
+ spec.add_development_dependency 'rubocop'
31
+ end
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'graphql'
4
+
5
+ module GraphQL
6
+ class PageCursor < GraphQL::Schema::Object
7
+ field :cursor, String, 'first cursor on the page', null: false
8
+ field :is_current, Boolean, 'is this the current page?', null: false
9
+ field :page, Int, 'page number out of totalPages', null: false
10
+ end
11
+
12
+ class PageCursors < GraphQL::Schema::Object
13
+ field :first, PageCursor, 'optional, may be included in field around', null: true
14
+ field :last, PageCursor, 'optional, may be included in field around', null: true
15
+ field :around, [PageCursor], null: false
16
+ field :previous, PageCursor, null: true
17
+ end
18
+
19
+ class PageCursorConnection < GraphQL::Types::Relay::BaseConnection
20
+ field :page_cursors, PageCursors, null: true
21
+ field :total_pages, Int, null: true
22
+ field :total_count, Int, null: true
23
+
24
+ MAX_CURSOR_COUNT = 5
25
+
26
+ def page_cursors
27
+ return if total_pages <= 1
28
+
29
+ {
30
+ around: around_cursors,
31
+ first: first_cursor,
32
+ last: last_cursor,
33
+ previous: previous_cursor
34
+ }.compact
35
+ end
36
+
37
+ def total_pages
38
+ return 0 if object.items.size.zero?
39
+ return 1 if nodes_per_page.nil?
40
+
41
+ (object.items.size.to_f / nodes_per_page).ceil
42
+ end
43
+
44
+ def total_count
45
+ object.items.size
46
+ end
47
+
48
+ private
49
+
50
+ def around_cursors
51
+ around_page_numbers.map { |page_num| page_cursor(page_num) }
52
+ end
53
+
54
+ def first_cursor
55
+ exceeds_max_cursor_count = total_pages > MAX_CURSOR_COUNT
56
+ include_first_cursor =
57
+ exceeds_max_cursor_count && around_page_numbers.exclude?(1)
58
+ return unless include_first_cursor
59
+
60
+ page_cursor(1)
61
+ end
62
+
63
+ def last_cursor
64
+ exceeds_max_cursor_count = total_pages > MAX_CURSOR_COUNT
65
+ include_last_cursor =
66
+ exceeds_max_cursor_count && around_page_numbers.exclude?(total_pages)
67
+ return unless include_last_cursor
68
+
69
+ page_cursor(total_pages)
70
+ end
71
+
72
+ def previous_cursor
73
+ include_previous_cursor = current_page > 1
74
+ return unless include_previous_cursor
75
+
76
+ page_cursor(current_page - 1)
77
+ end
78
+
79
+ def page_cursor(page_num)
80
+ {
81
+ cursor: cursor_for_page(page_num),
82
+ is_current: current_page == page_num,
83
+ page: page_num
84
+ }
85
+ end
86
+
87
+ def cursor_for_page(page_num)
88
+ return '' if page_num == 1
89
+
90
+ after_cursor = (page_num - 1) * nodes_per_page
91
+ encode(after_cursor.to_s)
92
+ end
93
+
94
+ def current_page
95
+ nodes_before / nodes_per_page + 1
96
+ end
97
+
98
+ def around_page_numbers # rubocop:disable Metrics/AbcSize
99
+ if total_pages <= MAX_CURSOR_COUNT
100
+ (1..total_pages).to_a
101
+ elsif current_page <= 3
102
+ (1..4).to_a
103
+ elsif current_page >= total_pages - 2
104
+ ((total_pages - 3)..total_pages).to_a
105
+ else
106
+ [current_page - 1, current_page, current_page + 1]
107
+ end
108
+ end
109
+
110
+ def nodes_before
111
+ node_offset(object.edge_nodes.first) - 1
112
+ end
113
+
114
+ def nodes_after
115
+ node_offset(object.edge_nodes.last)
116
+ end
117
+
118
+ def node_offset(node)
119
+ # this was previously accomplished by calling a private method:
120
+ # object.send(:offset_from_cursor, object.cursor_from_node(object.edge_nodes.first))
121
+ object.items.index(node) + 1
122
+ end
123
+
124
+ def nodes_per_page
125
+ object.first || object.last
126
+ end
127
+
128
+ # borrowed from https://graphql-ruby.org/api-doc/1.10.6/Base64Bp.html
129
+ def encode(value)
130
+ str = Base64.strict_encode64(value)
131
+ str.tr!('+/', '-_')
132
+ str.delete!('=')
133
+ str
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class PageCursors
4
+ VERSION = '0.0.1'
5
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphql-page_cursors
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jon Allured
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-11-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: graphql
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Add page cursors to your Rails GQL schema
84
+ email:
85
+ - jon.allured@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".circleci/config.yml"
91
+ - ".circleci/gem_credentials"
92
+ - ".circleci/setup-rubygems"
93
+ - ".gitignore"
94
+ - ".rspec"
95
+ - ".rubocop.yml"
96
+ - ".rubocop_todo.yml"
97
+ - Gemfile
98
+ - Gemfile.lock
99
+ - LICENSE.txt
100
+ - README.md
101
+ - Rakefile
102
+ - bin/console
103
+ - bin/release
104
+ - bin/setup
105
+ - graphql-page_cursors.gemspec
106
+ - lib/graphql/page_cursors.rb
107
+ - lib/page_cursors/version.rb
108
+ homepage: https://github.com/artsy/graphql-page_cursors
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubygems_version: 3.1.2
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Add page cursors to your Rails GQL schema
131
+ test_files: []