Skip to content

Commit 962410d

Browse files
jasonbahlkidunot89
andauthored
fix: add model classes to type configs to better support query analyzer 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 <[email protected]>
1 parent d9d968a commit 962410d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+520
-498
lines changed

codeception.dist.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ modules:
6767
WPLoader:
6868
wpRootFolder: '%WP_CORE_DIR%'
6969
dbUrl: 'mysql://%DB_USER%:%DB_PASSWORD%@%DB_HOST%:%DB_PORT%/%DB_NAME%'
70+
dbName: '%DB_NAME%'
71+
dbHost: '%DB_HOST%'
72+
dbUser: '%DB_USER%'
73+
dbPassword: '%DB_PASSWORD%'
7074
tablePrefix: '%WP_TABLE_PREFIX%'
7175
domain: '%WORDPRESS_DOMAIN%'
7276
adminEmail: '%ADMIN_EMAIL%'

includes/class-core-schema-filters.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public static function add_filters() {
123123
public static function register_post_types( $args, $post_type ) {
124124
if ( 'product' === $post_type ) {
125125
$args['show_in_graphql'] = true;
126+
$args['model'] = \WPGraphQL\WooCommerce\Model\Product::class;
126127
$args['graphql_single_name'] = 'Product';
127128
$args['graphql_plural_name'] = 'Products';
128129
$args['graphql_kind'] = 'interface';
@@ -358,6 +359,7 @@ public static function inject_union_type_resolver( $type, $value, $wp_union ) {
358359
* @return \WPGraphQL\Type\WPObjectType|null
359360
*/
360361
public static function inject_type_resolver( $type, $value ) {
362+
361363
$type_registry = \WPGraphQL::get_type_registry();
362364
switch ( $type ) {
363365
case 'Coupon':
@@ -367,6 +369,9 @@ public static function inject_type_resolver( $type, $value ) {
367369
$type = $type_registry->get_type( $new_type );
368370
}
369371
break;
372+
case 'ProductVariation':
373+
$type = self::resolve_product_variation_type( $value );
374+
break;
370375
case 'Product':
371376
$supported_types = WooGraphQL::get_enabled_product_types();
372377
if ( in_array( $value->type, array_keys( $supported_types ), true ) ) {
@@ -433,6 +438,7 @@ public static function resolve_product_variation_type( $value ) {
433438
$type_registry = \WPGraphQL::get_type_registry();
434439
$possible_types = WooGraphQL::get_enabled_product_variation_types();
435440
$product_type = $value->get_type();
441+
436442
if ( isset( $possible_types[ $product_type ] ) ) {
437443
return $type_registry->get_type( $possible_types[ $product_type ] );
438444
}

includes/model/class-customer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected function init() {
8686
},
8787
'id' => function () {
8888
return ( ! empty( $this->data->get_id() ) )
89-
? Relay::toGlobalId( 'customer', $this->data->get_id() )
89+
? Relay::toGlobalId( 'user', $this->data->get_id() )
9090
: 'guest';
9191
},
9292
'databaseId' => function () {

includes/model/class-product-variation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected function init() {
9595
return ! empty( $this->wc_data->get_id() ) ? $this->wc_data->get_id() : null;
9696
},
9797
'id' => function () {
98-
return ! empty( $this->ID ) ? Relay::toGlobalId( 'product_variation', "{$this->ID}" ) : null;
98+
return ! empty( $this->ID ) ? Relay::toGlobalId( 'post', "{$this->ID}" ) : null;
9999
},
100100
'name' => function () {
101101
return ! empty( $this->wc_data->get_name() ) ? $this->wc_data->get_name() : null;
@@ -226,7 +226,7 @@ protected function init() {
226226
return ! empty( $this->wc_data->get_parent_id() ) ? $this->wc_data->get_parent_id() : null;
227227
},
228228
'parentId' => function () {
229-
return ! empty( $this->wc_data->get_parent_id() ) ? Relay::toGlobalId( 'product', (string) $this->wc_data->get_parent_id() ) : null;
229+
return ! empty( $this->wc_data->get_parent_id() ) ? Relay::toGlobalId( 'post', (string) $this->wc_data->get_parent_id() ) : null;
230230
},
231231
'shipping_class_id' => function () {
232232
return ! empty( $this->wc_data->get_shipping_class_id() ) ? $this->wc_data->get_shipping_class_id() : null;

includes/model/class-product.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ protected function init() {
196196
return ! empty( $this->wc_data->get_id() ) ? $this->wc_data->get_id() : null;
197197
},
198198
'id' => function () {
199-
return ! empty( $this->ID ) ? Relay::toGlobalId( 'product', "{$this->ID}" ) : null;
199+
return ! empty( $this->ID ) ? Relay::toGlobalId( 'post', "{$this->ID}" ) : null;
200200
},
201201
'type' => function () {
202202
return ! empty( $this->wc_data->get_type() ) ? $this->wc_data->get_type() : null;

includes/type/interface/class-product-variation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public static function register_interface(): void {
5050
'SimpleProductVariation',
5151
[
5252
'eagerlyLoadType' => true,
53+
'model' => \WPGraphQL\WooCommerce\Model\Product_Variation::class,
5354
'description' => __( 'A product variation', 'wp-graphql-woocommerce' ),
5455
'interfaces' => [ 'Node', 'ProductVariation' ],
5556
'fields' => [],

includes/type/object/class-product-types.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ private static function register_simple_product_type() {
6868
'SimpleProduct',
6969
[
7070
'eagerlyLoadType' => true,
71+
'model' => \WPGraphQL\WooCommerce\Model\Product::class,
7172
'description' => __( 'A simple product object', 'wp-graphql-woocommerce' ),
7273
'interfaces' => self::get_product_interfaces(
7374
[
@@ -92,6 +93,7 @@ private static function register_variable_product_type() {
9293
'VariableProduct',
9394
[
9495
'eagerlyLoadType' => true,
96+
'model' => \WPGraphQL\WooCommerce\Model\Product::class,
9597
'description' => __( 'A variable product object', 'wp-graphql-woocommerce' ),
9698
'interfaces' => self::get_product_interfaces(
9799
[
@@ -116,6 +118,7 @@ private static function register_external_product_type() {
116118
'ExternalProduct',
117119
[
118120
'eagerlyLoadType' => true,
121+
'model' => \WPGraphQL\WooCommerce\Model\Product::class,
119122
'description' => __( 'A external product object', 'wp-graphql-woocommerce' ),
120123
'interfaces' => self::get_product_interfaces( [ 'ProductWithPricing' ] ),
121124
'fields' => array_merge(
@@ -144,6 +147,7 @@ private static function register_group_product_type() {
144147
'GroupProduct',
145148
[
146149
'eagerlyLoadType' => true,
150+
'model' => \WPGraphQL\WooCommerce\Model\Product::class,
147151
'description' => __( 'A group product object', 'wp-graphql-woocommerce' ),
148152
'interfaces' => self::get_product_interfaces( [ 'ProductWithPricing' ] ),
149153
'fields' => [
@@ -208,6 +212,7 @@ private static function register_unsupported_product_type() {
208212
WooGraphQL::get_supported_product_type(),
209213
[
210214
'eagerlyLoadType' => true,
215+
'model' => \WPGraphQL\WooCommerce\Model\Product::class,
211216
'description' => __( 'A product object for a product type that is unsupported by the current API.', 'wp-graphql-woocommerce' ),
212217
'interfaces' => self::get_product_interfaces(
213218
[

tests/_support/Helper/crud-helpers/cart.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,15 @@ public function print_item_query( $key ) {
119119
'key' => $item['key'],
120120
'product' => array(
121121
'node' => array(
122-
'id' => Relay::toGlobalId( 'product', $item['product_id'] ),
122+
'id' => Relay::toGlobalId( 'post', $item['product_id'] ),
123123
'databaseId' => $item['product_id'],
124124
),
125125
),
126126
'variation' => ! empty( $variation )
127127
? array(
128128
'attributes' => $attributes,
129129
'node' => array(
130-
'id' => Relay::toGlobalId( 'product_variation', $item['variation_id'] ),
130+
'id' => Relay::toGlobalId( 'post', $item['variation_id'] ),
131131
'databaseId' => $item['variation_id'],
132132
),
133133
)

tests/_support/Helper/crud-helpers/customer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public function __construct() {
1010
}
1111

1212
public function to_relay_id( $id ) {
13-
return Relay::toGlobalId( 'customer', $id );
13+
return Relay::toGlobalId( 'user', $id );
1414
}
1515

1616
public function create( $args = array() ) {

tests/_support/Helper/crud-helpers/order.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public function print_query( $id ) {
205205
'pricesIncludeTax' => $data->get_prices_include_tax(),
206206
'parent' => null,
207207
'customer' => ! empty( $data->get_customer_id() )
208-
? array( 'id' => Relay::toGlobalId( 'customer', $data->get_customer_id() ) )
208+
? array( 'id' => Relay::toGlobalId( 'user', $data->get_customer_id() ) )
209209
: null,
210210
'customerIpAddress' => ! empty( $data->get_customer_ip_address() )
211211
? $data->get_customer_ip_address()

0 commit comments

Comments
 (0)