valu-digital / wp-graphql-offset-pagination

Adds traditional offset pagination support to WPGraphQL
73 stars 18 forks source link

Returns total of 0 for products #17

Open WazzaJB opened 2 months ago

WazzaJB commented 2 months ago

Hey folks,

Appreciate you sharing this plugin, extremely useful for a project we're working on.

I'm having difficulties with it now on WordPress 6.5, WPGraphQL 1.27, and WPGraphQL-WooCommerce 0.20.0.

All my queries are now returning a total of 0.

I'm going to look at rolling back in the mean time but was wondering if anyone had ideas for a potential hotfix? PHP is not my forte these days but I am happy to work on a pull request!

Here is the query I am using:

query Products(
    $id: ID!
    $slug: String!
    $idType: ProductCategoryIdType = SLUG
    $size: Int = 15
    $offset: Int = 0
  ) {
    products(
      where: {
        offsetPagination: { size: $size, offset: $offset }
        category: $slug
      }
    ) {
      edges {
        node {
          ... on SimpleProduct {
            id
            title
            formattedPrice: price(format: FORMATTED)
            rawPrice: price(format: RAW)
            height
            width
            name
            slug
            sku
            image {
              thumbnail: sourceUrl(size: THUMBNAIL)
              large: sourceUrl(size: LARGE)
            }
          }
        }
      }
      pageInfo {
        offsetPagination {
          hasMore
          hasPrevious
          total
        }
      }
    }
  }

Thanks in advance for any guidance you might be able to offer.

FrozenAlex commented 1 month ago

@WazzaJB if you have this filter in your code somewhere, delete it, it did set the thing to 0 for me too. Not using Woocommerce but it might help

add_filter( 'graphql_connection_query_args', function( $query_args, \WPGraphQL\Data\Connection\AbstractConnectionResolver $connection, $input_args ) {

    if ( ! $connection->get_query() instanceof \WP_Query ) {
        return $query_args;
    }

    $field_selection = $connection->getInfo()->getFieldSelection( 2 );

    if ( isset( $field_selection['pageInfo']['total'] ) ) {
        $query_args['no_found_rows'] = false;
    }

    return  $query_args;

}, 10, 3 );
WazzaJB commented 1 month ago

Thanks @FrozenAlex - I actually ended up just using the found response from the parent schema which seems to have patched things up!