Skip to content

Commit

Permalink
feat: ProductWithAttributes and ProductVariation interfaces added
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 committed Sep 16, 2023
1 parent 481c12c commit 996ddd9
Show file tree
Hide file tree
Showing 15 changed files with 568 additions and 100 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
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
65 changes: 63 additions & 2 deletions includes/type/interface/class-product-union.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace WPGraphQL\WooCommerce\Type\WPInterface;

use WPGraphQL\AppContext;
use WPGraphQL\WooCommerce\Core_Schema_Filters as Core;

/**
Expand Down Expand Up @@ -40,14 +41,74 @@ public static function register_interface(): void {
public static function get_fields() {
return array_merge(
[
'id' => [
'id' => [
'type' => [ 'non_null' => 'ID' ],
'description' => __( 'Product or variation global ID', 'wp-graphql-woocommerce' ),
],
'databaseId' => [
'databaseId' => [
'type' => [ 'non_null' => 'Int' ],
'description' => __( 'Product or variation ID', 'wp-graphql-woocommerce' ),
],
'slug' => [
'type' => 'String',
'description' => __( 'Product slug', 'wp-graphql-woocommerce' ),
],
'type' => [
'type' => 'ProductTypesEnum',
'description' => __( 'Product type', 'wp-graphql-woocommerce' ),
],
'name' => [
'type' => 'String',
'description' => __( 'Product name', 'wp-graphql-woocommerce' ),
],
'featured' => [
'type' => 'Boolean',
'description' => __( 'If the product is featured', 'wp-graphql-woocommerce' ),
],
'catalogVisibility' => [
'type' => 'CatalogVisibilityEnum',
'description' => __( 'Catalog visibility', 'wp-graphql-woocommerce' ),
],
'sku' => [
'type' => 'String',
'description' => __( 'Product SKU', 'wp-graphql-woocommerce' ),
],
'description' => [
'type' => 'String',
'description' => __( 'Product description', 'wp-graphql-woocommerce' ),
'args' => [
'format' => [
'type' => 'PostObjectFieldFormatEnum',
'description' => __( 'Format of the field output', 'wp-graphql-woocommerce' ),
],
],
'resolve' => static function ( $source, $args ) {
if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
// @codingStandardsIgnoreLine.
return $source->descriptionRaw;
}
return $source->description;
},
],
'image' => [
'type' => 'MediaItem',
'description' => __( 'Main image', 'wp-graphql-woocommerce' ),
'resolve' => static function ( $source, array $args, AppContext $context ) {
// @codingStandardsIgnoreLine.
if ( empty( $source->image_id ) || ! absint( $source->image_id ) ) {
return null;
}
return $context->get_loader( 'post' )->load_deferred( $source->image_id );
},
],
'onSale' => [
'type' => 'Boolean',
'description' => __( 'Is product on sale?', 'wp-graphql-woocommerce' ),
],
'purchasable' => [
'type' => 'Boolean',
'description' => __( 'Can product be purchased?', 'wp-graphql-woocommerce' ),
],
],
Product::get_fields()
);
Expand Down
Loading

0 comments on commit 996ddd9

Please sign in to comment.