Skip to content

Commit 22b03e4

Browse files
authored
fix: "Product" interfaces shared fields fix (#901)
* fix: "Product" interface applied to all sub Product interfaces for more optimal schema shape * fix: more inconsistencies fixed. * feat: "ProductWithAttributes" interface radded to "ProductWithVariations" interface
1 parent 8fc7986 commit 22b03e4

14 files changed

+112
-77
lines changed

composer.lock

Lines changed: 39 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/connection/class-orders.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,11 @@ private static function get_customer_refund_connection( $resolver, $customer ) {
146146
$customer_id = $customer->get_id();
147147
$billing_email = $customer->get_billing_email();
148148
if ( ! empty( $customer_id ) ) {
149-
$args = [
149+
$args = [
150150
'customer_id' => $customer_id,
151151
'return' => 'ids',
152152
];
153+
/** @var array<int> $order_ids_by_customer_id */
153154
$order_ids_by_customer_id = wc_get_orders( $args );
154155

155156
if ( is_array( $order_ids_by_customer_id ) ) {
@@ -158,10 +159,11 @@ private static function get_customer_refund_connection( $resolver, $customer ) {
158159
}
159160

160161
if ( ! empty( $billing_email ) ) {
161-
$args = [
162+
$args = [
162163
'billing_email' => $billing_email,
163164
'return' => 'ids',
164165
];
166+
/** @var array<int> $order_ids_by_email */
165167
$order_ids_by_email = wc_get_orders( $args );
166168
// Merge the arrays of order IDs.
167169
if ( is_array( $order_ids_by_email ) ) {
@@ -182,6 +184,7 @@ private static function get_customer_refund_connection( $resolver, $customer ) {
182184
( 0 !== $customer_id && \WC()->customer->get_id() === $customer_id )
183185
|| \WC()->customer->get_billing_email() === $billing_email
184186
);
187+
185188
$resolver->set_query_arg( 'post_parent__in', array_map( 'absint', $order_ids ) );
186189

187190
// Execute and return connection.

includes/connection/class-product-attributes.php

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace WPGraphQL\WooCommerce\Connection;
1212

13+
use GraphQL\Error\Error;
1314
use GraphQL\Type\Definition\ResolveInfo;
1415
use WPGraphQL\AppContext;
1516
use WPGraphQL\WooCommerce\Data\Connection\Product_Attribute_Connection_Resolver;
@@ -24,15 +25,11 @@ class Product_Attributes {
2425
* @return void
2526
*/
2627
public static function register_connections() {
27-
// From Product to ProductAttribute.
28-
register_graphql_connection(
29-
self::get_connection_config()
30-
);
31-
3228
// From Product to LocalProductAttribute.
3329
register_graphql_connection(
3430
self::get_connection_config(
3531
[
32+
'fromType' => 'Product',
3633
'toType' => 'LocalProductAttribute',
3734
'fromFieldName' => 'localAttributes',
3835
'connectionArgs' => [],
@@ -44,6 +41,7 @@ public static function register_connections() {
4441
register_graphql_connection(
4542
self::get_connection_config(
4643
[
44+
'fromType' => 'Product',
4745
'toType' => 'GlobalProductAttribute',
4846
'fromFieldName' => 'globalAttributes',
4947
'connectionArgs' => [],
@@ -57,15 +55,23 @@ public static function register_connections() {
5755
* with the defaults.
5856
*
5957
* @param array $args - Connection configuration.
58+
* @throws \GraphQL\Error\Error If the "fromType" or "toType" is not provided.
59+
*
6060
* @return array
6161
*/
6262
public static function get_connection_config( $args = [] ): array {
63+
if ( ! isset( $args['fromType'] ) ) {
64+
throw new Error( __( 'The "fromType" is required for the ProductAttributes connection.', 'wp-graphql-woocommerce' ) );
65+
}
66+
67+
if ( ! isset( $args['toType'] ) ) {
68+
throw new Error( __( 'The "toType" is required for the ProductAttributes connection.', 'wp-graphql-woocommerce' ) );
69+
}
70+
6371
return array_merge(
6472
[
65-
'fromType' => 'Product',
66-
'toType' => 'ProductAttribute',
6773
'fromFieldName' => 'attributes',
68-
'connectionArgs' => self::get_connection_args(),
74+
'connectionArgs' => [],
6975
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
7076
$resolver = new Product_Attribute_Connection_Resolver();
7177
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
@@ -82,18 +88,4 @@ public static function get_connection_config( $args = [] ): array {
8288
$args
8389
);
8490
}
85-
86-
/**
87-
* Returns array of where args.
88-
*
89-
* @return array
90-
*/
91-
public static function get_connection_args(): array {
92-
return [
93-
'type' => [
94-
'type' => 'ProductAttributeTypesEnum',
95-
'description' => __( 'Filter results by attribute scope.', 'wp-graphql-woocommerce' ),
96-
],
97-
];
98-
}
9991
}

includes/connection/class-products.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ static function () {
148148

149149
register_graphql_connection(
150150
[
151-
'fromType' => 'ProductVariation',
152-
'toType' => 'VariableProduct',
151+
'fromType' => 'Product',
152+
'toType' => 'Product',
153153
'fromFieldName' => 'parent',
154154
'description' => __( 'The parent of the node. The parent object can be of various types', 'wp-graphql-woocommerce' ),
155155
'oneToOne' => true,

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static function register_interface(): void {
2525
'DownloadableProduct',
2626
[
2727
'description' => __( 'A downloadable product.', 'wp-graphql-woocommerce' ),
28-
'interfaces' => [ 'Node' ],
28+
'interfaces' => [ 'Node', 'Product' ],
2929
'fields' => self::get_fields(),
3030
'resolveType' => [ Core::class, 'resolve_product_type' ],
3131
]
@@ -47,10 +47,6 @@ public static function get_fields() {
4747
'type' => [ 'non_null' => 'Int' ],
4848
'description' => __( 'Product or variation ID', 'wp-graphql-woocommerce' ),
4949
],
50-
'virtual' => [
51-
'type' => 'Boolean',
52-
'description' => __( 'Is product virtual?', 'wp-graphql-woocommerce' ),
53-
],
5450
'downloadExpiry' => [
5551
'type' => 'Int',
5652
'description' => __( 'Download expiry', 'wp-graphql-woocommerce' ),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static function register_interface(): void {
2525
'InventoriedProduct',
2626
[
2727
'description' => __( 'A product with stock information.', 'wp-graphql-woocommerce' ),
28-
'interfaces' => [ 'Node' ],
28+
'interfaces' => [ 'Node', 'Product' ],
2929
'fields' => self::get_fields(),
3030
'resolveType' => [ Core::class, 'resolve_product_type' ],
3131
]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static function register_interface(): void {
2626
'ProductUnion',
2727
[
2828
'description' => __( 'Union between the product and product variation types', 'wp-graphql-woocommerce' ),
29-
'interfaces' => [ 'Node' ],
29+
'interfaces' => [ 'Node', 'Product' ],
3030
'fields' => self::get_fields(),
3131
'resolveType' => [ Core::class, 'resolve_product_type' ],
3232
]

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
use GraphQL\Type\Definition\ResolveInfo;
1212
use WPGraphQL\AppContext;
1313
use WPGraphQL\WooCommerce\Core_Schema_Filters as Core;
14+
use WPGraphQL\WooCommerce\Data\Connection\Product_Connection_Resolver;
1415
use WPGraphQL\WooCommerce\Data\Connection\Variation_Attribute_Connection_Resolver;
1516
use WPGraphQL\WooCommerce\Type\WPObject\Meta_Data_Type;
1617

18+
1719
/**
1820
* Class Product_Variation
1921
*/
@@ -285,6 +287,22 @@ public static function get_connections() {
285287
return $resolver->resolve( $source, $args, $context, $info );
286288
},
287289
],
290+
'parent' => [
291+
'toType' => 'Product',
292+
'description' => __( 'The parent of the variation', 'wp-graphql-woocommerce' ),
293+
'oneToOne' => true,
294+
'queryClass' => '\WC_Product_Query',
295+
'resolve' => static function ( $source, $args, AppContext $context, ResolveInfo $info ) {
296+
if ( empty( $source->parent_id ) ) {
297+
return null;
298+
}
299+
300+
$resolver = new Product_Connection_Resolver( $source, $args, $context, $info );
301+
$resolver->set_query_arg( 'p', $source->parent_id );
302+
303+
return $resolver->one_to_one()->get_connection();
304+
},
305+
],
288306
];
289307
}
290308
}

0 commit comments

Comments
 (0)