Skip to content

Commit 0b8ce4f

Browse files
jasonbahlkidunot89
andauthored
fix: Connections need to connect to Types that implement the Node interface (#675)
* - implement Node on different Types * - update class-customers to return arrays for edges and nodes - move id resolver from product-attribute interface to the specific types * - return empty arrays for edges/nodes for coupon connections - update test to check for falsy vs null * - remove manual registration of the `Product` Interface - register the fields to the Product Interface * - fix code style * - fix code style * - no longer pass type registry to Product::register_interface() * - use post type registry to register the Product type as an Interface * WPGraphQL v1.13.x support added. * WPGraphQL v1.13.x support added. * fix: filter corrected. * fix: Needed code restored * fix: Needed code restored * parent field no longer overwritten * chore: WPCS compliance met. Co-authored-by: Geoff Taylor <[email protected]>
1 parent 25a5734 commit 0b8ce4f

23 files changed

+118
-116
lines changed

bin/_env.sh

100644100755
File mode changed.

bin/_lib.sh

100644100755
File mode changed.

bin/codecept

100644100755
File mode changed.

bin/entrypoint.sh

100644100755
File mode changed.

bin/install-test-env.local.sh

100644100755
File mode changed.

bin/remove-testing-library.local.sh

100644100755
File mode changed.

bin/remove-wordpress.local.sh

100644100755
File mode changed.

includes/class-core-schema-filters.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace WPGraphQL\WooCommerce;
1010

11+
use GraphQL\Error\UserError;
1112
use WPGraphQL\WooCommerce\Data\Loader\WC_Customer_Loader;
1213
use WPGraphQL\WooCommerce\Data\Loader\WC_CPT_Loader;
1314
use WPGraphQL\WooCommerce\Data\Loader\WC_Db_Loader;
@@ -134,11 +135,27 @@ public static function add_filters() {
134135
*/
135136
public static function register_post_types( $args, $post_type ) {
136137
if ( 'product' === $post_type ) {
137-
$args['show_in_graphql'] = true;
138-
$args['graphql_single_name'] = 'Product';
139-
$args['graphql_plural_name'] = 'Products';
140-
$args['skip_graphql_type_registry'] = true;
141-
}
138+
$args['show_in_graphql'] = true;
139+
$args['graphql_single_name'] = 'Product';
140+
$args['graphql_plural_name'] = 'Products';
141+
$args['graphql_kind'] = 'interface';
142+
$args['graphql_register_root_field'] = false;
143+
$args['graphql_register_root_connection'] = false;
144+
$args['graphql_resolve_type'] = static function( $value ) {
145+
$type_registry = \WPGraphQL::get_type_registry();
146+
$possible_types = WP_GraphQL_WooCommerce::get_enabled_product_types();
147+
if ( isset( $possible_types[ $value->type ] ) ) {
148+
return $type_registry->get_type( $possible_types[ $value->type ] );
149+
}
150+
throw new UserError(
151+
sprintf(
152+
/* translators: %s: Product type */
153+
__( 'The "%s" product type is not supported by the core WPGraphQL WooCommerce (WooGraphQL) schema.', 'wp-graphql-woocommerce' ),
154+
$value->type
155+
)
156+
);
157+
};
158+
}//end if
142159
if ( 'product_variation' === $post_type ) {
143160
$args['show_in_graphql'] = true;
144161
$args['graphql_single_name'] = 'ProductVariation';

includes/class-type-registry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function init( \WPGraphQL\Registry\TypeRegistry $type_registry ) {
5959
Type\WPInputObject\Orderby_Inputs::register();
6060

6161
// Interfaces.
62-
Type\WPInterface\Product::register_interface( $type_registry );
62+
Type\WPInterface\Product::register_interface();
6363
Type\WPInterface\Attribute::register_interface( $type_registry );
6464
Type\WPInterface\Product_Attribute::register_interface( $type_registry );
6565
Type\WPInterface\Cart_Error::register_interface( $type_registry );

includes/connection/class-coupons.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ public static function get_connection_config( $args = [] ): array {
4545
$resolver = new PostObjectConnectionResolver( $source, $args, $context, $info, 'shop_coupon' );
4646

4747
if ( ! self::should_execute() ) {
48-
return [];
48+
return [
49+
'edges' => [],
50+
'nodes' => [],
51+
];
4952
}
5053

5154
return $resolver->get_connection();

0 commit comments

Comments
 (0)