Skip to content

Commit

Permalink
fix: "Product" interfaces shared fields fix (#901)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kidunot89 authored Nov 6, 2024
1 parent 8fc7986 commit 22b03e4
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 77 deletions.
74 changes: 39 additions & 35 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions includes/connection/class-orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ private static function get_customer_refund_connection( $resolver, $customer ) {
$customer_id = $customer->get_id();
$billing_email = $customer->get_billing_email();
if ( ! empty( $customer_id ) ) {
$args = [
$args = [
'customer_id' => $customer_id,
'return' => 'ids',
];
/** @var array<int> $order_ids_by_customer_id */
$order_ids_by_customer_id = wc_get_orders( $args );

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

if ( ! empty( $billing_email ) ) {
$args = [
$args = [
'billing_email' => $billing_email,
'return' => 'ids',
];
/** @var array<int> $order_ids_by_email */
$order_ids_by_email = wc_get_orders( $args );
// Merge the arrays of order IDs.
if ( is_array( $order_ids_by_email ) ) {
Expand All @@ -182,6 +184,7 @@ private static function get_customer_refund_connection( $resolver, $customer ) {
( 0 !== $customer_id && \WC()->customer->get_id() === $customer_id )
|| \WC()->customer->get_billing_email() === $billing_email
);

$resolver->set_query_arg( 'post_parent__in', array_map( 'absint', $order_ids ) );

// Execute and return connection.
Expand Down
36 changes: 14 additions & 22 deletions includes/connection/class-product-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace WPGraphQL\WooCommerce\Connection;

use GraphQL\Error\Error;
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\WooCommerce\Data\Connection\Product_Attribute_Connection_Resolver;
Expand All @@ -24,15 +25,11 @@ class Product_Attributes {
* @return void
*/
public static function register_connections() {
// From Product to ProductAttribute.
register_graphql_connection(
self::get_connection_config()
);

// From Product to LocalProductAttribute.
register_graphql_connection(
self::get_connection_config(
[
'fromType' => 'Product',
'toType' => 'LocalProductAttribute',
'fromFieldName' => 'localAttributes',
'connectionArgs' => [],
Expand All @@ -44,6 +41,7 @@ public static function register_connections() {
register_graphql_connection(
self::get_connection_config(
[
'fromType' => 'Product',
'toType' => 'GlobalProductAttribute',
'fromFieldName' => 'globalAttributes',
'connectionArgs' => [],
Expand All @@ -57,15 +55,23 @@ public static function register_connections() {
* with the defaults.
*
* @param array $args - Connection configuration.
* @throws \GraphQL\Error\Error If the "fromType" or "toType" is not provided.
*
* @return array
*/
public static function get_connection_config( $args = [] ): array {
if ( ! isset( $args['fromType'] ) ) {
throw new Error( __( 'The "fromType" is required for the ProductAttributes connection.', 'wp-graphql-woocommerce' ) );
}

if ( ! isset( $args['toType'] ) ) {
throw new Error( __( 'The "toType" is required for the ProductAttributes connection.', 'wp-graphql-woocommerce' ) );
}

return array_merge(
[
'fromType' => 'Product',
'toType' => 'ProductAttribute',
'fromFieldName' => 'attributes',
'connectionArgs' => self::get_connection_args(),
'connectionArgs' => [],
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
$resolver = new Product_Attribute_Connection_Resolver();
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
Expand All @@ -82,18 +88,4 @@ public static function get_connection_config( $args = [] ): array {
$args
);
}

/**
* Returns array of where args.
*
* @return array
*/
public static function get_connection_args(): array {
return [
'type' => [
'type' => 'ProductAttributeTypesEnum',
'description' => __( 'Filter results by attribute scope.', 'wp-graphql-woocommerce' ),
],
];
}
}
4 changes: 2 additions & 2 deletions includes/connection/class-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ static function () {

register_graphql_connection(
[
'fromType' => 'ProductVariation',
'toType' => 'VariableProduct',
'fromType' => 'Product',
'toType' => 'Product',
'fromFieldName' => 'parent',
'description' => __( 'The parent of the node. The parent object can be of various types', 'wp-graphql-woocommerce' ),
'oneToOne' => true,
Expand Down
6 changes: 1 addition & 5 deletions includes/type/interface/class-downloadable-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function register_interface(): void {
'DownloadableProduct',
[
'description' => __( 'A downloadable product.', 'wp-graphql-woocommerce' ),
'interfaces' => [ 'Node' ],
'interfaces' => [ 'Node', 'Product' ],
'fields' => self::get_fields(),
'resolveType' => [ Core::class, 'resolve_product_type' ],
]
Expand All @@ -47,10 +47,6 @@ public static function get_fields() {
'type' => [ 'non_null' => 'Int' ],
'description' => __( 'Product or variation ID', 'wp-graphql-woocommerce' ),
],
'virtual' => [
'type' => 'Boolean',
'description' => __( 'Is product virtual?', 'wp-graphql-woocommerce' ),
],
'downloadExpiry' => [
'type' => 'Int',
'description' => __( 'Download expiry', 'wp-graphql-woocommerce' ),
Expand Down
2 changes: 1 addition & 1 deletion includes/type/interface/class-inventoried-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function register_interface(): void {
'InventoriedProduct',
[
'description' => __( 'A product with stock information.', 'wp-graphql-woocommerce' ),
'interfaces' => [ 'Node' ],
'interfaces' => [ 'Node', 'Product' ],
'fields' => self::get_fields(),
'resolveType' => [ Core::class, 'resolve_product_type' ],
]
Expand Down
2 changes: 1 addition & 1 deletion includes/type/interface/class-product-union.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static function register_interface(): void {
'ProductUnion',
[
'description' => __( 'Union between the product and product variation types', 'wp-graphql-woocommerce' ),
'interfaces' => [ 'Node' ],
'interfaces' => [ 'Node', 'Product' ],
'fields' => self::get_fields(),
'resolveType' => [ Core::class, 'resolve_product_type' ],
]
Expand Down
18 changes: 18 additions & 0 deletions includes/type/interface/class-product-variation.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\WooCommerce\Core_Schema_Filters as Core;
use WPGraphQL\WooCommerce\Data\Connection\Product_Connection_Resolver;
use WPGraphQL\WooCommerce\Data\Connection\Variation_Attribute_Connection_Resolver;
use WPGraphQL\WooCommerce\Type\WPObject\Meta_Data_Type;


/**
* Class Product_Variation
*/
Expand Down Expand Up @@ -285,6 +287,22 @@ public static function get_connections() {
return $resolver->resolve( $source, $args, $context, $info );
},
],
'parent' => [
'toType' => 'Product',
'description' => __( 'The parent of the variation', 'wp-graphql-woocommerce' ),
'oneToOne' => true,
'queryClass' => '\WC_Product_Query',
'resolve' => static function ( $source, $args, AppContext $context, ResolveInfo $info ) {
if ( empty( $source->parent_id ) ) {
return null;
}

$resolver = new Product_Connection_Resolver( $source, $args, $context, $info );
$resolver->set_query_arg( 'p', $source->parent_id );

return $resolver->one_to_one()->get_connection();
},
],
];
}
}
Loading

0 comments on commit 22b03e4

Please sign in to comment.