|
10 | 10 |
|
11 | 11 | use WPGraphQL\WooCommerce\Data\Loader\WC_Customer_Loader; |
12 | 12 | use WPGraphQL\WooCommerce\Data\Loader\WC_Post_Crud_Loader; |
| 13 | +use WPGraphQL\WooCommerce\Data\Factory; |
13 | 14 |
|
14 | 15 | /** |
15 | 16 | * Class Core_Schema_Filters |
@@ -76,6 +77,21 @@ public static function add_filters() { |
76 | 77 | 10, |
77 | 78 | 2 |
78 | 79 | ); |
| 80 | + |
| 81 | + // Filter Unions. |
| 82 | + add_filter( |
| 83 | + 'graphql_wp_union_type_config', |
| 84 | + array( __CLASS__, 'inject_union_types' ), |
| 85 | + 10, |
| 86 | + 2 |
| 87 | + ); |
| 88 | + |
| 89 | + add_filter( |
| 90 | + 'graphql_union_resolve_type', |
| 91 | + array( __CLASS__, 'inject_union_type_resolver' ), |
| 92 | + 10, |
| 93 | + 3 |
| 94 | + ); |
79 | 95 | } |
80 | 96 |
|
81 | 97 | /** |
@@ -238,4 +254,62 @@ public static function graphql_data_loaders( $loaders, $context ) { |
238 | 254 |
|
239 | 255 | return $loaders; |
240 | 256 | } |
| 257 | + |
| 258 | + /** |
| 259 | + * Inject Union types that resolve to Product with Product types |
| 260 | + * |
| 261 | + * @param array $config WPUnion config. |
| 262 | + * @param \WPGraphQL\Type\WPUnionType $wp_union WPUnion object. |
| 263 | + */ |
| 264 | + public static function inject_union_types( $config, $wp_union ) { |
| 265 | + $refresh_callback = false; |
| 266 | + if ( in_array( 'Product', $config['typeNames'], true ) ) { |
| 267 | + // Strip 'Product' from config and child product types. |
| 268 | + $config['typeNames'] = array_merge( |
| 269 | + array_filter( |
| 270 | + $config['typeNames'], |
| 271 | + function( $type ) { |
| 272 | + return 'Product' !== $type; |
| 273 | + } |
| 274 | + ), |
| 275 | + array_values( \WP_GraphQL_WooCommerce::get_enabled_product_types() ) |
| 276 | + ); |
| 277 | + $refresh_callback = true; |
| 278 | + } |
| 279 | + |
| 280 | + // Update 'types' callback. |
| 281 | + if ( $refresh_callback ) { |
| 282 | + $config['types'] = function () use ( $config, $wp_union ) { |
| 283 | + $prepared_types = array(); |
| 284 | + if ( ! empty( $config['typeNames'] ) && is_array( $config['typeNames'] ) ) { |
| 285 | + $prepared_types = array(); |
| 286 | + foreach ( $config['typeNames'] as $type_name ) { |
| 287 | + $prepared_types[] = $wp_union->type_registry->get_type( $type_name ); |
| 288 | + } |
| 289 | + } |
| 290 | + return $prepared_types; |
| 291 | + }; |
| 292 | + } |
| 293 | + |
| 294 | + return $config; |
| 295 | + } |
| 296 | + |
| 297 | + /** |
| 298 | + * Inject Union type resolver that resolve to Product with Product types |
| 299 | + * |
| 300 | + * @param \WPGraphQL\Type\WPObjectType $type Type be resolve to. |
| 301 | + * @param mixed $value Object for which the type is being resolve config. |
| 302 | + * @param \WPGraphQL\Type\WPUnionType $wp_union WPUnion object. |
| 303 | + */ |
| 304 | + public static function inject_union_type_resolver( $type, $value, $wp_union ) { |
| 305 | + if ( 'product' === $value->post_type ) { |
| 306 | + $node = new \WPGraphQL\WooCommerce\Model\Product( $value->ID ); |
| 307 | + $new_type = Factory::resolve_node_type( '', $node ); |
| 308 | + if ( $new_type ) { |
| 309 | + $type = $wp_union->type_registry->get_type( $new_type ); |
| 310 | + } |
| 311 | + } |
| 312 | + |
| 313 | + return $type; |
| 314 | + } |
241 | 315 | } |
0 commit comments