wp-graphql / wp-graphql-acf

WPGraphQL for Advanced Custom Fields
https://wpgraphql.com/acf
626 stars 123 forks source link

Post Object field and Relationship field return null for arrays. #155

Closed HumidBrains closed 11 months ago

HumidBrains commented 4 years ago

I’m trying to relate products from within a Flexible Content field in ACF. That works when querying a single product with ACF Post Object by setting “Select multiple values?” to No. However, when querying for multiple values, instead of an array of product objects, I get null in WPGraphiQL. Same happens if using ACF Relationship Field, which I think always returns an array. The result is the same for simple and variable products.

This should be easy to replicate as it happens to me however I define my field groups

Using WPGraphQL 0.10.2 wp-graphql-woocommerce 0.5.1 wp-graphql-acf 0.3.4

HumidBrains commented 4 years ago

@jasonbahl Will there be any love given to this one anytime soon? :)

gustavarrhenius commented 4 years ago

@HumidBrains @jasonbahl I discovered the same problem in our project, and it seems like its the graphql_acf_post_object_source filter that wp-graphql-woocommerce uses and returns wrong value.

File includes/class-acf-schema-filters.php line 56.

public static function resolve_post_object_source( $source, $value ) {
    $post = get_post( $value );
    if ( $post instanceof \WP_Post ) {
        switch ( $post->post_type ) {
            case 'shop_coupon':
                $source = new Coupon( $post->ID );
                break;
            case 'shop_order':
                $source = new Order( $post->ID );
                break;
            case 'product':
                $source = new Product( $post->ID );
                break;
        }
    }

    return $source;
}

Since the Post Object field can return both null and an array this code wont work because when querying a single product get_post returns itself, and the code has no support for an array.

vstrelianyi commented 3 years ago

having the same issue

versions: WP 5.8.1 Woo 5.8.0 Graphql 1.6.5 WPGraphQL WooCommerce 0.10.4 Advanced Custom Fields PRO 5.10.2 WPGraphQL for Advanced Custom Fields 0.5.3

Steps:

I'm having a CPT - Auctions Auction has an ACF Relationship field with Products when I'm running the following query - I'm receiving an error

QUERY image

ERROR image

jasonbahl commented 11 months ago

This is a pretty old issue, but it typically occurred when there were orphaned IDs stored as values for ACF Fields.

WPGraphQL for ACF has been re-built over here: https://github.com/wp-graphql/wpgraphql-acf and relational fields are all now using connections and WPGraphQL DataLoaders which account for issues like orphaned IDs not throwing errors.

I recommend switching to the new version of WPGraphQL for ACF at your earliest convenience as it's the version we'll be releasing on WordPress.org and supporting long-term.