Created
October 2, 2019 14:45
-
-
Save dz0ny/6640e3e579dcd277f199dec5fde59c8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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