wp-graphql / wp-graphql-acf

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

Unable to use type generated by ACF Flexible_Content in custom field #360

Closed kamilbrenk closed 1 year ago

kamilbrenk commented 1 year ago

Hi. I am trying to use type generated by the ACF field (clickable in admin) in my GraphQL field. This is what the field registration looks like:

add_action( 'graphql_register_types', function() {
    register_graphql_field( 'RootQuery', 'mapRouteLayerBy', array(
        'type' => 'Map_Details_Layers_Route',
        'args' => array(
            'layer_id' => array(
                'type' => 'String',
                'description' => 'Map layer id',
            )
        ),
        'resolve' => function ($root, $args, $context, $info) {
            if (empty($args['layer_id'])) {
                return null;
            }

            $map_layers = get_field('layers', 4003);
            if (empty($map_layers)) {
                return null;
            }

            return $map_layers[0]; // this layer returned data of type Map_Details_Layers_Route
        },
    ));
});

As expected, a new field appears in the GraphiQL IDE:

image

Unfortunately, as you can see, the data beyond fieldGroupName is missing.

FYI: the data it passes in the resolve function:

image

Definition of type Map_Details_Layers_Route:

image

Why is it that despite providing the correct data in the resolve function, it does not match the Map_Details_Layers_Route type? How to fix this?

kamilbrenk commented 1 year ago

FYI: The problem was passing content from the ACF field with formatting. The solution is the following fix:

$map_layers = get_field('layers', 4003, false);