Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: collectionStats query added #785

Merged

Conversation

kidunot89
Copy link
Member

@kidunot89 kidunot89 commented Aug 16, 2023

Your checklist for this pull request

Thanks for sending a pull request! Please make sure you click the link above to view the contribution guidelines, then fill out the blanks below.

🚨Please review the guidelines for contributing to this repository.

  • Make sure you are making a pull request against the develop branch (left side). Also you should start your branch off our develop.
  • Make sure you are requesting to pull request from a topic/feature/bugfix/devops branch (right side). Don't pull request from your master!
  • Have you ensured/updated that CLI tests to extend coverage to any new logic. Learn how to modify the tests here.

What does this implement/fix? Explain your changes.

Adds collectionStats query. Here's an example usage.

Note: the query is currently in flux and without hooks for modification because it utilizes the functionality of the woocommerce-blocks Store API's ProductCollection endpoint which is still kinda limited. It works for the most part, but the attributeCounts field only takes the where.attributes field is taken into account with calculating the attribute counts. Meaning it's not possible to filter the result by any "non product attribute" taxonomies at the time of this release.

Example

query ($where: CollectionStatsWhereArgs){
  collectionStats(
    calculatePriceRange: true
    calculateRatingCounts: true
    calculateStockStatusCounts: true
    taxonomies: [{taxonomy: PACOLOR, relation: AND}]
    where: $where
  ) {
    priceRange {
      maxPrice
      minPrice
    }
    ratingCounts {
      count
    }
    attributeCounts {
      name 
      terms {
        node {
          slug
        }
        termId
        count
      }
    }
    stockStatusCounts {
      status
      count
    }
  }
}

image

TODO

  • Add a unit test for the collectionStats query.

Does this close any currently open issues?

Any relevant logs, error output, GraphiQL screenshots, etc?

(If it’s long, please paste to https://ghostbin.com/ and insert the link here.)

Any other comments?

Where has this been tested?

  • WooGraphQL Version:
  • WPGraphQL Version:
  • WordPress Version:
  • WooCommerce Version:

@kidunot89 kidunot89 changed the title feat: collectionStats query added feat: collectionStats query added Aug 16, 2023
Copy link
Contributor

@creative-andrew creative-andrew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thist is great! I think you are on the correct track but I would advise the following.
Instead of having the field on the Root type, it needs to be inside the Products (and Product?) field.
The collection stats depend on the query made on the product as the count of attributes changes according to it.

https://github.com/woocommerce/woocommerce-blocks/blob/trunk/src/StoreApi/docs/product-collection-data.md#product-collection-data-api

Important

In addition to the above attributes, all product list attributes are supported. This allows you to get data for a certain subset of products. See the products API list products section for the full list.

GET /products/collection-data?calculate_attribute_counts[0][query_type]=or&calculate_attribute_counts[0][taxonomy]=pa_sizes&attributes[0]&[attribute]=pa_color&attributes[0][slug]=red

would be similar to do

query GetProducts {
  products(where: {taxonomyFilter: {filters: {taxonomy: PACOLOR, terms: "red"}}}) {
    collectionStats(taxonomies: [{taxonomy: PASIZES, relation: OR}]) {
      attributeCounts {
        termId
        count
      }
    }
    nodes {
      title
    }
  }
}

The results here should be the term count of the attribute SIZES for all products that have a COLOR attribute of red

Comment on lines 115 to 121
'page' => [
'type' => 'Int',
'description' => __( 'Page of results to return.', 'wp-graphql-woocommerce' ),
],
'perPage' => [
'type' => 'Int',
'description' => __( 'Number of results to return per page.', 'wp-graphql-woocommerce' ),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should return all the results. I don't see any type of pagination in the Store API implementation.

Copy link
Member Author

@kidunot89 kidunot89 Aug 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this result list can get pretty monstrous and it would be pretty foolishly to not do this. And REST is already caching the full result list so might as well take advantage of that. If you want the full list don't set any arguments, these arguments are if you actually want pagination but doesn't force it on the client

'stock_status_counts' => null,
'rating_counts' => null,
];
$filters = new ProductQueryFilters();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider recreating this class inside WooGraphQL plugin so we can modify it accordingly and not be subject to non-existent query params that only make sense in the REST API context.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naw, having gone down that route with the checkout mutation. I can say with confidence this is more future-proof and easier to maintain/refactor in the likely event of a breaking change.

@kidunot89 kidunot89 force-pushed the feat/wc-store-product-collection-query branch from 870f945 to c556ab7 Compare September 22, 2023 22:45
return $request;
}

$key_mapping = [
Copy link
Contributor

@creative-andrew creative-andrew Sep 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we would need a filter here for extensibility?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not at this time, because the functionality of ProductQueryFilters is kinda limited at the moment and not extendable by hooks or extra params, we can revisit this once it has been expanded upon or made extendable is some way 🤔.

@kidunot89 kidunot89 merged commit 6b0c798 into wp-graphql:develop Sep 23, 2023
14 checks passed
@kidunot89 kidunot89 deleted the feat/wc-store-product-collection-query branch September 23, 2023 07:50
@kidunot89 kidunot89 added enhancement New feature or request bugfix Implements bugfix labels Sep 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix Implements bugfix enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants