graphqr 0.0.1 → 0.0.2

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
  SHA1:
3
- metadata.gz: c0fb5f222bd481b018d41ea5033bfa5402c5bbec
4
- data.tar.gz: 4ba76746f0322bba9067fef162459c54dfa5340e
3
+ metadata.gz: 30f68dd1bcb6907786a2bd497b056fc44e5cd5ff
4
+ data.tar.gz: 33e51b0fe16f514a13afe9be920abd2f1adf4c5b
5
5
  SHA512:
6
- metadata.gz: 518e94ce07c32774355fb55d1ca6a3b42c685685e942e6ca657eea13dec850c5c763f19c213cf75b4a976f97667fbdc0099c1406bd08ff880f3acc1616d77a30
7
- data.tar.gz: 3693a00b845f41176bc1606461f72dbc09236fec9361759b6f5092b08a2ac2eec0af5cb818192b52f6c37b66969b34f6d78189d11e1c1b6522c36c8d22219b49
6
+ metadata.gz: d7b24cac875d94516b5dd8f08c69f4b6086310127acaa50daf0baf15427b9ad3c69edd3c711fbb7fb86f641ca0a94d7ce06260c0e5263a1ccdff25e62ef157b8
7
+ data.tar.gz: 3635d74b54fddd9b2753cd7d21872f4be500b4400060d899783b7c6d7ce88b09e50be1fbe56ce2c467c7fb3d715b68af276eedfd760a03f84bd283039a8eb257
data/README.md CHANGED
@@ -20,6 +20,8 @@ Or install it yourself as:
20
20
 
21
21
  $ gem install graphqr
22
22
 
23
+ If you'd like to use the pagination feature, you must have `pagy` installed.
24
+
23
25
  ## Usage
24
26
 
25
27
  TODO: Write usage instructions here
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQR # rubocop:disable Style/Documentation
4
+ ##
5
+ # TODO: add documentation
6
+ class Configuration
7
+ def configure
8
+ yield self
9
+ end
10
+
11
+ # Returns the selected paginator
12
+ def paginator
13
+ if instance_variable_defined? :@paginator
14
+ @paginator
15
+ else
16
+ set_paginator
17
+ end
18
+ end
19
+
20
+ ##
21
+ # Sets the preferred paginator
22
+ # TODO: support more than Pagy
23
+ def paginator=(paginator)
24
+ case paginator.to_sym
25
+ when :pagy
26
+ use_pagy
27
+ else
28
+ raise StandardError, "Unknown paginator: #{paginator}"
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def set_paginator
35
+ use_pagy if defined?(Pagy)
36
+ end
37
+ end
38
+
39
+ class << self
40
+ def configure
41
+ yield config
42
+ end
43
+
44
+ def config
45
+ @config ||= Configuration.new # rubocop:disable ThreadSafety/InstanceVariableInClassMethod
46
+ end
47
+ alias configuration config
48
+ end
49
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'pagy'
5
+ rescue LoadError
6
+ Kernel.warn 'Pagy not found'
7
+ end
@@ -5,6 +5,8 @@ module GraphQR
5
5
  ##
6
6
  # TODO: add documentation
7
7
  class PaginationExtension < GraphQL::Schema::FieldExtension
8
+ DEFAULT_PAGINATION_ERROR = 'No paginator defined'
9
+
8
10
  def apply
9
11
  field.argument :per, 'Int', required: false, default_value: 25,
10
12
  description: 'The requested number of nodes for the page'
@@ -21,7 +23,9 @@ module GraphQR
21
23
  end
22
24
 
23
25
  def after_resolve(value:, arguments:, **_kwargs)
24
- PaginationResolver.new(value, items: arguments[:per], page: arguments[:page])
26
+ raise GraphQL::ExecutionError, DEFAULT_PAGINATION_ERROR unless GraphQR.paginator.present?
27
+
28
+ Resolvers::PagyResolver.new(value, items: arguments[:per], page: arguments[:page]) if GraphQR.use_pagy?
25
29
  end
26
30
  end
27
31
  end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphQR
