Skip to content

Commit 74eb1e1

Browse files
committed
"WPUnionType" injection filters implemented.
1 parent 5b13160 commit 74eb1e1

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

includes/class-core-schema-filters.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use WPGraphQL\WooCommerce\Data\Loader\WC_Customer_Loader;
1212
use WPGraphQL\WooCommerce\Data\Loader\WC_Post_Crud_Loader;
13+
use WPGraphQL\WooCommerce\Data\Factory;
1314

1415
/**
1516
* Class Core_Schema_Filters
@@ -76,6 +77,21 @@ public static function add_filters() {
7677
10,
7778
2
7879
);
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+
);
7995
}
8096

8197
/**
@@ -238,4 +254,62 @@ public static function graphql_data_loaders( $loaders, $context ) {
238254

239255
return $loaders;
240256
}
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+
}
241315
}

0 commit comments

Comments
 (0)