Skip to content

Commit

Permalink
fix: add model classes to type configs to better support query analyz…
Browse files Browse the repository at this point in the history
…er ID tracking (#874)

* - remove several fields that are already registered by core when the post type is mapped to the schema

* devops: Tests updated and coupon "salt" restored

* chore: Linter & PHPStan compliance met

* chore: Linter & PHPStan compliance met

* devops: "self" replaced with "static" in cli tests

* chore: fix cleanup after rebasing

---------

Co-authored-by: Geoff Taylor <geoff@axistaylor.com>
  • Loading branch information
jasonbahl and kidunot89 authored Aug 6, 2024
1 parent d9d968a commit 962410d
Show file tree
Hide file tree
Showing 42 changed files with 520 additions and 498 deletions.
4 changes: 4 additions & 0 deletions codeception.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ modules:
WPLoader:
wpRootFolder: '%WP_CORE_DIR%'
dbUrl: 'mysql://%DB_USER%:%DB_PASSWORD%@%DB_HOST%:%DB_PORT%/%DB_NAME%'
dbName: '%DB_NAME%'
dbHost: '%DB_HOST%'
dbUser: '%DB_USER%'
dbPassword: '%DB_PASSWORD%'
tablePrefix: '%WP_TABLE_PREFIX%'
domain: '%WORDPRESS_DOMAIN%'
adminEmail: '%ADMIN_EMAIL%'
Expand Down
6 changes: 6 additions & 0 deletions includes/class-core-schema-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public static function add_filters() {
public static function register_post_types( $args, $post_type ) {
if ( 'product' === $post_type ) {
$args['show_in_graphql'] = true;
$args['model'] = \WPGraphQL\WooCommerce\Model\Product::class;
$args['graphql_single_name'] = 'Product';
$args['graphql_plural_name'] = 'Products';
$args['graphql_kind'] = 'interface';
Expand Down Expand Up @@ -358,6 +359,7 @@ public static function inject_union_type_resolver( $type, $value, $wp_union ) {
* @return \WPGraphQL\Type\WPObjectType|null
*/
public static function inject_type_resolver( $type, $value ) {

$type_registry = \WPGraphQL::get_type_registry();
switch ( $type ) {
case 'Coupon':
Expand All @@ -367,6 +369,9 @@ public static function inject_type_resolver( $type, $value ) {
$type = $type_registry->get_type( $new_type );
}
break;
case 'ProductVariation':
$type = self::resolve_product_variation_type( $value );
break;
case 'Product':
$supported_types = WooGraphQL::get_enabled_product_types();
if ( in_array( $value->type, array_keys( $supported_types ), true ) ) {
Expand Down Expand Up @@ -433,6 +438,7 @@ public static function resolve_product_variation_type( $value ) {
$type_registry = \WPGraphQL::get_type_registry();
$possible_types = WooGraphQL::get_enabled_product_variation_types();
$product_type = $value->get_type();

if ( isset( $possible_types[ $product_type ] ) ) {
return $type_registry->get_type( $possible_types[ $product_type ] );
}
Expand Down
2 changes: 1 addition & 1 deletion includes/model/class-customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected function init() {
},
'id' => function () {
return ( ! empty( $this->data->get_id() ) )
? Relay::toGlobalId( 'customer', $this->data->get_id() )
? Relay::toGlobalId( 'user', $this->data->get_id() )
: 'guest';
},
'databaseId' => function () {
Expand Down
4 changes: 2 additions & 2 deletions includes/model/class-product-variation.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected function init() {
return ! empty( $this->wc_data->get_id() ) ? $this->wc_data->get_id() : null;
},
'id' => function () {
return ! empty( $this->ID ) ? Relay::toGlobalId( 'product_variation', "{$this->ID}" ) : null;
return ! empty( $this->ID ) ? Relay::toGlobalId( 'post', "{$this->ID}" ) : null;
},
'name' => function () {
return ! empty( $this->wc_data->get_name() ) ? $this->wc_data->get_name() : null;
Expand Down Expand Up @@ -226,7 +226,7 @@ protected function init() {
return ! empty( $this->wc_data->get_parent_id() ) ? $this->wc_data->get_parent_id() : null;
},
'parentId' => function () {
return ! empty( $this->wc_data->get_parent_id() ) ? Relay::toGlobalId( 'product', (string) $this->wc_data->get_parent_id() ) : null;
return ! empty( $this->wc_data->get_parent_id() ) ? Relay::toGlobalId( 'post', (string) $this->wc_data->get_parent_id() ) : null;
},
'shipping_class_id' => function () {
return ! empty( $this->wc_data->get_shipping_class_id() ) ? $this->wc_data->get_shipping_class_id() : null;
Expand Down
2 changes: 1 addition & 1 deletion includes/model/class-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ protected function init() {
return ! empty( $this->wc_data->get_id() ) ? $this->wc_data->get_id() : null;
},
'id' => function () {
return ! empty( $this->ID ) ? Relay::toGlobalId( 'product', "{$this->ID}" ) : null;
return ! empty( $this->ID ) ? Relay::toGlobalId( 'post', "{$this->ID}" ) : null;
},
'type' => function () {
return ! empty( $this->wc_data->get_type() ) ? $this->wc_data->get_type() : null;
Expand Down
1 change: 1 addition & 0 deletions includes/type/interface/class-product-variation.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static function register_interface(): void {
'SimpleProductVariation',
[
'eagerlyLoadType' => true,
'model' => \WPGraphQL\WooCommerce\Model\Product_Variation::class,
'description' => __( 'A product variation', 'wp-graphql-woocommerce' ),
'interfaces' => [ 'Node', 'ProductVariation' ],
'fields' => [],
Expand Down
5 changes: 5 additions & 0 deletions includes/type/object/class-product-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private static function register_simple_product_type() {
'SimpleProduct',
[
'eagerlyLoadType' => true,
'model' => \WPGraphQL\WooCommerce\Model\Product::class,
'description' => __( 'A simple product object', 'wp-graphql-woocommerce' ),
'interfaces' => self::get_product_interfaces(
[
Expand All @@ -92,6 +93,7 @@ private static function register_variable_product_type() {
'VariableProduct',
[
'eagerlyLoadType' => true,
'model' => \WPGraphQL\WooCommerce\Model\Product::class,
'description' => __( 'A variable product object', 'wp-graphql-woocommerce' ),
'interfaces' => self::get_product_interfaces(
[
Expand All @@ -116,6 +118,7 @@ private static function register_external_product_type() {
'ExternalProduct',
[
'eagerlyLoadType' => true,
'model' => \WPGraphQL\WooCommerce\Model\Product::class,
'description' => __( 'A external product object', 'wp-graphql-woocommerce' ),
'interfaces' => self::get_product_interfaces( [ 'ProductWithPricing' ] ),
'fields' => array_merge(
Expand Down Expand Up @@ -144,6 +147,7 @@ private static function register_group_product_type() {
'GroupProduct',
[
'eagerlyLoadType' => true,
'model' => \WPGraphQL\WooCommerce\Model\Product::class,
'description' => __( 'A group product object', 'wp-graphql-woocommerce' ),
'interfaces' => self::get_product_interfaces( [ 'ProductWithPricing' ] ),
'fields' => [
Expand Down Expand Up @@ -208,6 +212,7 @@ private static function register_unsupported_product_type() {
WooGraphQL::get_supported_product_type(),
[
'eagerlyLoadType' => true,
'model' => \WPGraphQL\WooCommerce\Model\Product::class,
'description' => __( 'A product object for a product type that is unsupported by the current API.', 'wp-graphql-woocommerce' ),
'interfaces' => self::get_product_interfaces(
[
Expand Down
4 changes: 2 additions & 2 deletions tests/_support/Helper/crud-helpers/cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ public function print_item_query( $key ) {
'key' => $item['key'],
'product' => array(
'node' => array(
'id' => Relay::toGlobalId( 'product', $item['product_id'] ),
'id' => Relay::toGlobalId( 'post', $item['product_id'] ),
'databaseId' => $item['product_id'],
),
),
'variation' => ! empty( $variation )
? array(
'attributes' => $attributes,
'node' => array(
'id' => Relay::toGlobalId( 'product_variation', $item['variation_id'] ),
'id' => Relay::toGlobalId( 'post', $item['variation_id'] ),
'databaseId' => $item['variation_id'],
),
)
Expand Down
2 changes: 1 addition & 1 deletion tests/_support/Helper/crud-helpers/customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function __construct() {
}

public function to_relay_id( $id ) {
return Relay::toGlobalId( 'customer', $id );
return Relay::toGlobalId( 'user', $id );
}

public function create( $args = array() ) {
Expand Down
2 changes: 1 addition & 1 deletion tests/_support/Helper/crud-helpers/order.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function print_query( $id ) {
'pricesIncludeTax' => $data->get_prices_include_tax(),
'parent' => null,
'customer' => ! empty( $data->get_customer_id() )
? array( 'id' => Relay::toGlobalId( 'customer', $data->get_customer_id() ) )
? array( 'id' => Relay::toGlobalId( 'user', $data->get_customer_id() ) )
: null,
'customerIpAddress' => ! empty( $data->get_customer_ip_address() )
? $data->get_customer_ip_address()
Expand Down
2 changes: 1 addition & 1 deletion tests/_support/Helper/crud-helpers/product-variation.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected function __construct() {
}

public function to_relay_id( $id ) {
return Relay::toGlobalId( 'product_variation', $id );
return Relay::toGlobalId( 'post', $id );
}

public function reset_indexes() {
Expand Down
2 changes: 1 addition & 1 deletion tests/_support/Helper/crud-helpers/product.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected function __construct() {
}

public function to_relay_id( $id ) {
return Relay::toGlobalId( 'product', $id );
return Relay::toGlobalId( 'post', $id );
}

public function reset_indexes() {
Expand Down
8 changes: 7 additions & 1 deletion tests/_support/TestCase/WooGraphQLTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,13 @@ protected function logout() {
*
* @return mixed
*/
protected function maybe( $possible, $default = self::IS_NULL ) {
protected function maybe( $possible, $custom_default = null ) {
if ( null === $custom_default ) {
$default = static::IS_NULL;
} else {
$default = $custom_default;
}

if ( is_array( $possible ) && 2 === count( $possible ) ) {
list( $possible, $decorated ) = $possible;
} else {
Expand Down
16 changes: 8 additions & 8 deletions tests/wpunit/CartMutationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function testAddToCartMutationWithProduct() {
[
$this->expectedField( 'addToCart.clientMutationId', 'someId' ),
$this->expectedField( 'addToCart.cartItem.key', $cart_item_key ),
$this->expectedField( 'addToCart.cartItem.product.node.id', $this->toRelayId( 'product', $product_id ) ),
$this->expectedField( 'addToCart.cartItem.product.node.id', $this->toRelayId( 'post', $product_id ) ),
$this->expectedField( 'addToCart.cartItem.quantity', 2 ),
$this->expectedField( 'addToCart.cartItem.subtotal', wc_graphql_price( $cart_item['line_subtotal'] ) ),
$this->expectedField( 'addToCart.cartItem.subtotalTax', wc_graphql_price( $cart_item['line_subtotal_tax'] ) ),
Expand Down Expand Up @@ -131,8 +131,8 @@ public function testAddToCartMutationWithProductVariation() {
[
$this->expectedField( 'addToCart.clientMutationId', 'someId' ),
$this->expectedField( 'addToCart.cartItem.key', $cart_item_key ),
$this->expectedField( 'addToCart.cartItem.product.node.id', $this->toRelayId( 'product', $ids['product'] ) ),
$this->expectedField( 'addToCart.cartItem.variation.node.id', $this->toRelayId( 'product_variation', $ids['variations'][0] ) ),
$this->expectedField( 'addToCart.cartItem.product.node.id', $this->toRelayId( 'post', $ids['product'] ) ),
$this->expectedField( 'addToCart.cartItem.variation.node.id', $this->toRelayId( 'post', $ids['variations'][0] ) ),
$this->expectedField( 'addToCart.cartItem.quantity', 3 ),
$this->expectedField( 'addToCart.cartItem.subtotal', wc_graphql_price( $cart_item['line_subtotal'] ) ),
$this->expectedField( 'addToCart.cartItem.subtotalTax', wc_graphql_price( $cart_item['line_subtotal_tax'] ) ),
Expand Down Expand Up @@ -444,7 +444,7 @@ public function testEmptyCartMutation() {
'key' => $cart_item['key'],
'product' => [
'node' => [
'id' => $this->toRelayId( 'product', $cart_item['product_id'] ),
'id' => $this->toRelayId( 'post', $cart_item['product_id'] ),
],
],
'variation' => null,
Expand Down Expand Up @@ -541,7 +541,7 @@ public function testApplyCouponMutation() {
'key' => $cart_item['key'],
'product' => [
'node' => [
'id' => $this->toRelayId( 'product', $cart_item['product_id'] ),
'id' => $this->toRelayId( 'post', $cart_item['product_id'] ),
],
],
'quantity' => $cart_item['quantity'],
Expand Down Expand Up @@ -720,7 +720,7 @@ public function testRemoveCouponMutation() {
'key' => $cart_item['key'],
'product' => [
'node' => [
'id' => $this->toRelayId( 'product', $cart_item['product_id'] ),
'id' => $this->toRelayId( 'post', $cart_item['product_id'] ),
],
],
'quantity' => $cart_item['quantity'],
Expand Down Expand Up @@ -1204,15 +1204,15 @@ public function testFillCartMutationAndErrors() {
[
$this->expectedField( 'product.node.databaseId', $product_one ),
$this->expectedField( 'quantity', 3 ),
$this->expectedField( 'variation', self::IS_NULL ),
$this->expectedField( 'variation', static::IS_NULL ),
]
),
$this->expectedNode(
'nodes',
[
$this->expectedField( 'product.node.databaseId', $product_two ),
$this->expectedField( 'quantity', 2 ),
$this->expectedField( 'variation', self::IS_NULL ),
$this->expectedField( 'variation', static::IS_NULL ),
]
),
]
Expand Down
28 changes: 14 additions & 14 deletions tests/wpunit/CartQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ public function getExpectedCartData() {
$this->expectedField( 'cart.isEmpty', $cart->is_empty() ),
$this->expectedField( 'cart.displayPricesIncludeTax', $cart->display_prices_including_tax() ),
$this->expectedField( 'cart.needsShippingAddress', $cart->needs_shipping_address() ),
$this->expectedField( 'cart.rawSubtotal', self::NOT_NULL ),
$this->expectedField( 'cart.rawSubtotalTax', self::NOT_NULL ),
$this->expectedField( 'cart.rawDiscountTotal', self::NOT_NULL ),
$this->expectedField( 'cart.rawDiscountTax', self::NOT_NULL ),
$this->expectedField( 'cart.rawShippingTotal', self::NOT_NULL ),
$this->expectedField( 'cart.rawShippingTax', self::NOT_NULL ),
$this->expectedField( 'cart.rawContentsTotal', self::NOT_NULL ),
$this->expectedField( 'cart.rawContentsTax', self::NOT_NULL ),
$this->expectedField( 'cart.rawFeeTotal', self::NOT_NULL ),
$this->expectedField( 'cart.rawFeeTax', self::NOT_NULL ),
$this->expectedField( 'cart.rawtotal', self::NOT_NULL ),
$this->expectedField( 'cart.rawTotalTax', self::NOT_NULL ),
$this->expectedField( 'cart.rawSubtotal', static::NOT_NULL ),
$this->expectedField( 'cart.rawSubtotalTax', static::NOT_NULL ),
$this->expectedField( 'cart.rawDiscountTotal', static::NOT_NULL ),
$this->expectedField( 'cart.rawDiscountTax', static::NOT_NULL ),
$this->expectedField( 'cart.rawShippingTotal', static::NOT_NULL ),
$this->expectedField( 'cart.rawShippingTax', static::NOT_NULL ),
$this->expectedField( 'cart.rawContentsTotal', static::NOT_NULL ),
$this->expectedField( 'cart.rawContentsTax', static::NOT_NULL ),
$this->expectedField( 'cart.rawFeeTotal', static::NOT_NULL ),
$this->expectedField( 'cart.rawFeeTax', static::NOT_NULL ),
$this->expectedField( 'cart.rawtotal', static::NOT_NULL ),
$this->expectedField( 'cart.rawTotalTax', static::NOT_NULL ),
];
}

Expand All @@ -47,12 +47,12 @@ public function getExpectedCartItemData( $path, $cart_item_key ) {
$path,
[
$this->expectedField( 'key', $item['key'] ),
$this->expectedField( 'product.node.id', $this->toRelayId( 'product', $item['product_id'] ) ),
$this->expectedField( 'product.node.id', $this->toRelayId( 'post', $item['product_id'] ) ),
$this->expectedField( 'product.node.databaseId', $item['product_id'] ),
$this->expectedField(
'variation.node.id',
! empty( $item['variation_id'] )
? $this->toRelayId( 'product_variation', $item['variation_id'] )
? $this->toRelayId( 'post', $item['variation_id'] )
: 'NULL'
),
$this->expectedField(
Expand Down
Loading

0 comments on commit 962410d

Please sign in to comment.