TopShelfCraft / Wordsmith

A plugin for Craft CMS to help you manage and manipulate text.
Other
30 stars 20 forks source link

[FR] Add GraphQL support for filters as directives #46

Closed denisyilmaz closed 3 months ago

denisyilmaz commented 2 years ago

Would it be possible to add the filters (like {{'Mark & Sons: "industry leaders" in... stuff.' | typogrify}}) to GraphQL as directives? It would be awesome to be able to parse text in graphql like this:

{
    entry {
        id
        title @titleize
        slug
        content @typogrify
    }
}
michaelrog commented 2 years ago

That's a GREAT idea! — I imagine it'd be useful for a lot of folks!

You can define custom directives using Craft's Gql::EVENT_REGISTER_GQL_DIRECTIVES event.

The trick would be... How can we avoid needing to create and register dozens of PHP classes (one for each of Wordsmith's functions)?

Maybe a catch-all @wordsmith directive could work? Then you'd have a syntax like:

{
    entry {
        id
        title @wordsmith(function: "titleize")
        slug
        content @wordsmith(function: "typogrify", args: [])
    }
}

Alternatively, for sugar, I can envision a config setting like

return [
    'registerGqlDirectives' => [
        'titleize',
        'typogriphy',
    ],
]

...that could dynamically instantiate and register directives, allowing a shorter syntax like the one in your example.

If you pursue this, please PR to the dev branch! This would make an awesome contribution!

denisyilmaz commented 2 years ago

I like the idea of having one single @wordsmithdirective. I have created a PR with all functions which return string/boolean and don't need any arguments to work correctly or have defaults set.

image

It might be necessary to declare different directives for the functions with arguments because this would make it easier to keep the syntax for the arguments in sync with the original functions and provide better GraphQL Schema insights. Additionally, the return type might not be editable to something else then string (currently the functions which return an int or boolean are returning these as string, which works, but is not as intended).

mihob commented 2 years ago

Any news on this topic? That would really be a great feature

michaelrog commented 1 year ago

I've pushed an implementation of this functionality to 4.x.dev with the intention of releasing it in 4.3.0.

For now, I went with a single @wordsmith directive, which supports any function that doesn't require arguments.

(The way Craft has implemented custom GQL Directives requires the apply() logic to be in a static context; Unfortunately, that prohibits my idea for generating/registering directives dynamically, at least for now.)

michaelrog commented 3 months ago

Wordsmith 4.5.0 and 5.0.0 are out with official support for this new @wordsmith GQL directive. ✅