@@ -130,32 +130,29 @@ public static function register_post_types( $args, $post_type ) {
130
130
$ args ['graphql_interfaces ' ] = [ 'ContentNode ' ];
131
131
$ args ['graphql_register_root_field ' ] = false ;
132
132
$ 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
+ }
153
135
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 ' ];
159
156
}
160
157
if ( 'shop_coupon ' === $ post_type ) {
161
158
$ args ['show_in_graphql ' ] = true ;
@@ -373,22 +370,7 @@ public static function inject_type_resolver( $type, $value ) {
373
370
$ type = self ::resolve_product_variation_type ( $ value );
374
371
break ;
375
372
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 );
392
374
}//end switch
393
375
394
376
return $ type ;
@@ -397,7 +379,7 @@ public static function inject_type_resolver( $type, $value ) {
397
379
/**
398
380
* Resolves GraphQL type for provided product model.
399
381
*
400
- * @param \WPGraphQL\WooCommerce\Model\Product $value Product model.
382
+ * @param \WPGraphQL\WooCommerce\Model\Product|\WPGraphQL\WooCommerce\Model\Product_Variation $value Product model.
401
383
*
402
384
* @throws \GraphQL\Error\UserError Invalid product type requested.
403
385
*
@@ -409,7 +391,7 @@ public static function resolve_product_type( $value ) {
409
391
$ product_type = $ value ->get_type ();
410
392
if ( isset ( $ possible_types [ $ product_type ] ) ) {
411
393
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 ) {
413
395
return self ::resolve_product_variation_type ( $ value );
414
396
} elseif ( 'on ' === woographql_setting ( 'enable_unsupported_product_type ' , 'off ' ) ) {
415
397
$ unsupported_type = WooGraphQL::get_supported_product_type ();
@@ -428,7 +410,7 @@ public static function resolve_product_type( $value ) {
428
410
/**
429
411
* Resolves GraphQL type for provided product variation model.
430
412
*
431
- * @param \WPGraphQL\WooCommerce\Model\Product $value Product model.
413
+ * @param \WPGraphQL\WooCommerce\Model\Product_Variation $value Product model.
432
414
*
433
415
* @throws \GraphQL\Error\UserError Invalid product type requested.
434
416
*
0 commit comments