-
-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error when querying products that are ACF relationship field items within a Flexible Content #449
Comments
Attempting to find a work around or at the very least a quick fix for the above issue. The Attempt add_filter('graphql_Page_Wpgraphqlexample_FlexibleContent_RelationshipField_fields', function ($fields) {
if (isset($fields['productRelationship'])) {
$fields['productRelationship']['resolve'] = function ($root, $args, $context, $info) {
return array_map(function ($id) {
return new \WPGraphQL\WooCommerce\Model\Product($id);
}, $root['field_6041494b07558']);
};
}
return $fields;
}, 10, 1); The Output Understanding this falls all over the place, (not dynamic for a start), such as:
Could I possibly have your thoughts @jasonbahl @kidunot89 |
@boxxroom You want this filter. This example should work as a workaround. function workaround( $type, $object, $wp_union ) {
if ( 'Product' === $type ) {
$type_name = \WPGraphQL\WooCommerce\Data\Factory::resolve_type( $type );
$type = $wp_union->type_registry->get_type( $type );
}
return $type;
}
add_filter( 'graphql_union_resolve_type', 'workaround', 10, 3 ); |
Thanks for the reply and the code @kidunot89 This filter To double check this, we have added your code and additional logging inside the |
@boxxroom There's an error in my example. This is the correction. function workaround( $type, $object, $wp_union ) {
if ( 'Product' === $type ) {
$type_name = \WPGraphQL\WooCommerce\Data\Factory::resolve_type( $type, $object );
$type = $wp_union->type_registry->get_type( $type );
}
return $type;
}
add_filter( 'graphql_union_resolve_type', 'workaround', 10, 3 ); |
@kidunot89 Thanks for looking in to this. Even with the updated code, the previous statement still applies with the conditional. The Also to note, we were unable to locate the method Attached below is the filter code with a Attached below is the filter code with a |
@kidunot89 We think we have found the issue and it's to do with the booking plugins. WooCommerce Accommodation Bookings: 1.1.23 WooCommerce Bookings: 1.15.31 In the admin area when editing a product and updating its Product data to add_filter('graphql_Page_Wpgraphqlexample_FlexibleContent_RelationshipField_fields', function ($fields) {
if (isset($fields['productRelationship'])) {
$fields['productRelationship']['resolve'] = function ($root, $args, $context, $info) {
return array_map(function ($id) {
return new \WPGraphQL\WooCommerce\Model\Product($id);
}, $root['field_6041494b07558']);
};
}
return $fields;
}, 10, 1); We have created two plugins to add BookingProduct and AccommodationBookingProduct as unions of a Product which can be downloaded here in an attempt to resolve this issue but the error Thanks in advance. |
@boxxroom I think I ran into something similar when working with the ProductBundle type (https://github.com/jacobarriola/woographql-product-bundles). I had to to do 2 things to get it working:
/**
* Set source Type to correct WooCommerce Type when dealing with the Relationship field.
*
* Not doing so causes GraphQL to try and resolve a WC Type (ie Product) to the Post
* model, which causes resolve errors.
*/
add_filter( 'graphql_acf_relationship_model', function ( $source, $value ) {
$post = get_post( $value );
// Bail if we're not dealing with a product
if ( $post->post_type !== 'product' ) {
return $source;
}
// Only allow published posts to go through. This causes GraphQL errors and breaks builds.
if ( 'publish' !== $post->post_status ) {
return;
}
if ( false === class_exists( 'WPGraphQL\WooCommerce\ACF_Schema_Filters' ) ) {
return $source;
}
$source = ACF_Schema_Filters::resolve_post_object_source( $source, $value );
return $source;
}, 10, 2 );
/**
* Set correct product type so that ACF relationship -> product bundles can resolve. Without this, a bundle added to
* an ACF relationship field will throw an error.
*/
add_filter( 'graphql_union_resolve_type', function ( $type, $object, $wp_union ) {
// Bail if not a Product type
if ( $type->name !== 'Product' ) {
return $type;
}
// Bail if as_WC_Data() is not available
if ( false === method_exists( $object, 'as_WC_Data') ) {
return $type;
}
// Bail if $object is not a bundle
if ( $object->as_WC_Data()->product_type !== 'bundle' ) {
return $type;
}
// Set the correct type
return $wp_union->type_registry->get_type( 'BundleProduct' );
}, 10, 3 ); |
@jacobarriola thanks very much for chiming in on this. We have this working with a tweak to the To test - We initially added the WPGraphQL ACF We then set out adding the code you provided, but this still failed with the same error output The way we got this working was to build the product from factory and check it is an instance of /**
* Set correct product type so that ACF relationship -> product accommodations can resolve. Without this, an accommodation added to
* an ACF relationship field will throw an error.
*/
add_filter('graphql_union_resolve_type', function ($type, $object, $wp_union) {
// Bail if not a Product type
if ($type->name !== 'Product') {
return $type;
}
$product = $object->product_factory()->get_product();
// Bail if $product is not an instance of WC_Product_Accommodation_Booking
if (! $product instanceof WC_Product_Accommodation_Booking) {
return $type;
}
// Set the correct type
return $wp_union->type_registry->get_type('AccommodationBookingProduct');
}, 10, 3); Even if this is only a temporary/workaround fix. It's ideal and allows the build to move on. Thank you @kidunot89, @jacobarriola for your time and help resolving this. |
@boxxroom glad you got it working!
|
@jacobarriola. This is a small side note: Just checked this file in our codebase and it definitely doesn't have this We are using plugin version: 0.6.1 |
@boxxroom Looks like it was added in Since you're on a recent version of core wp-graphql, I'd consider an update. There are some breaking changes from your version to |
@jacobarriola updated to the latest versions, updated the code to reflect that of yours and all seems to be working perfect. Thanks for your help on this. |
Running into this issue as well. I try to establish a relationship to WooCommerce If so, how could we solve it? |
Describe the bug
Attaching a product relationship within a Flexible content ACF field on to a
Page post type
renders anAbstract type
error due to the resolve. The resolve is expected to resolve to anObject type
.To Reproduce
Steps to reproduce the behavior:
ACF JSON Export File OR
Create a new ACF Field Group (named WPGraphQL Example) with a Location > Rules equal to Page
Add a Flexible Content (named Flexible Content)
Field Type: Relationship (Layout name: Relationship Field, Field Label: Product Relationship)
Filter by Post Type: Product
Filter by Taxonomy: Simple (optional)
Filters: All Checked
Elements: Featured Image Checked
Return Object: Post Object
Save.
Navigate to Pages
Choose a page: Home (take note of page ID)
Add ACF Flexible Content
Relationship Field
Select 1+ products
Save
Navigate to GraphiQL IDE > Add GraphQL statement (GraphQL Statement) OR
Expected behavior
A list of product objects
Screenshots
Desktop (please complete the following information):
Plugin Versions
Additional context
I have also added to this thread with a information, findings and debug #253 (comment)
The text was updated successfully, but these errors were encountered: