> For the complete documentation index, see [llms.txt](https://siler.leocavalcante.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://siler.leocavalcante.dev/graphql/annotations.md).

# @Annotations

## 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:

```
$ composer require doctrine/annotations
```

## 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:

```php
<?php declare(strict_types=1);

namespace App;

use GraphQL\Type\Definition\ResolveInfo;
use Siler\GraphQL\Annotation\Field;
use Siler\GraphQL\Annotation\ObjectType;

/** @ObjectType */
class Query
{
    /** @Field(description="A common greet") */
    public static function hello(): string
    {
        return 'Hello, World!';
    }
}
```

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:

```php
<?php declare(strict_types=1);

namespace App;

use function Siler\GraphQL\{annotated, init};

require_once __DIR__ . '/vendor/autoload.php';

$schema = annotated([Query::class]);
init($schema);
```

**And that is it!** It auto-magically servers the following SDL:

```graphql
type Query {
  """
  A common greet
  """
  hello: String!
}
```

With the static `hello` method body already playing the **resolver** role, so:

```graphql
query {
  hello
}
```

Returns:

```javascript
{
  "data": {
    "hello": "Hello, World!"
  }
}
```

**For a full-featured example, please take a look at:** [**github.com/leocavalcante/siler/examples/graphql-annotations**](https://github.com/leocavalcante/siler/tree/master/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`:

```javascript
composer require doctrine/cache
```

Then pass a `Doctrine\Common\Cache\Cache` to `Siler\GraphQL\Deannotator::cache()` like:

```javascript
Siler\GraphQL\Deannotator::cache(new ApcuCache());
```

{% hint style="info" %}
Make sure you do this before the `annotated()` call.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://siler.leocavalcante.dev/graphql/annotations.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
