@@ -130,32 +130,29 @@ public static function register_post_types( $args, $post_type ) {
130130 $ args ['graphql_interfaces ' ] = [ 'ContentNode ' ];
131131 $ args ['graphql_register_root_field ' ] = false ;
132132 $ args ['graphql_register_root_connection ' ] = false ;
133- $ args ['graphql_resolve_type ' ] = static function ( $ value ) {
134- $ type_registry = \WPGraphQL::get_type_registry ();
135- $ possible_types = WooGraphQL::get_enabled_product_types ();
136- $ product_type = $ value ->get_type ();
137- if ( isset ( $ possible_types [ $ product_type ] ) ) {
138- return $ type_registry ->get_type ( $ possible_types [ $ product_type ] );
139- } elseif ( 'on ' === woographql_setting ( 'enable_unsupported_product_type ' , 'off ' ) ) {
140- $ unsupported_type = WooGraphQL::get_supported_product_type ();
141- return $ type_registry ->get_type ( $ unsupported_type );
142- }
143-
144- throw new UserError (
145- sprintf (
146- /* translators: %s: Product type */
147- __ ( 'The "%s" product type is not supported by the core WPGraphQL for WooCommerce (WooGraphQL) schema. ' , 'wp-graphql-woocommerce ' ),
148- $ value ->type
149- )
150- );
151- };
152- }//end if
133+ $ args ['graphql_resolve_type ' ] = [ self ::class, 'resolve_product_type ' ];
134+ }
153135 if ( 'product_variation ' === $ post_type ) {
154- $ args ['show_in_graphql ' ] = true ;
155- $ args ['graphql_single_name ' ] = 'ProductVariation ' ;
156- $ args ['graphql_plural_name ' ] = 'ProductVariations ' ;
157- $ args ['publicly_queryable ' ] = true ;
158- $ args ['skip_graphql_type_registry ' ] = true ;
136+ $ args ['show_in_graphql ' ] = true ;
137+ $ args ['model ' ] = \WPGraphQL \WooCommerce \Model \Product_Variation::class;
138+ $ args ['graphql_single_name ' ] = 'ProductVariation ' ;
139+ $ args ['graphql_plural_name ' ] = 'ProductVariations ' ;
140+ $ args ['publicly_queryable ' ] = true ;
141+ $ args ['graphql_kind ' ] = 'interface ' ;
142+ $ args ['graphql_interfaces ' ] = [
143+ 'Node ' ,
144+ 'NodeWithFeaturedImage ' ,
145+ 'ContentNode ' ,
146+ 'UniformResourceIdentifiable ' ,
147+ 'ProductUnion ' ,
148+ 'ProductWithPricing ' ,
149+ 'ProductWithDimensions ' ,
150+ 'InventoriedProduct ' ,
151+ 'DownloadableProduct ' ,
152+ ];
153+ $ args ['graphql_register_root_field ' ] = false ;
154+ $ args ['graphql_register_root_connection ' ] = false ;
155+ $ args ['graphql_resolve_type ' ] = [ self ::class, 'resolve_product_variation_type ' ];
159156 }
160157 if ( 'shop_coupon ' === $ post_type ) {
161158 $ args ['show_in_graphql ' ] = true ;
@@ -373,22 +370,7 @@ public static function inject_type_resolver( $type, $value ) {
373370 $ type = self ::resolve_product_variation_type ( $ value );
374371 break ;
375372 case 'Product ' :
376- $ supported_types = WooGraphQL::get_enabled_product_types ();
377- if ( in_array ( $ value ->type , array_keys ( $ supported_types ), true ) ) {
378- $ type_name = $ supported_types [ $ value ->type ];
379- $ type = $ type_registry ->get_type ( $ type_name );
380- } elseif ( 'on ' === woographql_setting ( 'enable_unsupported_product_type ' , 'off ' ) ) {
381- $ type_name = WooGraphQL::get_supported_product_type ();
382- $ type = $ type_registry ->get_type ( $ type_name );
383- } else {
384- throw new UserError (
385- sprintf (
386- /* translators: %s: Product type */
387- __ ( 'The "%s" product type is not supported by the core WPGraphQL for WooCommerce (WooGraphQL) schema. ' , 'wp-graphql-woocommerce ' ),
388- $ value ->type
389- )
390- );
391- }
373+ $ type = self ::resolve_product_type ( $ value );
392374 }//end switch
393375
394376 return $ type ;
@@ -397,7 +379,7 @@ public static function inject_type_resolver( $type, $value ) {
397379 /**
398380 * Resolves GraphQL type for provided product model.
399381 *
400- * @param \WPGraphQL\WooCommerce\Model\Product $value Product model.
382+ * @param \WPGraphQL\WooCommerce\Model\Product|\WPGraphQL\WooCommerce\Model\Product_Variation $value Product model.
401383 *
402384 * @throws \GraphQL\Error\UserError Invalid product type requested.
403385 *
@@ -409,7 +391,7 @@ public static function resolve_product_type( $value ) {
409391 $ product_type = $ value ->get_type ();
410392 if ( isset ( $ possible_types [ $ product_type ] ) ) {
411393 return $ type_registry ->get_type ( $ possible_types [ $ product_type ] );
412- } elseif ( str_ends_with ( $ product_type , ' variation ' ) ) {
394+ } elseif ( $ value instanceof \ WPGraphQL \ WooCommerce \ Model \Product_Variation ) {
413395 return self ::resolve_product_variation_type ( $ value );
414396 } elseif ( 'on ' === woographql_setting ( 'enable_unsupported_product_type ' , 'off ' ) ) {
415397 $ unsupported_type = WooGraphQL::get_supported_product_type ();
@@ -428,7 +410,7 @@ public static function resolve_product_type( $value ) {
428410 /**
429411 * Resolves GraphQL type for provided product variation model.
430412 *
431- * @param \WPGraphQL\WooCommerce\Model\Product $value Product model.
413+ * @param \WPGraphQL\WooCommerce\Model\Product_Variation $value Product model.
432414 *
433415 * @throws \GraphQL\Error\UserError Invalid product type requested.
434416 *
0 commit comments