Skip to content

Commit

Permalink
feat: ProductWithAttributes and ProductVariation interfaces added (#803)
Browse files Browse the repository at this point in the history
* feat: ProductWithAttributes and ProductVariation interfaces added

* chore: Unused files removed
  • Loading branch information
kidunot89 authored Sep 16, 2023
1 parent 481c12c commit c526a5b
Show file tree
Hide file tree
Showing 17 changed files with 568 additions and 412 deletions.
28 changes: 27 additions & 1 deletion includes/class-core-schema-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ public static function resolve_product_type( $value ) {
if ( isset( $possible_types[ $product_type ] ) ) {
return $type_registry->get_type( $possible_types[ $product_type ] );
} elseif ( str_ends_with( $product_type, 'variation' ) ) {
return $type_registry->get_type( 'ProductVariation' );
return self::resolve_product_variation_type( $value );
} elseif ( 'on' === woographql_setting( 'enable_unsupported_product_type', 'off' ) ) {
$unsupported_type = WooGraphQL::get_supported_product_type();
return $type_registry->get_type( $unsupported_type );
Expand All @@ -415,4 +415,30 @@ public static function resolve_product_type( $value ) {
)
);
}

/**
* Resolves GraphQL type for provided product variation model.
*
* @param \WPGraphQL\WooCommerce\Model\Product $value Product model.
*
* @throws \GraphQL\Error\UserError Invalid product type requested.
*
* @return mixed
*/
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 ] );
}

throw new UserError(
sprintf(
/* translators: %s: Product type */
__( 'The "%s" product variation type is not supported by the core WPGraphQL WooCommerce (WooGraphQL) schema.', 'wp-graphql-woocommerce' ),
$value->type
)
);
}
}
14 changes: 7 additions & 7 deletions includes/class-type-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,19 @@ public function init() {
* Interfaces.
*/
Type\WPInterface\Product::register_interface();
Type\WPInterface\Product_Variation::register_interface();
Type\WPInterface\Attribute::register_interface();
Type\WPInterface\Product_Attribute::register_interface();
Type\WPInterface\Cart_Error::register_interface();
Type\WPInterface\Payment_Token::register_interface();
Type\WPInterface\Product_Union::register_interface();
Type\WPInterface\Cart_Item::register_interface();
Type\WPInterface\Downloadable_Products::register_interface();
Type\WPInterface\Inventoried_Products::register_interface();
Type\WPInterface\Products_With_Dimensions::register_interface();
Type\WPInterface\Products_With_Pricing::register_interface();
Type\WPInterface\Products_With_Variations::register_interface();
Type\WPInterface\Downloadable_Product::register_interface();
Type\WPInterface\Inventoried_Product::register_interface();
Type\WPInterface\Product_With_Dimensions::register_interface();
Type\WPInterface\Product_With_Pricing::register_interface();
Type\WPInterface\Product_With_Variations::register_interface();
Type\WPInterface\Product_With_Attributes::register_interface();

/**
* Objects.
Expand All @@ -85,7 +87,6 @@ public function init() {
Type\WPObject\Coupon_Type::register();
Type\WPObject\Product_Types::register();
Type\WPObject\Product_Attribute_Types::register();
Type\WPObject\Product_Variation_Type::register();
Type\WPObject\Order_Item_Type::register();
Type\WPObject\Order_Type::register();
Type\WPObject\Refund_Type::register();
Expand Down Expand Up @@ -132,7 +133,6 @@ public function init() {
Connection\Products::register_connections();
Connection\Orders::register_connections();
Connection\Product_Attributes::register_connections();
Connection\Variation_Attributes::register_connections();
Connection\Customers::register_connections();
Connection\Tax_Rates::register_connections();
Connection\Shipping_Methods::register_connections();
Expand Down
16 changes: 8 additions & 8 deletions includes/class-wp-graphql-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static function get_enabled_product_types() {
* @return array
*/
public static function get_enabled_product_variation_types() {
return apply_filters( 'graphql_woocommerce_product_variation_types', [ 'variation' => 'ProductVariation' ] );
return apply_filters( 'graphql_woocommerce_product_variation_types', [ 'variation' => 'SimpleProductVariation' ] );
}

/**
Expand Down Expand Up @@ -242,14 +242,16 @@ private function includes() {
require $include_directory_path . 'type/interface/class-cart-error.php';
require $include_directory_path . 'type/interface/class-product-attribute.php';
require $include_directory_path . 'type/interface/class-product.php';
require $include_directory_path . 'type/interface/class-product-variation.php';
require $include_directory_path . 'type/interface/class-payment-token.php';
require $include_directory_path . 'type/interface/class-product-union.php';
require $include_directory_path . 'type/interface/class-cart-item.php';
require $include_directory_path . 'type/interface/class-downloadable-products.php';
require $include_directory_path . 'type/interface/class-inventoried-products.php';
require $include_directory_path . 'type/interface/class-products-with-dimensions.php';
require $include_directory_path . 'type/interface/class-products-with-pricing.php';
require $include_directory_path . 'type/interface/class-products-with-variations.php';
require $include_directory_path . 'type/interface/class-downloadable-product.php';
require $include_directory_path . 'type/interface/class-inventoried-product.php';
require $include_directory_path . 'type/interface/class-product-with-dimensions.php';
require $include_directory_path . 'type/interface/class-product-with-pricing.php';
require $include_directory_path . 'type/interface/class-product-with-variations.php';
require $include_directory_path . 'type/interface/class-product-with-attributes.php';

// Include object type class files.
require $include_directory_path . 'type/object/class-cart-error-types.php';
Expand All @@ -266,7 +268,6 @@ private function includes() {
require $include_directory_path . 'type/object/class-product-category-type.php';
require $include_directory_path . 'type/object/class-product-download-type.php';
require $include_directory_path . 'type/object/class-product-types.php';
require $include_directory_path . 'type/object/class-product-variation-type.php';
require $include_directory_path . 'type/object/class-refund-type.php';
require $include_directory_path . 'type/object/class-root-query.php';
require $include_directory_path . 'type/object/class-shipping-method-type.php';
Expand Down Expand Up @@ -334,7 +335,6 @@ private function includes() {
require $include_directory_path . 'connection/class-products.php';
require $include_directory_path . 'connection/class-shipping-methods.php';
require $include_directory_path . 'connection/class-tax-rates.php';
require $include_directory_path . 'connection/class-variation-attributes.php';
require $include_directory_path . 'connection/class-wc-terms.php';

// Include admin files.
Expand Down
25 changes: 20 additions & 5 deletions includes/connection/class-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,12 @@ public static function set_connection_config( $config ) {
public static function get_connection_config( $args = [] ): array {
return array_merge(
[
'fromType' => 'RootQuery',
'toType' => 'ProductUnion',
'fromFieldName' => 'products',
'connectionArgs' => self::get_connection_args(),
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
'fromType' => 'RootQuery',
'toType' => 'ProductUnion',
'fromFieldName' => 'products',
'connectionArgs' => self::get_connection_args(),
'connectionFields' => self::get_connection_fields(),
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
$resolver = new Product_Connection_Resolver( $source, $args, $context, $info );

return $resolver->get_connection();
Expand All @@ -272,6 +273,20 @@ public static function get_connection_config( $args = [] ): array {
);
}

/**
* Returns array of edge fields.
*
* @return array
*/
public static function get_connection_fields(): array {
return [
'found' => [
'type' => 'Number',
'description' => __( 'Total products founds', 'wp-graphql-woocommerce' ),
],
];
}

/**
* Returns array of where args.
*
Expand Down
68 changes: 0 additions & 68 deletions includes/connection/class-variation-attributes.php

This file was deleted.

2 changes: 1 addition & 1 deletion includes/model/class-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ protected function init() {
) {
$fields += [
'manageStock' => function () {
return $this->wc_data->get_manage_stock();
return ! empty( $this->wc_data->get_manage_stock() ) ? $this->wc_data->get_manage_stock() : null;
},
'stockQuantity' => function () {
return ! empty( $this->wc_data->get_stock_quantity() ) ? $this->wc_data->get_stock_quantity() : null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Defines the fields for downloadable products.
* Defines the "DownloadableProduct" interface.
*
* @package WPGraphQL\WooCommerce\Type\WPInterface
* @since TBD
Expand All @@ -11,20 +11,20 @@
use WPGraphQL\WooCommerce\Core_Schema_Filters as Core;

/**
* Class Downloadable_Products
* Class Downloadable_Product
*/
class Downloadable_Products {
class Downloadable_Product {
/**
* Registers the "DownloadableProducts" type
* Registers the "DownloadableProduct" type
*
* @return void
* @throws \Exception
*/
public static function register_interface(): void {
register_graphql_interface_type(
'DownloadableProducts',
'DownloadableProduct',
[
'description' => __( 'Downloadable products.', 'wp-graphql-woocommerce' ),
'description' => __( 'A downloadable product.', 'wp-graphql-woocommerce' ),
'interfaces' => [ 'Node' ],
'fields' => self::get_fields(),
'resolveType' => [ Core::class, 'resolve_product_type' ],
Expand All @@ -33,7 +33,7 @@ public static function register_interface(): void {
}

/**
* Defines "DownloadableProducts" fields.
* Defines fields of "DownloadableProduct".
*
* @return array
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Defines the fields for manage product inventories.
* Defines the "InventoriedProduct" interface.
*
* @package WPGraphQL\WooCommerce\Type\WPInterface
* @since TBD
Expand All @@ -11,20 +11,20 @@
use WPGraphQL\WooCommerce\Core_Schema_Filters as Core;

/**
* Class Inventoried_Products
* Class Inventoried_Product
*/
class Inventoried_Products {
class Inventoried_Product {
/**
* Registers the "InventoriedProducts" type
* Registers the "InventoriedProduct" type
*
* @return void
* @throws \Exception
*/
public static function register_interface(): void {
register_graphql_interface_type(
'InventoriedProducts',
'InventoriedProduct',
[
'description' => __( 'Products with stock information.', 'wp-graphql-woocommerce' ),
'description' => __( 'A product with stock information.', 'wp-graphql-woocommerce' ),
'interfaces' => [ 'Node' ],
'fields' => self::get_fields(),
'resolveType' => [ Core::class, 'resolve_product_type' ],
Expand All @@ -33,7 +33,7 @@ public static function register_interface(): void {
}

/**
* Defines "InventoriedProducts" fields.
* Defines fields of "InventoriedProduct".
*
* @return array
*/
Expand All @@ -48,7 +48,7 @@ public static function get_fields() {
'description' => __( 'Product or variation ID', 'wp-graphql-woocommerce' ),
],
'manageStock' => [
'type' => 'Boolean',
'type' => 'ManageStockEnum',
'description' => __( 'If product manage stock', 'wp-graphql-woocommerce' ),
],
'stockQuantity' => [
Expand Down
Loading

0 comments on commit c526a5b

Please sign in to comment.