graphqr 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/graphqr.rb +1 -0
- data/lib/graphqr/base_resolver.rb +2 -3
- data/lib/graphqr/base_resolvers.rb +41 -2
- data/lib/graphqr/pagination/resolvers/pagy_resolver.rb +4 -1
- data/lib/graphqr/pagination/resolvers/record_page_number_resolver.rb +29 -0
- data/lib/graphqr/pagination/types/pagination_page_info_type.rb +1 -0
- data/lib/graphqr/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 56b827169ae4fc8970f688a1405c1cfce763fdb7619e2b5d64af996ae983de74
|
4
|
+
data.tar.gz: 5c6b08ea0a203fb0a3e65c7ccef3cdcf511355715fd38315ae50f536d0dcee83
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6abd868b81b35aaae90b89544e66f0a8a33e75f468ed9004e0a102d331be8e7ebead88092db3dbb6bb17edc1d1b8e1de282fa64d1e597a96af069e57379a6ffe
|
7
|
+
data.tar.gz: 5c52447a2c4df95b8a14f3caed8b84170667302381ee0e3b8d071f4aa38b06a3d2dd5446a2c7a3be07ceaeeab04d08a49984033d4dd85e0f6bec04784d5f7029
|
data/lib/graphqr.rb
CHANGED
@@ -37,6 +37,7 @@ end
|
|
37
37
|
require 'graphqr/hooks'
|
38
38
|
|
39
39
|
require 'graphqr/fields/base_field'
|
40
|
+
require 'graphqr/pagination/resolvers/record_page_number_resolver'
|
40
41
|
require 'graphqr/pagination/types/pagination_page_info_type'
|
41
42
|
require 'graphqr/pagination/pagination_extension'
|
42
43
|
|
@@ -2,10 +2,9 @@
|
|
2
2
|
|
3
3
|
module GraphQR
|
4
4
|
##
|
5
|
-
#
|
5
|
+
# This is the base class for all resolvers defined by the `GraphQR` gem.
|
6
|
+
# It includes authorization and scoping extensions defined by the gem.
|
6
7
|
class BaseResolver < GraphQL::Schema::Resolver
|
7
|
-
##
|
8
|
-
# @TODO doc
|
9
8
|
include GraphQR::ApplyScopes
|
10
9
|
include GraphQR::Policies::AuthorizeGraphQL
|
11
10
|
end
|
@@ -2,10 +2,34 @@
|
|
2
2
|
|
3
3
|
module GraphQR
|
4
4
|
##
|
5
|
-
#
|
5
|
+
# The `BaseResolvers` module defines methods used by other extensions to define resolver classes.
|
6
|
+
# All resolvers defined by this module's methods inherit from `GraphQR::BaseResolver`.
|
6
7
|
module BaseResolvers
|
7
8
|
##
|
8
|
-
#
|
9
|
+
# The method defines and returns a resolver class meant for resolving a paginated ActiveRecordRelation.
|
10
|
+
# The returned class implements authorization, running the `PolicyProvider`'s' `index?` action
|
11
|
+
# and `authorized_records` scope.
|
12
|
+
#
|
13
|
+
# The defined resolver does not implement `#unscoped_collection`. Define it before adding the query to the schema**
|
14
|
+
#
|
15
|
+
# ### Params:
|
16
|
+
#
|
17
|
+
# +type_class+: the `GraphQL::Schema::Object` of the ActiveRecordRelation
|
18
|
+
#
|
19
|
+
# +scope_class+: a `GraphQL::Schema::InputObject` which defines arguments to be used by `ApplyScopes`
|
20
|
+
# #### Example:
|
21
|
+
# ```
|
22
|
+
# class ObjectScope < GraphQL::Schema::InputObject
|
23
|
+
# argument :with_relation_id, ID, required: false
|
24
|
+
# ...
|
25
|
+
# end
|
26
|
+
# ```
|
27
|
+
#
|
28
|
+
# ### Example:
|
29
|
+
#
|
30
|
+
# ```
|
31
|
+
# base_collection_resolver(ObjectType, ObjectScope)
|
32
|
+
# ```
|
9
33
|
def base_collection_resolver(type_class, scope_class)
|
10
34
|
Class.new(GraphQR::BaseResolver) do
|
11
35
|
type type_class.pagination_type, null: false
|
@@ -25,6 +49,21 @@ module GraphQR
|
|
25
49
|
end
|
26
50
|
end
|
27
51
|
|
52
|
+
##
|
53
|
+
# The method defines and returns a resolver class meant for resolving a single ActiveRecord
|
54
|
+
# The returned class implements authorization, running the `PolicyProvider`'s' `show`
|
55
|
+
#
|
56
|
+
# The defined resolver does not implement `#record`. Define it before adding the query to the schema**
|
57
|
+
#
|
58
|
+
# ### Params:
|
59
|
+
#
|
60
|
+
# +type_class+: the `GraphQL::Schema::Object` of the ActiveRecord
|
61
|
+
#
|
62
|
+
# ### Example:
|
63
|
+
#
|
64
|
+
# ```
|
65
|
+
# base_resource_resolver(ObjectType)
|
66
|
+
# ```
|
28
67
|
def base_resource_resolver(type_class)
|
29
68
|
Class.new(GraphQR::BaseResolver) do
|
30
69
|
type type_class, null: false
|
@@ -33,7 +33,10 @@ module GraphQR
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def page_info
|
36
|
-
@pagy
|
36
|
+
@pagy.tap do |pagy|
|
37
|
+
pagy.class_eval { attr_accessor :ordered_record_ids }
|
38
|
+
pagy.ordered_record_ids = @records&.all? { |r| r&.respond_to?(:id) } ? @records.map(&:id) : []
|
39
|
+
end
|
37
40
|
end
|
38
41
|
end
|
39
42
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GraphQR
|
4
|
+
module Pagination
|
5
|
+
module Resolvers
|
6
|
+
##
|
7
|
+
# This is a resolver that receives the id of an object and returns the page number
|
8
|
+
# in which the object is located.
|
9
|
+
class RecordPageNumberResolver < ::GraphQL::Schema::Resolver
|
10
|
+
INDEX_OFFSET = 1
|
11
|
+
|
12
|
+
type Int, null: true
|
13
|
+
|
14
|
+
argument :record_id, ID, required: true
|
15
|
+
|
16
|
+
def resolve(record_id:)
|
17
|
+
per_page = object.vars[:items]
|
18
|
+
records_ids = object.ordered_record_ids
|
19
|
+
record_index = records_ids.find_index(record_id.to_i)
|
20
|
+
|
21
|
+
return if per_page.zero? || records_ids.blank? || record_index.blank?
|
22
|
+
|
23
|
+
record_position = (record_index + INDEX_OFFSET).to_f
|
24
|
+
(record_position / per_page).ceil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -25,6 +25,7 @@ module GraphQR
|
|
25
25
|
field :next_page, Int, null: true,
|
26
26
|
method: :next,
|
27
27
|
description: 'The previous page number or nil if there is no previous page'
|
28
|
+
field :record_page_number, resolver: GraphQR::Pagination::Resolvers::RecordPageNumberResolver
|
28
29
|
end
|
29
30
|
end
|
30
31
|
end
|
data/lib/graphqr/version.rb
CHANGED
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.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Manuel Puyol
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2020-02-04 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: graphql
|
@@ -152,6 +152,7 @@ files:
|
|
152
152
|
- lib/graphqr/pagination.rb
|
153
153
|
- lib/graphqr/pagination/pagination_extension.rb
|
154
154
|
- lib/graphqr/pagination/resolvers/pagy_resolver.rb
|
155
|
+
- lib/graphqr/pagination/resolvers/record_page_number_resolver.rb
|
155
156
|
- lib/graphqr/pagination/types/pagination_page_info_type.rb
|
156
157
|
- lib/graphqr/permitted_fields_extension.rb
|
157
158
|
- lib/graphqr/policies/authorize_graphql.rb
|
@@ -181,8 +182,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
181
182
|
- !ruby/object:Gem::Version
|
182
183
|
version: '0'
|
183
184
|
requirements: []
|
184
|
-
|
185
|
-
rubygems_version: 2.6.14
|
185
|
+
rubygems_version: 3.0.6
|
186
186
|
signing_key:
|
187
187
|
specification_version: 4
|
188
188
|
summary: Extensions and helpers for graphql-ruby
|