@Annotations
On the previous guide you saw how to map resolvers (callables) from a existing SDL (.graphql or .gql). Annotations enables the other way around, it provides a GraphQL SDL from annotated PHP code.
Thank you Doctrine
Siler's GraphQL Annotations uses the super-powers from Doctrine's Annotations and like any other dependency, is a peer that we should explicitly require:
What is available:
There are 9 annotations fulfilling the GraphQL's ecosystem:
Class annotations are:
ObjectType
InterfaceType
InputType
EnumType
UnionType
Directive
Complementary method and property annotations are:
Field
Args
EnumVal
They follow a ubiquitous language to GraphQL spec, so if you know GraphQL, there is nothing new here, you probably already know what each of them does just by its name.
Hello, World!
Let's start by defining our root query:
The ObjectType
name will be inferred by the class name, so it will already be Query.
Then we just provide this class to the annotated
function on the Siler\GraphQL
namespace:
And that is it! It auto-magically servers the following SDL:
With the static hello
method body already playing the resolver role, so:
Returns:
For a full-featured example, please take a look at: github.com/leocavalcante/siler/examples/graphql-annotations****
Caching
Parsing docblocks can be expensive, on production environments is recommended to cache this process by using a caching reader.
First, install doctrine/cache
:
Then pass a Doctrine\Common\Cache\Cache
to Siler\GraphQL\Deannotator::cache()
like:
Make sure you do this before the annotated()
call.
Last updated