houke / acf-icon-picker

Creates an icon picker ACF field
100 stars 47 forks source link

Add GraphQL support #31

Open chrysos opened 1 year ago

chrysos commented 1 year ago

Hello, your plugin is great, congratulations.

It would be really nice if you added support for WP-Graphql.

https://www.wpgraphql.com/

samfrank commented 7 months ago

Hey!

I have managed to add the Icon Picker to WPGraphQL. Hopefully this will help someone in the future.

I am using the ACF for WPGraphQl v2, the documentation for this will be here: https://acf.wpgraphql.com/

More specifically I used this example code found on these pages

https://acf.wpgraphql.com/adding-support-for-3rd-party-acf-field-type/ https://github.com/wp-graphql/wpgraphql-acf/tree/develop/src/ThirdParty/AcfExtended/FieldType

        // Add field-resolver
        add_action( 'wpgraphql/acf/registry_init', function() {
            register_graphql_acf_field_type('icon-picker');
        });

        register_graphql_acf_field_type(
            'icon-picker',
            [
                'graphql_type' => static function () {
                    return 'String';
                },
                'resolve' => static function ( $root, $args, $context, $info, $field_type, $field_config ) {
                    $value = $field_config->resolve_field( $root, $args, $context, $info );

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

                    return $value;
                },
            ]
        );