Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ function bind_hooks()
2
);

add_filter(
'graphql_map_input_fields_to_wp_comment_query',
[$this, 'op_filter_map_offset_to_wp_comment_query_args'],
10,
2
);

add_filter(
'graphql_connection_page_info',
[$this, 'op_filter_graphql_connection_page_info'],
Expand Down Expand Up @@ -135,6 +142,8 @@ function op_filter_graphql_connection_page_info(
$total = $query->found_posts;
} elseif ($query instanceof \WP_User_Query) {
$total = $query->total_users;
} else if($query instanceof \WP_Comment_Query) {
$total = $query->found_comments;
}

$page_info['offsetPagination'] = [
Expand Down Expand Up @@ -179,12 +188,38 @@ function op_filter_map_offset_to_wp_user_query_args(
return $query_args;
}

function op_filter_map_offset_to_wp_comment_query_args(
$query_args,
$where_args
) {
if ( isset( $where_args['offsetPagination']['offset'] ) ) {
$query_args['offset'] = $where_args['offsetPagination']['offset'];
}

if ( isset( $where_args['offsetPagination']['size'] ) ) {
$query_args['posts_per_page'] =
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_fields('RootQueryToCommentConnectionWhereArgs', [
'offsetPagination' => [
'type' => 'OffsetPagination',
'description' => __(
'Paginate Comments with offsets',
'wp-graphql-offset-pagination'
),
],
]);

register_graphql_object_type('OffsetPaginationPageInfo', [
'description' => __(
'Get information about the offset pagination state',
Expand Down