• toficofi

    (@toficofi)


    Hi, are we getting first-party WPGraphQL support for Table fields?

    Until then, this is what I’ve done:

    add_action( 'wpgraphql/acf/registry_init', function() {
    register_graphql_acf_field_type( 'table', [
    'graphql_type' => 'Table',
    'resolve' => function ($root, $args, $context, $info, $field_type, $field_config ) {
    $value = $field_config->resolve_field($root, $args, $context, $info);

    if (is_null($value)) {
    return null;
    }

    error_log(print_r($value, true));

    return [
    'header' => 1 == $value['use_header'] ? array_map(function ($th) {
    return $th['c'];
    }, $value['header']) : [],
    'body' => array_map(function ($row) {
    return array_map(function ($cell) {
    return $cell['c'];
    }, $row);
    }, $value['body'])
    ];
    },
    ]);
    });


    // Add Table-Type
    add_action('graphql_register_types', function () {
    register_graphql_object_type('Table', [
    'fields' => [
    'header' => ['type' => ['list_of' => 'String']],
    'body' => ['type' => ['list_of' => ['list_of' => 'String']]],
    ],
    ]);
    });
  • You must be logged in to reply to this topic.