Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement