|
| 1 | +--- |
| 2 | +id: annotations_reference |
| 3 | +title: Annotations reference |
| 4 | +sidebar_label: Annotations reference |
| 5 | +--- |
| 6 | + |
| 7 | +Note: all annotations are available both in a Doctrine annotation format (`@Query`) and in PHP 8 attribute format (`#[Query]`). |
| 8 | +See [Doctrine annotations vs PHP 8 attributes](doctrine_annotations_attributes.md) for more details. |
| 9 | + |
| 10 | +## @Query annotation |
| 11 | + |
| 12 | +The `@Query` annotation is used to declare a GraphQL query. |
| 13 | + |
| 14 | +**Applies on**: controller methods. |
| 15 | + |
| 16 | +Attribute | Compulsory | Type | Definition |
| 17 | +---------------|------------|------|-------- |
| 18 | +name | *no* | string | The name of the query. If skipped, the name of the method is used instead. |
| 19 | +[outputType](custom_types.md) | *no* | string | Forces the GraphQL output type of a query. |
| 20 | + |
| 21 | +## @Mutation annotation |
| 22 | + |
| 23 | +The `@Mutation` annotation is used to declare a GraphQL mutation. |
| 24 | + |
| 25 | +**Applies on**: controller methods. |
| 26 | + |
| 27 | +Attribute | Compulsory | Type | Definition |
| 28 | +---------------|------------|------|-------- |
| 29 | +name | *no* | string | The name of the mutation. If skipped, the name of the method is used instead. |
| 30 | +[outputType](custom_types.md) | *no* | string | Forces the GraphQL output type of a query. |
| 31 | + |
| 32 | +## @Type annotation |
| 33 | + |
| 34 | +The `@Type` annotation is used to declare a GraphQL object type. |
| 35 | + |
| 36 | +**Applies on**: classes. |
| 37 | + |
| 38 | +Attribute | Compulsory | Type | Definition |
| 39 | +---------------|------------|------|-------- |
| 40 | +class | *no* | string | The targeted class. If no class is passed, the type applies to the current class. The current class is assumed to be an entity. If the "class" attribute is passed, [the class annotated with `@Type` is a service](external_type_declaration.md). |
| 41 | +name | *no* | string | The name of the GraphQL type generated. If not passed, the name of the class is used. If the class ends with "Type", the "Type" suffix is removed |
| 42 | +default | *no* | bool | Defaults to *true*. Whether the targeted PHP class should be mapped by default to this type. |
| 43 | +external | *no* | bool | Whether this is an [external type declaration](external_type_declaration.md) or not. You usually do not need to use this attribute since this value defaults to true if a "class" attribute is set. This is only useful if you are declaring a type with no PHP class mapping using the "name" attribute. |
| 44 | + |
| 45 | +## @ExtendType annotation |
| 46 | + |
| 47 | +The `@ExtendType` annotation is used to add fields to an existing GraphQL object type. |
| 48 | + |
| 49 | +**Applies on**: classes. |
| 50 | + |
| 51 | +Attribute | Compulsory | Type | Definition |
| 52 | +---------------|------------|------|-------- |
| 53 | +class | see below | string | The targeted class. [The class annotated with `@ExtendType` is a service](extend_type.md). |
| 54 | +name | see below | string | The targeted GraphQL output type. |
| 55 | + |
| 56 | +One and only one of "class" and "name" parameter can be passed at the same time. |
| 57 | + |
| 58 | +## @Input annotation |
| 59 | + |
| 60 | +The `@Input` annotation is used to declare a GraphQL input type. |
| 61 | + |
| 62 | +**Applies on**: classes. |
| 63 | + |
| 64 | +Attribute | Compulsory | Type | Definition |
| 65 | +---------------|------------|--------|-------- |
| 66 | +name | *no* | string | The name of the GraphQL input type generated. If not passed, the name of the class with suffix "Input" is used. If the class ends with "Input", the "Input" suffix is not added. |
| 67 | +description | *no* | string | Description of the input type in the documentation. If not passed, PHP doc comment is used. |
| 68 | +default | *no* | bool | Defaults to *true* if name is not specified. Whether the annotated PHP class should be mapped by default to this type. |
| 69 | +update | *no* | bool | Determines if the the input represents a partial update. When set to *true* all input fields will become optional and won't have default values thus won't be set on resolve if they are not specified in the query/mutation. |
| 70 | + |
| 71 | + |
| 72 | +## @Field annotation |
| 73 | + |
| 74 | +The `@Field` annotation is used to declare a GraphQL field. |
| 75 | + |
| 76 | +**Applies on**: methods or properties of classes annotated with `@Type`, `@ExtendType` or `@Input`. |
| 77 | +When it's applied on private or protected property, public getter or/and setter method is expected in the class accordingly |
| 78 | +whether it's used for output type or input type. For example if property name is `foo` then getter should be `getFoo()` or setter should be `setFoo($foo)`. Setter can be omitted if property related to the field is present in the constructor with the same name. |
| 79 | + |
| 80 | +Attribute | Compulsory | Type | Definition |
| 81 | +------------------------------|------------|---------------|-------- |
| 82 | +name | *no* | string | The name of the field. If skipped, the name of the method is used instead. |
| 83 | +for | *no* | string, array | Forces the field to be used only for specific output or input type(s). By default field is used for all possible declared types. |
| 84 | +description | *no* | string | Field description displayed in the GraphQL docs. If it's empty PHP doc comment is used instead. |
| 85 | +[outputType](custom_types.md) | *no* | string | Forces the GraphQL output type of a query. |
| 86 | +[inputType](input_types.md) | *no* | string | Forces the GraphQL input type of a query. |
| 87 | + |
| 88 | +## @SourceField annotation |
| 89 | + |
| 90 | +The `@SourceField` annotation is used to declare a GraphQL field. |
| 91 | + |
| 92 | +**Applies on**: classes annotated with `@Type` or `@ExtendType`. |
| 93 | + |
| 94 | +Attribute | Compulsory | Type | Definition |
| 95 | +---------------|------------|------|-------- |
| 96 | +name | *yes* | string | The name of the field. |
| 97 | +[outputType](custom_types.md) | *no* | string | Forces the GraphQL output type of the field. Otherwise, return type is used. |
| 98 | +phpType | *no* | string | The PHP type of the field (as you would write it in a Docblock) |
| 99 | +annotations | *no* | array<Annotations> | A set of annotations that apply to this field. You would typically used a "@Logged" or "@Right" annotation here. Available in Doctrine annotations only (not available in the #SourceField PHP 8 attribute) |
| 100 | + |
| 101 | +**Note**: `outputType` and `phpType` are mutually exclusive. |
| 102 | + |
| 103 | +## @MagicField annotation |
| 104 | + |
| 105 | +The `@MagicField` annotation is used to declare a GraphQL field that originates from a PHP magic property (using `__get` magic method). |
| 106 | + |
| 107 | +**Applies on**: classes annotated with `@Type` or `@ExtendType`. |
| 108 | + |
| 109 | +Attribute | Compulsory | Type | Definition |
| 110 | +---------------|------------|------|-------- |
| 111 | +name | *yes* | string | The name of the field. |
| 112 | +[outputType](custom_types.md) | *no*(*) | string | The GraphQL output type of the field. |
| 113 | +phpType | *no*(*) | string | The PHP type of the field (as you would write it in a Docblock) |
| 114 | +annotations | *no* | array<Annotations> | A set of annotations that apply to this field. You would typically used a "@Logged" or "@Right" annotation here. Available in Doctrine annotations only (not available in the #MagicField PHP 8 attribute) |
| 115 | + |
| 116 | +(*) **Note**: `outputType` and `phpType` are mutually exclusive. You MUST provide one of them. |
| 117 | + |
| 118 | +## @Logged annotation |
| 119 | + |
| 120 | +The `@Logged` annotation is used to declare a Query/Mutation/Field is only visible to logged users. |
| 121 | + |
| 122 | +**Applies on**: methods or properties annotated with `@Query`, `@Mutation` or `@Field`. |
| 123 | + |
| 124 | +This annotation allows no attributes. |
| 125 | + |
| 126 | +## @Right annotation |
| 127 | + |
| 128 | +The `@Right` annotation is used to declare a Query/Mutation/Field is only visible to users with a specific right. |
| 129 | + |
| 130 | +**Applies on**: methods or properties annotated with `@Query`, `@Mutation` or `@Field`. |
| 131 | + |
| 132 | +Attribute | Compulsory | Type | Definition |
| 133 | +---------------|------------|------|-------- |
| 134 | +name | *yes* | string | The name of the right. |
| 135 | + |
| 136 | +## @FailWith annotation |
| 137 | + |
| 138 | +The `@FailWith` annotation is used to declare a default value to return in the user is not authorized to see a specific |
| 139 | +query / mutation / field (according to the `@Logged` and `@Right` annotations). |
| 140 | + |
| 141 | +**Applies on**: methods or properties annotated with `@Query`, `@Mutation` or `@Field` and one of `@Logged` or `@Right` annotations. |
| 142 | + |
| 143 | +Attribute | Compulsory | Type | Definition |
| 144 | +---------------|------------|------|-------- |
| 145 | +value | *yes* | mixed | The value to return if the user is not authorized. |
| 146 | + |
| 147 | +## @HideIfUnauthorized annotation |
| 148 | + |
| 149 | +The `@HideIfUnauthorized` annotation is used to completely hide the query / mutation / field if the user is not authorized |
| 150 | +to access it (according to the `@Logged` and `@Right` annotations). |
| 151 | + |
| 152 | +**Applies on**: methods or properties annotated with `@Query`, `@Mutation` or `@Field` and one of `@Logged` or `@Right` annotations. |
| 153 | + |
| 154 | +`@HideIfUnauthorized` and `@FailWith` are mutually exclusive. |
| 155 | + |
| 156 | +## @InjectUser annotation |
| 157 | + |
| 158 | +Use the `@InjectUser` annotation to inject an instance of the current user logged in into a parameter of your |
| 159 | +query / mutation / field. |
| 160 | + |
| 161 | +**Applies on**: methods annotated with `@Query`, `@Mutation` or `@Field`. |
| 162 | + |
| 163 | +Attribute | Compulsory | Type | Definition |
| 164 | +---------------|------------|--------|-------- |
| 165 | +*for* | *yes* | string | The name of the PHP parameter |
| 166 | + |
| 167 | +## @Security annotation |
| 168 | + |
| 169 | +The `@Security` annotation can be used to check fin-grained access rights. |
| 170 | +It is very flexible: it allows you to pass an expression that can contains custom logic. |
| 171 | + |
| 172 | +See [the fine grained security page](fine-grained-security.md) for more details. |
| 173 | + |
| 174 | +**Applies on**: methods or properties annotated with `@Query`, `@Mutation` or `@Field`. |
| 175 | + |
| 176 | +Attribute | Compulsory | Type | Definition |
| 177 | +---------------|------------|--------|-------- |
| 178 | +*default* | *yes* | string | The security expression |
| 179 | + |
| 180 | +## @Factory annotation |
| 181 | + |
| 182 | +The `@Factory` annotation is used to declare a factory that turns GraphQL input types into objects. |
| 183 | + |
| 184 | +**Applies on**: methods from classes in the "types" namespace. |
| 185 | + |
| 186 | +Attribute | Compulsory | Type | Definition |
| 187 | +---------------|------------|------|-------- |
| 188 | +name | *no* | string | The name of the input type. If skipped, the name of class returned by the factory is used instead. |
| 189 | +default | *no* | bool | If `true`, this factory will be used by default for its PHP return type. If set to `false`, you must explicitly [reference this factory using the `@Parameter` annotation](http://localhost:3000/docs/input-types#declaring-several-input-types-for-the-same-php-class). |
| 190 | + |
| 191 | +## @UseInputType annotation |
| 192 | + |
| 193 | +Used to override the GraphQL input type of a PHP parameter. |
| 194 | + |
| 195 | +**Applies on**: methods annotated with `@Query`, `@Mutation` or `@Field` annotation. |
| 196 | + |
| 197 | +Attribute | Compulsory | Type | Definition |
| 198 | +---------------|------------|------|-------- |
| 199 | +*for* | *yes* | string | The name of the PHP parameter |
| 200 | +*inputType* | *yes* | string | The GraphQL input type to force for this input field |
| 201 | + |
| 202 | +## @Decorate annotation |
| 203 | + |
| 204 | +The `@Decorate` annotation is used [to extend/modify/decorate an input type declared with the `@Factory` annotation](extend_input_type.md). |
| 205 | + |
| 206 | +**Applies on**: methods from classes in the "types" namespace. |
| 207 | + |
| 208 | +Attribute | Compulsory | Type | Definition |
| 209 | +---------------|------------|------|-------- |
| 210 | +name | *yes* | string | The GraphQL input type name extended by this decorator. |
| 211 | + |
| 212 | +## @Autowire annotation |
| 213 | + |
| 214 | +[Resolves a PHP parameter from the container](autowiring.md). |
| 215 | + |
| 216 | +Useful to inject services directly into `@Field` method arguments. |
| 217 | + |
| 218 | +**Applies on**: methods annotated with `@Query`, `@Mutation` or `@Field` annotation. |
| 219 | + |
| 220 | +Attribute | Compulsory | Type | Definition |
| 221 | +---------------|------------|------|-------- |
| 222 | +*for* | *yes* | string | The name of the PHP parameter |
| 223 | +*identifier* | *no* | string | The identifier of the service to fetch. This is optional. Please avoid using this attribute as this leads to a "service locator" anti-pattern. |
| 224 | + |
| 225 | +## @HideParameter annotation |
| 226 | + |
| 227 | +Removes [an argument from the GraphQL schema](input_types.md#ignoring_some_parameters). |
| 228 | + |
| 229 | +Attribute | Compulsory | Type | Definition |
| 230 | +---------------|------------|------|-------- |
| 231 | +*for* | *yes* | string | The name of the PHP parameter to hide |
| 232 | + |
| 233 | + |
| 234 | +## @Validate annotation |
| 235 | + |
| 236 | +<div class="alert alert-info">This annotation is only available in the GraphQLite Laravel package</div> |
| 237 | + |
| 238 | +[Validates a user input in Laravel](laravel-package-advanced.md). |
| 239 | + |
| 240 | +**Applies on**: methods annotated with `@Query`, `@Mutation`, `@Field`, `@Factory` or `@Decorator` annotation. |
| 241 | + |
| 242 | +Attribute | Compulsory | Type | Definition |
| 243 | +---------------|------------|------|-------- |
| 244 | +*for* | *yes* | string | The name of the PHP parameter |
| 245 | +*rule* | *yes | string | Laravel validation rules |
| 246 | + |
| 247 | +Sample: |
| 248 | + |
| 249 | +``` |
| 250 | +@Validate(for="$email", rule="email|unique:users") |
| 251 | +``` |
| 252 | + |
| 253 | +## @Assertion annotation |
| 254 | + |
| 255 | +[Validates a user input](validation.md). |
| 256 | + |
| 257 | +The `@Assertion` annotation is available in the *thecodingmachine/graphqlite-symfony-validator-bridge* third party package. |
| 258 | +It is available out of the box if you use the Symfony bundle. |
| 259 | + |
| 260 | +**Applies on**: methods annotated with `@Query`, `@Mutation`, `@Field`, `@Factory` or `@Decorator` annotation. |
| 261 | + |
| 262 | +Attribute | Compulsory | Type | Definition |
| 263 | +---------------|------------|------|-------- |
| 264 | +*for* | *yes* | string | The name of the PHP parameter |
| 265 | +*constraint* | *yes | annotation | One (or many) Symfony validation annotations. |
| 266 | + |
| 267 | +## @EnumType annotation |
| 268 | + |
| 269 | +The `@EnumType` annotation is used to change the name of a "Enum" type. |
| 270 | +Note that if you do not want to change the name, the annotation is optionnal. Any object extending `MyCLabs\Enum\Enum` |
| 271 | +is automatically mapped to a GraphQL enum type. |
| 272 | + |
| 273 | +**Applies on**: classes extending the `MyCLabs\Enum\Enum` base class. |
| 274 | + |
| 275 | +Attribute | Compulsory | Type | Definition |
| 276 | +---------------|------------|------|-------- |
| 277 | +name | *no* | string | The name of the enum type (in the GraphQL schema) |
0 commit comments