Skip to content

Instantly share code, notes, and snippets.

@dz0ny
Created October 2, 2019 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dz0ny/6640e3e579dcd277f199dec5fde59c8d to your computer and use it in GitHub Desktop.
Save dz0ny/6640e3e579dcd277f199dec5fde59c8d to your computer and use it in GitHub Desktop.
<?php
add_action('graphql_register_types', 'PostsWithPaginationConnection', 99);
function PostsWithPaginationConnection()
{
$connection_args = array_merge(['offset' => [
'type' => 'Int',
'description' => __('Offset for current results', 'wp-graphql'),
]], \WPGraphQL\Connection\PostObjects::get_connection_args());
$config = [
'fromType' => 'RootQuery',
'toType' => 'Post',
'fromFieldName' => 'postsWithPagination',
'connectionTypeName' => 'PostsWithPaginationConnection',
'resolve' => function ($id, $args, $context, $info) {
$resolver = new \WPGraphQL\Data\Connection\PostObjectConnectionResolver($source, $args, $context, $info, 'post');
if (!is_null($args["offset"])) {
$resolver->setQueryArg('posts_per_page', !is_null($args["first"]) ? $args["first"] : 10);
$resolver->setQueryArg('offset', $args["offset"]);
}
$connection = $resolver->get_connection();
return $connection;
},
'connectionArgs' => $connection_args,
'resolveNode' => function ($id, $args, $context, $info) {
return \WPGraphQL\Data\DataSource::resolve_post_object($id, $context);
},
];
register_graphql_connection($config);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment