valu-digital / wp-graphql-offset-pagination

Adds traditional offset pagination support to WPGraphQL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

WooCommerce integration

saleebm opened this issue · comments

Why I want it

  • simple page numbers for product catalogs with complex filters and sorting options.

Is it worth it?

  • Not for typical SEO, probably would prefer using cursors as specific pointers to each edge for consistency.
  • But for clients, I see a few benefits with implementing this after reading your tutorial. Especially when it comes to a reducing complexity in the wp_query.

how?

foreach ( \WP_GraphQL_WooCommerce::get_post_types() as $post_type ) {
	$post_type_object = get_post_type_object( $post_type );
	self::add_post_type_fields( $post_type_object );
}

add_filter( 'graphql_product_connection_query_args',  function($query_args, $source, $args, $context, $info) {
                if ( isset( $args['where']['offsetPagination']['offset'] ) ) {
			$query_args['offset'] = $args['where']['offsetPagination']['offset'];
		}
                return $query_args;
}), 10, 5);

I'd like to see what you would think, seeing as you created this and I'm fairly new to all this, all I am trying to do is get some input if this makes any sense or not. I personally forked it and implemented the WooCommerce feature here.

Thank you for this awesome work!

I am facing similar issue. offsetPagination working for all other post types but for products it's not working!
image
Is there any way to add support for products as well or i am missing something?

Here's something Mehidi Hassan did on the WPGraphQL slack:

/**
 * Add offset pagination to products.
 */
function theme_prefix_add_offset_pagination_to_products() {
	register_graphql_field(
		'RootQueryToProductConnectionWhereArgs',
		'offsetPagination',
		array(
			'type'        => 'OffsetPagination',
			'description' => 'Paginate content nodes with offsets',
		)
	);
}
add_action( 'graphql_register_types', 'theme_prefix_add_offset_pagination_to_products' );
/**
 * Add offset pagination to products.
 *
 * @param array $query_args query args.
 * @param array $where_args where query args.
 */
function theme_prefix_filter_map_offset_to_wp_query_args(
	array $query_args,
	array $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;
}
add_filter( 'graphql_map_input_fields_to_product_query', 'theme_prefix_filter_map_offset_to_wp_query_args', 10, 2 );

The thread:
https://wp-graphql.slack.com/archives/C3NM1M291/p1616563369156900

From the solution mentioned in the comment above

Only the following part is needed:

/**
 * Add offset pagination to products.
 */
function theme_prefix_add_offset_pagination_to_products() {
	register_graphql_field(
		'RootQueryToProductConnectionWhereArgs',
		'offsetPagination',
		array(
			'type'        => 'OffsetPagination',
			'description' => 'Paginate content nodes with offsets',
		)
	);
}
add_action( 'graphql_register_types', 'theme_prefix_add_offset_pagination_to_products' );

The filter graphql_map_input_fields_to_product_query has been deprecated and its recommended to use instead

graphql_map_input_fields_to_wp_query which is already implemented by the plugin on

function op_filter_map_offset_to_wp_query_args(

As of today on:
wp-graphql-offset-pagination: 0.2.0
WooGraphQL Version: 0.2.0
WPGraphQL Version: 1.26
WordPress Version: 6.5.3
WooCommerce Version: 8.8.3

This worked:
https://gist.github.com/ZeroPie/9870b323c14262e6df19d44bf72d8445