-
Notifications
You must be signed in to change notification settings - Fork 17
/
Loader.php
267 lines (233 loc) · 8.4 KB
/
Loader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
namespace WPGraphQL\Extensions\OffsetPagination;
use WPGraphQL\Data\Connection\AbstractConnectionResolver;
use WPGraphQL\Data\Connection\UserConnectionResolver;
class Loader
{
public static function init()
{
define('WP_GRAPHQL_OFFSET_PAGINATION', 'initialized');
(new Loader())->bind_hooks();
}
function bind_hooks()
{
add_action(
'graphql_register_types',
[$this, 'op_action_register_types'],
9,
0
);
add_filter(
'graphql_map_input_fields_to_wp_query',
[$this, 'op_filter_map_offset_to_wp_query_args'],
10,
2
);
add_filter(
'graphql_map_input_fields_to_wp_user_query',
[$this, 'op_filter_map_offset_to_wp_user_query_args'],
10,
2
);
add_filter(
'graphql_connection_page_info',
[$this, 'op_filter_graphql_connection_page_info'],
10,
2
);
add_filter(
'graphql_connection_query_args',
[$this, 'op_filter_graphql_connection_query_args'],
10,
5
);
add_filter(
'graphql_connection_amount_requested',
[$this, 'op_filter_graphql_connection_amount_requested'],
10,
2
);
}
function op_filter_graphql_connection_amount_requested($amount, $resolver)
{
if (self::is_offset_resolver($resolver)) {
return self::get_page_size($resolver);
}
return $amount;
}
/**
* Returns true when the resolver is resolving offset pagination
*/
static function get_page_size(AbstractConnectionResolver $resolver)
{
$args = $resolver->getArgs();
return intval($args['where']['offsetPagination']['size'] ?? 0);
}
static function is_offset_resolver(AbstractConnectionResolver $resolver)
{
$args = $resolver->getArgs();
return isset($args['where']['offsetPagination']);
}
/**
* Lazily enable total calculations only when they are asked in the
* selection set.
*/
function op_filter_graphql_connection_query_args(
$query_args,
AbstractConnectionResolver $resolver
) {
$info = $resolver->getInfo();
$selection_set = $info->getFieldSelection(2);
if (!isset($selection_set['pageInfo']['offsetPagination']['total'])) {
// get out if not requesting total counting
return $query_args;
}
if ($resolver instanceof UserConnectionResolver) {
// Enable slow total counting for user connections
$query_args['count_total'] = true;
} else {
// Enable slow total counting for posts connections
$query_args['no_found_rows'] = false;
}
return $query_args;
}
static function add_post_type_fields(\WP_Post_Type $post_type_object)
{
$type = ucfirst($post_type_object->graphql_single_name);
register_graphql_fields("RootQueryTo${type}ConnectionWhereArgs", [
'offsetPagination' => [
'type' => 'OffsetPagination',
'description' => "Paginate ${type}s with offsets",
],
]);
}
function op_filter_graphql_connection_page_info(
$page_info,
AbstractConnectionResolver $resolver
) {
$size = self::get_page_size($resolver);
$query = $resolver->get_query();
$args = $resolver->getArgs();
$offset = $args['where']['offsetPagination']['offset'] ?? 0;
$total = null;
if ($query instanceof \WP_Query) {
$total = $query->found_posts;
} elseif ($query instanceof \WP_User_Query) {
$total = $query->total_users;
}
$page_info['offsetPagination'] = [
'total' => $total,
'hasMore' => count($resolver->get_ids()) > $size,
'hasPrevious' => $offset > 0,
];
return $page_info;
}
function op_filter_map_offset_to_wp_query_args(
array $query_args,
array $where_args
) {
if (isset($where_args['offsetPagination']['offset'])) {
$query_args['offset'] = $where_args['offsetPagination']['offset'];
}
if (isset($where_args['offsetPagination']['size'])) {
// Fetch size+1 to be able calculate "hasMore" field without
// slowly counting full totals.
$query_args['posts_per_page'] =
intval($where_args['offsetPagination']['size']) + 1;
}
return $query_args;
}
function op_filter_map_offset_to_wp_user_query_args(
array $query_args,
array $where_args
) {
if (isset($where_args['offsetPagination']['offset'])) {
$query_args['offset'] = $where_args['offsetPagination']['offset'];
}
if (isset($where_args['offsetPagination']['size'])) {
$query_args['number'] =
intval($where_args['offsetPagination']['size']) + 1;
}
return $query_args;
}
function op_action_register_types()
{
foreach (\WPGraphQL::get_allowed_post_types() as $post_type) {
self::add_post_type_fields(get_post_type_object($post_type));
}
register_graphql_object_type('OffsetPaginationPageInfo', [
'description' => __(
'Get information about the offset pagination state',
'wp-graphql-offset-pagination'
),
'fields' => [
'total' => [
'type' => 'Int',
'description' => __(
'Total amount of nodes in this connection',
'wp-graphql-offset-pagination'
),
],
'hasMore' => [
'type' => 'Boolean',
'description' => __(
'True if there is one or more nodes available in this connection. Eg. you can increase the offset at least by one.',
'wp-graphql-offset-pagination'
),
],
'hasPrevious' => [
'type' => 'Boolean',
'description' => __(
'True when offset can be decresed eg. offset is 0<',
'wp-graphql-offset-pagination'
),
],
],
]);
register_graphql_field('WPPageInfo', 'offsetPagination', [
'type' => 'OffsetPaginationPageInfo',
'description' => __(
'Get information about the offset pagination state in the current connection',
'wp-graphql-offset-pagination'
),
]);
register_graphql_input_type('OffsetPagination', [
'description' => __(
'Offset pagination input type',
'wp-graphql-offet-pagination'
),
'fields' => [
'size' => [
'type' => 'Int',
'description' => __(
'Number of post to show per page. Passed to posts_per_page of WP_Query.',
'wp-graphql-offset-pagination'
),
],
'offset' => [
'type' => 'Int',
'description' => __(
'Number of post to show per page. Passed to posts_per_page of WP_Query.',
'wp-graphql-offset-pagination'
),
],
],
]);
register_graphql_field(
'RootQueryToContentNodeConnectionWhereArgs',
'offsetPagination',
[
'type' => 'OffsetPagination',
'description' => 'Paginate content nodes with offsets',
]
);
register_graphql_field(
'RootQueryToUserConnectionWhereArgs',
'offsetPagination',
[
'type' => 'OffsetPagination',
'description' => 'Paginate users with offsets',
]
);
}
}