Skip to content

Commit

Permalink
feat: "found" field added to the Products Connection types (#804)
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 authored Sep 16, 2023
1 parent c526a5b commit 134dc5e
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 28 deletions.
5 changes: 4 additions & 1 deletion includes/connection/class-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,11 @@ public static function get_connection_config( $args = [] ): array {
public static function get_connection_fields(): array {
return [
'found' => [
'type' => 'Number',
'type' => 'Integer',
'description' => __( 'Total products founds', 'wp-graphql-woocommerce' ),
'resolve' => static function ( $source ) {
return ! empty( $source['pageInfo']['found'] ) ? $source['pageInfo']['found'] : null;
},
],
];
}
Expand Down
19 changes: 13 additions & 6 deletions includes/data/connection/class-product-connection-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ public function get_query_args() {
*/
$query_args['ignore_sticky_posts'] = true;

/**
* Don't calculate the total rows, it's not needed and can be expensive
*/
$query_args['no_found_rows'] = true;

/**
* Set post_type
*/
Expand All @@ -117,9 +112,11 @@ public function get_query_args() {
/**
* If the cursor offsets not empty,
* ignore sticky posts on the query
* and don't count the total number of posts
*/
if ( ! empty( $this->get_after_offset() ) || ! empty( $this->get_after_offset() ) ) {
if ( ! empty( $this->get_after_offset() ) || ! empty( $this->get_before_offset() ) ) {
$query_args['ignore_sticky_posts'] = true;
$query_args['no_found_rows'] = true;
}

/**
Expand Down Expand Up @@ -657,4 +654,14 @@ public function add_tax_query( $value ) {

return $this;
}

/**
* {@inheritDoc}
*/
public function get_page_info() {
$page_info = parent::get_page_info();
$page_info['found'] = $this->query->found_posts;

return $page_info;
}
}
3 changes: 3 additions & 0 deletions includes/model/class-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ protected function init() {
'soldIndividually' => function () {
return $this->wc_data->is_sold_individually();
},
'lowStockAmount' => function () {
return ! empty( $this->wc_data->get_low_stock_amount() ) ? $this->wc_data->get_low_stock_amount() : null;
},
'weight' => function () {
return ! empty( $this->wc_data->get_weight() ) ? $this->wc_data->get_weight() : null;
},
Expand Down
4 changes: 4 additions & 0 deletions includes/type/interface/class-inventoried-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public static function get_fields() {
'type' => 'ManageStockEnum',
'description' => __( 'If product manage stock', 'wp-graphql-woocommerce' ),
],
'lowStockAmount' => [
'type' => 'Int',
'description' => __( 'Low stock amount', 'wp-graphql-woocommerce' ),
],
'stockQuantity' => [
'type' => 'Int',
'description' => __( 'Number of items available for sale', 'wp-graphql-woocommerce' ),
Expand Down
43 changes: 27 additions & 16 deletions includes/type/object/class-root-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function register_fields() {
register_graphql_fields(
'RootQuery',
[
'cart' => [
'cart' => [
'type' => 'Cart',
'args' => [
'recalculateTotals' => [
Expand All @@ -52,7 +52,7 @@ public static function register_fields() {
return $cart;
},
],
'cartItem' => [
'cartItem' => [
'type' => 'CartItem',
'args' => [
'key' => [
Expand All @@ -69,7 +69,7 @@ public static function register_fields() {
return $item;
},
],
'cartFee' => [
'cartFee' => [
'type' => 'CartFee',
'args' => [
'id' => [
Expand All @@ -88,7 +88,7 @@ public static function register_fields() {
return $fees[ $fee_id ];
},
],
'coupon' => [
'coupon' => [
'type' => 'Coupon',
'description' => __( 'A coupon object', 'wp-graphql-woocommerce' ),
'args' => [
Expand Down Expand Up @@ -146,7 +146,7 @@ public static function register_fields() {
return Factory::resolve_crud_object( $coupon_id, $context );
},
],
'customer' => [
'customer' => [
'type' => 'Customer',
'description' => __( 'A customer object', 'wp-graphql-woocommerce' ),
'args' => [
Expand Down Expand Up @@ -194,7 +194,7 @@ public static function register_fields() {
return Factory::resolve_session_customer();
},
],
'order' => [
'order' => [
'type' => 'Order',
'description' => __( 'A order object', 'wp-graphql-woocommerce' ),
'args' => [
Expand Down Expand Up @@ -272,7 +272,7 @@ public static function register_fields() {
return Factory::resolve_crud_object( $order_id, $context );
},
],
'productVariation' => [
'productVariation' => [
'type' => 'ProductVariation',
'description' => __( 'A product variation object', 'wp-graphql-woocommerce' ),
'args' => [
Expand Down Expand Up @@ -318,7 +318,7 @@ public static function register_fields() {
return Factory::resolve_crud_object( $variation_id, $context );
},
],
'refund' => [
'refund' => [
'type' => 'Refund',
'description' => __( 'A refund object', 'wp-graphql-woocommerce' ),
'args' => [
Expand Down Expand Up @@ -399,7 +399,7 @@ public static function register_fields() {
return Factory::resolve_crud_object( $refund_id, $context );
},
],
'shippingMethod' => [
'shippingMethod' => [
'type' => 'ShippingMethod',
'description' => __( 'A shipping method object', 'wp-graphql-woocommerce' ),
'args' => [
Expand Down Expand Up @@ -434,7 +434,7 @@ public static function register_fields() {
return Factory::resolve_shipping_method( $method_id );
},
],
'taxRate' => [
'taxRate' => [
'type' => 'TaxRate',
'description' => __( 'A tax rate object', 'wp-graphql-woocommerce' ),
'args' => [
Expand Down Expand Up @@ -469,7 +469,17 @@ public static function register_fields() {
return Factory::resolve_tax_rate( $rate_id, $context );
},
],
'allowedCountries' => [
'countries' => [
'type' => [ 'list_of' => 'CountriesEnum' ],
'description' => __( 'Countries', 'wp-graphql-woocommerce' ),
'resolve' => static function () {
$wc_countries = new \WC_Countries();
$countries = $wc_countries->get_countries();

return array_keys( $countries );
},
],
'allowedCountries' => [
'type' => [ 'list_of' => 'CountriesEnum' ],
'description' => __( 'Countries that the store sells to', 'wp-graphql-woocommerce' ),
'resolve' => static function () {
Expand All @@ -479,7 +489,7 @@ public static function register_fields() {
return array_keys( $countries );
},
],
'allowedCountryStates' => [
'countryStates' => [
'type' => [ 'list_of' => 'CountryState' ],
'args' => [
'country' => [
Expand Down Expand Up @@ -535,10 +545,11 @@ public static function register_fields() {
'RootQuery',
$field_name,
[
'type' => $type_name,
'type' => $type_name,
/* translators: Product type slug */
'description' => sprintf( __( 'A %s product object', 'wp-graphql-woocommerce' ), $type_key ),
'args' => [
'description' => sprintf( __( 'A %s product object', 'wp-graphql-woocommerce' ), $type_key ),
'deprecationReason' => 'Use "product" instead.',
'args' => [
'id' => [
'type' => 'ID',
'description' => sprintf(
Expand All @@ -552,7 +563,7 @@ public static function register_fields() {
'description' => __( 'Type of ID being used identify product', 'wp-graphql-woocommerce' ),
],
],
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) use ( $type_key, $unsupported_type_enabled ) {
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) use ( $type_key, $unsupported_type_enabled ) {
$id = isset( $args['id'] ) ? $args['id'] : null;
$id_type = isset( $args['idType'] ) ? $args['idType'] : 'global_id';

Expand Down
5 changes: 5 additions & 0 deletions tests/wpunit/ConnectionPaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ function( $key_a, $key_b ) {
$query = '
query ($first: Int, $last: Int, $after: String, $before: String) {
products(first: $first, last: $last, after: $after, before: $before) {
found
nodes {
databaseId
}
Expand All @@ -170,6 +171,7 @@ function( $key_a, $key_b ) {
$variables = [ 'first' => 2 ];
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
$this->expectedField( 'products.found', 5 ),
$this->expectedField( 'products.pageInfo.hasPreviousPage', false ),
$this->expectedField( 'products.pageInfo.hasNextPage', true ),
$this->expectedField( 'products.pageInfo.startCursor', $this->toCursor( $products[0] ) ),
Expand All @@ -191,6 +193,7 @@ function( $key_a, $key_b ) {
];
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
$this->expectedField( 'products.found', self::IS_NULL ),
$this->expectedField( 'products.pageInfo.hasPreviousPage', true ),
$this->expectedField( 'products.pageInfo.hasNextPage', false ),
$this->expectedField( 'products.pageInfo.startCursor', $this->toCursor( $products[2] ) ),
Expand All @@ -210,6 +213,7 @@ function( $key_a, $key_b ) {
$variables = [ 'last' => 2 ];
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
$this->expectedField( 'products.found', 5 ),
$this->expectedField( 'products.pageInfo.hasPreviousPage', true ),
$this->expectedField( 'products.pageInfo.hasNextPage', false ),
$this->expectedField( 'products.pageInfo.startCursor', $this->toCursor( $products[3] ) ),
Expand All @@ -231,6 +235,7 @@ function( $key_a, $key_b ) {
];
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
$this->expectedField( 'products.found', self::IS_NULL ),
$this->expectedField( 'products.pageInfo.hasPreviousPage', true ),
$this->expectedField( 'products.pageInfo.hasNextPage', true ),
$this->expectedField( 'products.pageInfo.startCursor', $this->toCursor( $products[1] ) ),
Expand Down
33 changes: 28 additions & 5 deletions tests/wpunit/RootQueriesTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
<?php

class RootQueriesTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraphQLTestCase {
public function testBillingCountriesQuery() {
// Create shipping zones and shipping rates.
update_option( 'woocommerce_allowed_countries', 'specific' );
update_option( 'woocommerce_specific_allowed_countries', [ 'US', 'CA' ] );

// Create query
$query = '
query {
countries
}
';

$response = $this->graphql( compact( 'query' ) );
$expected = [
$this->expectedField( 'countries.#', 'US' ),
$this->expectedField( 'countries.#', 'CA' ),
$this->expectedField( 'countries.#', 'GB' ),
$this->expectedField( 'countries.#', 'JP' ),
];

$this->assertQuerySuccessful( $response, $expected );
}

public function testShippingCountriesQuery() {
// Create shipping zones and shipping rates.
update_option( 'woocommerce_allowed_countries', 'specific' );
Expand Down Expand Up @@ -31,7 +54,7 @@ public function testShippingCountryStatesQuery() {
// Create query
$query = '
query ($country: CountriesEnum!) {
allowedCountryStates(country: $country) {
countryStates(country: $country) {
name
code
}
Expand All @@ -42,14 +65,14 @@ public function testShippingCountryStatesQuery() {
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
$this->expectedObject(
'allowedCountryStates.#',
'countryStates.#',
[
$this->expectedField( 'name', 'Alaska' ),
$this->expectedField( 'code', 'AL' ),
]
),
$this->expectedObject(
'allowedCountryStates.#',
'countryStates.#',
[
$this->not()->expectedField( 'name', 'Ontario' ),
$this->not()->expectedField( 'code', 'ON' ),
Expand All @@ -63,14 +86,14 @@ public function testShippingCountryStatesQuery() {
$response = $this->graphql( compact( 'query', 'variables' ) );
$expected = [
$this->expectedObject(
'allowedCountryStates.#',
'countryStates.#',
[
$this->expectedField( 'name', 'Ontario' ),
$this->expectedField( 'code', 'ON' ),
]
),
$this->expectedObject(
'allowedCountryStates.#',
'countryStates.#',
[
$this->not()->expectedField( 'name', 'Alaska' ),
$this->not()->expectedField( 'code', 'AL' ),
Expand Down

0 comments on commit 134dc5e

Please sign in to comment.