Skip to content

Commit 566c206

Browse files
committed
Adding Doctrine annotations VS PHP8 attributes documentation
1 parent f1c3f21 commit 566c206

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
id: doctrine-annotations-attributes
3+
title: Doctrine annotations VS PHP8 attributes
4+
sidebar_label: Annotations VS Attributes
5+
---
6+
7+
GraphQLite is heavily relying on the concept of annotations (also called attributes in PHP 8+).
8+
9+
## Doctrine annotations
10+
11+
<div class="alert alert-warning"><strong>Deprecated!</strong> Doctrine annotations are deprecated in favor of native PHP 8 attributes. Support will be dropped in GraphQLite 5.0</div>
12+
13+
Historically, attributes were not available in PHP and PHP developers had to "trick" PHP to get annotation support.
14+
This was the purpose of the [doctrine/annotation](https://www.doctrine-project.org/projects/doctrine-annotations/en/latest/index.html) library.
15+
16+
Using Doctrine annotations, you write annotations in your docblocks:
17+
18+
```php
19+
use TheCodingMachine\GraphQLite\Annotations\Type;
20+
21+
/**
22+
* @Type
23+
*/
24+
class MyType
25+
{
26+
}
27+
```
28+
29+
Please note that:
30+
31+
- The annotation is added in a **docblock** (a comment starting with "`/**`")
32+
- The `Type` part is actually a class. It must be declared in the `use` statements at the top of your file.
33+
34+
35+
<div class="alert alert-info"><strong>Heads up!</strong>
36+
Some IDEs provide support for Doctrine annotations:
37+
38+
<ul>
39+
<li>PhpStorm via the <a href="PHP Annotations Plugin">https://plugins.jetbrains.com/plugin/7320-php-annotations</a></li>
40+
<li>Eclipse via the <a href="Symfony2 Plugin">https://marketplace.eclipse.org/content/symfony-plugin</a></li>
41+
<li>Netbeans has native support</li>
42+
</ul>
43+
44+
We strongly recommend using an IDE that has Doctrine annotations support.
45+
</div>
46+
47+
## PHP 8 attributes
48+
49+
Starting with PHP 8, PHP got native annotations support. They are actually called "attributes" in the PHP world.
50+
51+
The same code can be written this way:
52+
53+
```php
54+
use TheCodingMachine\GraphQLite\Annotations\Type;
55+
56+
#[Type]
57+
class MyType
58+
{
59+
}
60+
```
61+
62+
GraphQLite v4.1+ has support for PHP 8 attributes.
63+
64+
The Doctrine annotation class and the PHP 8 attribute class is **the same** (so you will be using the same `use` statement at the top of your file).
65+
66+
They support the same attributes too.
67+
68+
A few notable differences:
69+
70+
- PHP 8 attributes do not support nested attributes (unlike Doctrine annotations). This means there is no equivalent to the `annotations` attribute of `@MagicField` and `@SourceField`.
71+
- PHP 8 attributes can be written at the parameter level. Any attribute targeting a "parameter" must be written at the parameter level.
72+
73+
Let's take an example with the [`#Autowire` attribute](autowiring.md):
74+
75+
**PHP 7+**
76+
```
77+
/**
78+
* @Field
79+
* @Autowire(for="$productRepository")
80+
*/
81+
public function getProduct(ProductRepository $productRepository) : Product {
82+
//...
83+
}
84+
```
85+
86+
**PHP 8**
87+
```
88+
#[Field]
89+
public function getProduct(#[Autowire] ProductRepository $productRepository) : Product {
90+
//...
91+
}
92+
```
93+

docs/queries.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ As you can see, GraphQLite will automatically do the mapping between PHP types a
4242

4343
<div class="alert alert-warning"><strong>Heads up!</strong> If you are not using a framework with an autowiring container (like Symfony or Laravel), please be aware that the <code>MyController</code> class must exist in the container of your application. Furthermore, the identifier of the controller in the container MUST be the fully qualified class name of controller.</div>
4444

45+
## About annotations / attributes
46+
47+
GraphQLite relies a lot on annotations (we call them attributes since PHP 8).
48+
49+
It supports both the old "Doctrine annotations" style (`@Query`) and the new PHP 8 attributes (`#[Query]`).
50+
51+
Read the [Doctrine annotations VS attributes](doctrine_annotations_attributes.md) documentation if you are not familiar with this concept.
52+
4553
## Testing the query
4654

4755
The default GraphQL endpoint is `/graphql`.

website/sidebars.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"Security": ["authentication_authorization", "fine-grained-security", "implementing-security"],
77
"Performance": ["query-plan", "prefetch-method"],
88
"Advanced": ["file-uploads", "pagination", "custom-types", "field-middlewares", "argument-resolving", "extend_input_type", "multiple_output_types", "symfony-bundle-advanced", "laravel-package-advanced", "internals", "troubleshooting", "migrating"],
9-
"Reference": ["annotations_reference", "semver","changelog"]
9+
"Reference": ["doctrine-annotations-attributes", "annotations_reference", "semver","changelog"]
1010
}
1111
}

website/versioned_docs/version-4.0/annotations_reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The `@Mutation` annotation is used to declare a GraphQL mutation.
2525
Attribute | Compulsory | Type | Definition
2626
---------------|------------|------|--------
2727
name | *no* | string | The name of the mutation. If skipped, the name of the method is used instead.
28-
[outputType](custom_output_types.md) | *no* | string | Forces the GraphQL output type of a query.
28+
[outputType](custom_types.md) | *no* | string | Forces the GraphQL output type of a query.
2929

3030
## @Type annotation
3131

0 commit comments

Comments
 (0)