4
+ module Pagination
5
+ module Resolvers
6
+ ##
7
+ # TODO: add documentation
8
+ class PagyResolver
9
+ include Pagy::Backend
10
+
11
+ def initialize(records, arguments)
12
+ @records = records
13
+ @arguments = arguments
14
+
15
+ @pagy, paginated_records = pagy(records, arguments)
16
+ @paginated_records = paginated_records.to_a
17
+ end
18
+
19
+ def cursor_from_node(item)
20
+ item.to_global_id.to_s
21
+ end
22
+
23
+ def edge_nodes
24
+ @paginated_records
25
+ end
26
+
27
+ def nodes
28
+ @paginated_records
29
+ end
30
+
31
+ def edges
32
+ @paginated_records
33
+ end
34
+
35
+ def page_info
36
+ @pagy
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GraphQR
4
- VERSION = '0.0.1'
4
+ VERSION = '0.0.2'
5
5
  end
data/lib/graphqr.rb CHANGED
@@ -1,30 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'graphql'
4
- require 'pagy'
5
4
 
6
5
  ##
7
6
  # This module represents all the extensions we made to the graphql-ruby library
8
7
  # it contains helpers and integrations we need to keep our workflow as simple as possible.
9
8
  module GraphQR
10
9
  class << self
11
- # Switches GraphQR on or off
12
- def enabled=(value)
13
- GraphQR.config.enabled = value
10
+ def paginator
11
+ GraphQR.config.paginator
14
12
  end
15
13
 
16
- # Returns if GraphQR is turned on
17
- def enabled?
18
- GraphQR.config.enabled.present?
14
+ def use_pagy?
15
+ paginator == :pagy
19
16
  end
20
17
  end
21
18
  end
22
19
 
23
- require 'graphqr/fields/base_field'
20
+ require 'graphqr/hooks'
24
21
 
22
+ require 'graphqr/fields/base_field'
25
23
  require 'graphqr/pagination/types/pagination_page_info_type'
26
24
  require 'graphqr/pagination/pagination_extension'
27
- require 'graphqr/pagination/pagination_resolver'
25
+
26
+ begin
27
+ require 'graphqr/pagination/resolvers/pagy_resolver'
28
+ rescue NameError
29
+ Kernel.warn 'Pagy not found'
30
+ end
28
31
 
29
32
  require 'graphqr/apply_scopes'
30
33
  require 'graphqr/authorized'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphqr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manuel Puyol
@@ -32,26 +32,6 @@ dependencies:
32
32
  - - "<"
33
33
  - !ruby/object:Gem::Version
34
34
  version: '2'
35
- - !ruby/object:Gem::Dependency
36
- name: pagy
37
- requirement: !ruby/object:Gem::Requirement
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- version: '3'
42
- - - "<"
43
- - !ruby/object:Gem::Version
44
- version: '4'
45
- type: :runtime
46
- prerelease: false
47
- version_requirements: !ruby/object:Gem::Requirement
48
- requirements:
49
- - - ">="
50
- - !ruby/object:Gem::Version
51
- version: '3'
52
- - - "<"
53
- - !ruby/object:Gem::Version
54
- version: '4'
55
35
  - !ruby/object:Gem::Dependency
56
36
  name: bundler
57
37
  requirement: !ruby/object:Gem::Requirement
@@ -165,10 +145,12 @@ files:
165
145
  - lib/graphqr/apply_scopes.rb
166
146
  - lib/graphqr/authorized.rb
167
147
  - lib/graphqr/base.rb
148
+ - lib/graphqr/configuration.rb
168
149
  - lib/graphqr/fields/base_field.rb
150
+ - lib/graphqr/hooks.rb
169
151
  - lib/graphqr/pagination.rb
170
152
  - lib/graphqr/pagination/pagination_extension.rb
171
- - lib/graphqr/pagination/pagination_resolver.rb
153
+ - lib/graphqr/pagination/resolvers/pagy_resolver.rb
172
154
  - lib/graphqr/pagination/types/pagination_page_info_type.rb
173
155
  - lib/graphqr/permitted_fields_extension.rb
174
156
  - lib/graphqr/query_field.rb
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module GraphQR
4
- module Pagination
5
- ##
6
- # TODO: add documentation
7
- class PaginationResolver
8
- include Pagy::Backend
9
-
10
- def initialize(records, arguments)
11
- @records = records
12
- @arguments = arguments
13
-
14
- @pagy, paginated_records = pagy(records, arguments)
15
- @paginated_records = paginated_records.to_a
16
- end
17
-
18
- def cursor_from_node(item)
19
- item.to_global_id.to_s
20
- end
21
-
22
- def edge_nodes
23
- @paginated_records
24
- end
25
-
26
- def nodes
27
- @paginated_records
28
- end
29
-
30
- def edges
31
- @paginated_records
32
- end
33
-
34
- def page_info
35
- @pagy
36
- end
37
- end
38
- end
39
- end