Closed denisyilmaz closed 3 months 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!
I like the idea of having one single @wordsmith
directive. 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.
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).
Any news on this topic? That would really be a great feature
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.)
Wordsmith 4.5.0
and 5.0.0
are out with official support for this new @wordsmith
GQL directive. ✅
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: