SHARE
TWEET

Untitled

a guest Oct 20th, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. add_action('graphql_register_types', 'PostsWithPaginationConnection', 99);
  3. function PostsWithPaginationConnection()
  4. {
  5.     $connection_args = array_merge(['offset' => [
  6.         'type' => 'Int',
  7.         'description' => __('Offset for current results', 'wp-graphql'),
  8.     ]], \WPGraphQL\Connection\PostObjects::get_connection_args());
  9.     $config = [
  10.         'fromType' => 'RootQuery',
  11.         'toType' => 'Post',
  12.         'fromFieldName' => 'postsWithPagination',
  13.         'connectionTypeName' => 'PostsWithPaginationConnection',
  14.         'resolve' => function ($id, $args, $context, $info) {
  15.             $resolver = new \WPGraphQL\Data\Connection\PostObjectConnectionResolver($source, $args, $context, $info, 'post');
  16.             if (!is_null($args["offset"])) {
  17.                 $resolver->setQueryArg('posts_per_page', !is_null($args["first"]) ? $args["first"] : 10);
  18.                 $resolver->setQueryArg('offset', $args["offset"]);
  19.             }
  20.             $connection = $resolver->get_connection();
  21.             return $connection;
  22.         },
  23.         'connectionArgs' => $connection_args,
  24.         'resolveNode' => function ($id, $args, $context, $info) {
  25.             return \WPGraphQL\Data\DataSource::resolve_post_object($id, $context);
  26.         },
  27.     ];
  28.     register_graphql_connection($config);
  29. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top