Skip to main content
Version: 8.2.0

Attributes reference

All attributes use PHP 8 native attribute syntax (e.g., #[Query]). The legacy Doctrine annotations format (@Query) is no longer supported. See migrating from Doctrine annotations to PHP 8 attributes for details.

Attributes are listed in alphabetical order.

#[Assertion]

Validates a user input.

The #[Assertion] attribute is available in the thecodingmachine/graphqlite-symfony-validator-bridge third party package. It is available out of the box if you use the Symfony bundle.

Applies on: methods annotated with #[Query], #[Mutation], #[Field], #[Factory] or #[Decorator] attribute.

AttributeCompulsoryTypeDefinition
foryesstringThe name of the PHP parameter
constraint*yesattributeOne (or many) Symfony validation attributes.

#[Autowire]

Resolves a PHP parameter from the container.

Useful to inject services directly into #[Field] method arguments.

Applies on: methods annotated with #[Query], #[Mutation] or #[Field] attribute.

AttributeCompulsoryTypeDefinition
foryesstringThe name of the PHP parameter
identifiernostringThe identifier of the service to fetch. This is optional. Please avoid using this attribute as this leads to a "service locator" anti-pattern.

#[Cost]

Sets complexity and multipliers on fields for automatic query complexity.

AttributeCompulsoryTypeDefinition
complexitynointComplexity for that field
multipliersnoarray\<string>Names of fields by value of which complexity will be multiplied
defaultMultipliernointDefault multiplier value if all multipliers are missing/null

#[Decorate]

The #[Decorate] attribute is used to extend/modify/decorate an input type declared with the #[Factory] attribute.

Applies on: methods from classes in the "types" namespace.

AttributeCompulsoryTypeDefinition
nameyesstringThe GraphQL input type name extended by this decorator.

#[EnumValue]

The #[EnumValue] attribute attaches GraphQL schema metadata (description, deprecation reason) to an individual case of a PHP 8.1+ native enum that is exposed as a GraphQL enum type.

Applies on: cases of an enum annotated (directly or indirectly) with #[Type].

AttributeCompulsoryTypeDefinition
descriptionnostringDescription of the enum value. When omitted, the case's PHP docblock summary is used (see schema descriptions). An explicit empty string '' deliberately suppresses the docblock fallback.
deprecationReasonnostringDeprecation reason published to the schema. When omitted, the @deprecated tag on the case docblock is used. An explicit empty string '' deliberately clears any inherited @deprecated tag.
#[Type]
enum Genre: string
{
#[EnumValue(description: 'Fiction works including novels and short stories.')]
case Fiction = 'fiction';

#[EnumValue(deprecationReason: 'Use Fiction::Verse instead.')]
case Poetry = 'poetry';
}

#[ExtendType]

The #[ExtendType] attribute is used to add fields to an existing GraphQL object type.

Applies on: classes.

AttributeCompulsoryTypeDefinition
classsee belowstringThe targeted class. The class annotated with #[ExtendType] is a service.
namesee belowstringThe targeted GraphQL output type.
descriptionnostringDescription of the extended GraphQL type. Use this only when the base #[Type] does not already declare a description — see description uniqueness on #[ExtendType]. Cannot be combined with a description on the base #[Type] or on another #[ExtendType] targeting the same class.

One and only one of "class" and "name" parameter can be passed at the same time.

#[Factory]

The #[Factory] attribute is used to declare a factory that turns GraphQL input types into objects.

Applies on: methods from classes in the "types" namespace.

AttributeCompulsoryTypeDefinition
namenostringThe name of the input type. If skipped, the name of class returned by the factory is used instead.
defaultnoboolIf 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] attribute.
descriptionnostringDescription of the GraphQL input type produced by this factory. When omitted, the factory method's docblock summary is used (see schema descriptions). An explicit empty string '' deliberately suppresses the docblock fallback.

#[FailWith]

The #[FailWith] attribute is used to declare a default value to return in the user is not authorized to see a specific query/mutation/subscription/field (according to the #[Logged] and #[Right] attributes).

Applies on: methods or properties annotated with #[Query], #[Mutation] or #[Field] and one of #[Logged] or #[Right] attributes.

AttributeCompulsoryTypeDefinition
valueyesmixedThe value to return if the user is not authorized.

#[Field]

The #[Field] attribute is used to declare a GraphQL field.

Applies on: methods or properties of classes annotated with #[Type], #[ExtendType] or #[Input]. When it's applied on private or protected property, public getter or/and setter method is expected in the class accordingly 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.

AttributeCompulsoryTypeDefinition
namenostringThe name of the field. If skipped, the name of the method is used instead.
fornostring, arrayForces the field to be used only for specific output or input type(s). By default field is used for all possible declared types.
descriptionnostringField description displayed in the GraphQL docs. If it's empty PHP doc comment is used instead.
outputTypenostringForces the GraphQL output type of a query.
inputTypenostringForces the GraphQL input type of a query.

#[HideIfUnauthorized]

This attribute only works when a Schema is used to handle exactly one use request. If you serve your GraphQL API from long-running standalone servers (like Laravel Octane, Swoole, RoadRunner etc) and share the same Schema instance between multiple requests, please avoid using #[HideIfUnauthorized].

The #[HideIfUnauthorized] attribute is used to completely hide the query/mutation/subscription/field if the user is not authorized to access it (according to the #[Logged] and #[Right] attributes).

Applies on: methods or properties annotated with #[Query], #[Mutation] or #[Field] and one of #[Logged] or #[Right] attributes.

#[HideIfUnauthorized] and #[FailWith] are mutually exclusive.

#[HideParameter]

Removes an argument from the GraphQL schema.

AttributeCompulsoryTypeDefinition
foryesstringThe name of the PHP parameter to hide

#[InjectUser]

Use the #[InjectUser] attribute to inject an instance of the current user logged in into a parameter of your query/mutation/subscription/field.

See the authentication and authorization page for more details.

Applies on: methods annotated with #[Query], #[Mutation] or #[Field].

AttributeCompulsoryTypeDefinition
foryesstringThe name of the PHP parameter

#[Input]

The #[Input] attribute is used to declare a GraphQL input type.

Applies on: classes.

AttributeCompulsoryTypeDefinition
namenostringThe 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.
descriptionnostringDescription of the input type in the documentation. If not passed, PHP doc comment is used.
defaultnoboolName of the input type represented in your GraphQL schema. Defaults to true only if the name is not specified. If name is specified, this will default to false, so must also be included for true when name is used.
updatenoboolDetermines 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/subscription. This primarily applies to nullable fields.

#[Logged]

The #[Logged] attribute is used to declare a Query/Mutation/Field is only visible to logged users.

Applies on: methods or properties annotated with #[Query], #[Mutation] or #[Field].

This attribute allows no arguments.

#[MagicField]

The #[MagicField] attribute is used to declare a GraphQL field that originates from a PHP magic property (using __get magic method).

Applies on: classes annotated with #[Type] or #[ExtendType].

AttributeCompulsoryTypeDefinition
nameyesstringThe name of the field.
outputTypeno(*)stringThe GraphQL output type of the field.
phpTypeno(*)stringThe PHP type of the field (as you would write it in a Docblock)
descriptionnostringField description displayed in the GraphQL docs. If not set, no description will be shown.
sourceNamenostringThe name of the property in the source class. If not set, the name will be used to get property value.
annotationsnoarray\<Annotations>A set of annotations that apply to this field. You would typically used a "#[Logged]" or "#[Right]" attribute as class here.

(*) Note: outputType and phpType are mutually exclusive. You MUST provide one of them.

#[Mutation]

The #[Mutation] attribute is used to declare a GraphQL mutation.

Applies on: controller methods.

AttributeCompulsoryTypeDefinition
namenostringThe name of the mutation. If skipped, the name of the method is used instead.
outputTypenostringForces the GraphQL output type of a query.
descriptionnostringDescription of the mutation in the documentation. When omitted, the method's PHP docblock summary is used (see schema descriptions). An explicit empty string '' deliberately suppresses the docblock fallback.

#[Prefetch]

Marks field parameter to be used for prefetching.

Applies on: parameters of methods annotated with #[Query], #[Mutation] or #[Field].

AttributeCompulsoryTypeDefinition
callablenocallableName of the prefetch method (in same class) or a full callable, either a static method or regular service from the container

#[Query]

The #[Query] attribute is used to declare a GraphQL query.

Applies on: controller methods.

AttributeCompulsoryTypeDefinition
namenostringThe name of the query. If skipped, the name of the method is used instead.
outputTypenostringForces the GraphQL output type of a query.
descriptionnostringDescription of the query in the documentation. When omitted, the method's PHP docblock summary is used (see schema descriptions). An explicit empty string '' deliberately suppresses the docblock fallback.

The #[Right] attribute is used to declare a Query/Mutation/Field is only visible to users with a specific right.

Applies on: methods or properties annotated with #[Query], #[Mutation] or #[Field].

AttributeCompulsoryTypeDefinition
nameyesstringThe name of the right.

#[Security]

The #[Security] attribute can be used to check fin-grained access rights. It is very flexible: it allows you to pass an expression that can contains custom logic.

See the fine grained security page for more details.

Applies on: methods or properties annotated with #[Query], #[Mutation] or #[Field].

AttributeCompulsoryTypeDefinition
defaultyesstringThe security expression

#[SourceField]

The #[SourceField] attribute is used to declare a GraphQL field.

Applies on: classes annotated with #[Type] or #[ExtendType].

AttributeCompulsoryTypeDefinition
nameyesstringThe name of the field.
outputTypenostringForces the GraphQL output type of the field. Otherwise, return type is used.
phpTypenostringThe PHP type of the field (as you would write it in a Docblock)
descriptionnostringField description displayed in the GraphQL docs. If it's empty PHP doc comment of the method in the source class is used instead.
sourceNamenostringThe name of the property in the source class. If not set, the name will be used to get property value.
annotationsnoarray\<Annotations>A set of annotations that apply to this field. You would typically used a "#[Logged]" or "#[Right]" attribute as class here.

Note: outputType and phpType are mutually exclusive.

#[Subscription]

The #[Subscription] attribute is used to declare a GraphQL subscription.

Applies on: controller methods.

AttributeCompulsoryTypeDefinition
namenostringThe name of the subscription. If skipped, the name of the method is used instead.
outputTypenostringDefines the GraphQL output type that will be sent for the subscription.
descriptionnostringDescription of the subscription in the documentation. When omitted, the method's PHP docblock summary is used (see schema descriptions). An explicit empty string '' deliberately suppresses the docblock fallback.

#[Type]

The #[Type] attribute is used to declare a GraphQL object type. This is used with standard output types, as well as enum types. For input types, use the #[Input] attribute directly on the input type or a #[Factory] attribute to make/return an input type.

Applies on: classes.

AttributeCompulsoryTypeDefinition
classnostringThe targeted class/enum for the actual type. If no "class" attribute is passed, the type applies to the current class/enum. The current class/enum is assumed to be an entity (not service). If the "class" attribute is passed, the class/enum annotated with #[Type] becomes a service.
namenostringThe 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
defaultnoboolDefaults to true. Whether the targeted PHP class should be mapped by default to this type.
externalnoboolWhether this is an external type declaration 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.
descriptionnostringDescription of the GraphQL type in the schema documentation. When omitted, the class docblock summary is used (see schema descriptions). An explicit empty string '' deliberately suppresses the docblock fallback.

#[UseInputType]

Used to override the GraphQL input type of a PHP parameter.

Applies on: methods annotated with #[Query], #[Mutation] or #[Field] attribute.

AttributeCompulsoryTypeDefinition
foryesstringThe name of the PHP parameter
inputTypeyesstringThe GraphQL input type to force for this input field

#[Validate]

This attribute is only available in the GraphQLite Laravel package

Validates a user input in Laravel.

Applies on: methods annotated with #[Query], #[Mutation], #[Field], #[Factory] or #[Decorator] attribute.

AttributeCompulsoryTypeDefinition
foryesstringThe name of the PHP parameter
rule*yesstringLaravel validation rules

Sample:

#[Validate(for: "$email", rule: "email|unique:users")]