Skip to content

Commit 5812837

Browse files
committed
Clarify type syntax #9954
`API\Field` and `API\Input` must always specify a FQCN if referencing models. Otherwise, `ecodev/graphql-doctrine` is not able to find the corresponding GraphQL type. So from `'User'`, it cannot guess we really mean `'Application\Model\User'`. The easiest solution is to use `::class` notation as much as possible, even for non-models: ```diff - #[API\Field(type: 'Group')] + #[API\Field(type: Group::class)] ``` ```diff - #[API\Input(type: 'Login')] + #[API\Input(type: LoginType::class)] ``` And keep string literals only when we must specify a nullable or list type, such as: ```php #[API\Field(type: '?Login')] ``` or ```php #[API\Input(type: '?TagID[]')] ```
1 parent 5acf016 commit 5812837

File tree

6 files changed

+27
-24
lines changed

6 files changed

+27
-24
lines changed

README.md

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -197,24 +197,28 @@ public function getStatus(): string
197197
}
198198
```
199199

200-
The type must be the PHP class implementing the GraphQL type (see
201-
[limitations](#limitations)). The declaration can be defined as nullable and/or as
202-
an array with one the following syntaxes (PHP style or GraphQL style):
203-
204-
- `?MyType`
205-
- `null|MyType`
206-
- `MyType|null`
207-
- `MyType[]`
208-
- `?MyType[]`
209-
- `null|MyType[]`
210-
- `MyType[]|null`
211-
- `Collection<MyType>`
200+
### Type syntax
201+
202+
In most cases, the type must use the `::class` notation to specify the PHP class that is either implementing the GraphQL
203+
type or the entity itself (see [limitations](#limitations)). Use string literals only if you must define it as nullable
204+
and/or as an array. Never use the short name of an entity (it is only possible for user-defined custom types).
205+
206+
Supported syntaxes (PHP style or GraphQL style) are:
207+
208+
- `MyType::class`
209+
- `'?Application\MyType'`
210+
- `'null|Application\MyType'`
211+
- `'Application\MyType|null'`
212+
- `'Application\MyType[]'`
213+
- `'?Application\MyType[]'`
214+
- `'null|Application\MyType[]'`
215+
- `'Application\MyType[]|null'`
216+
- `'Collection<Application\MyType>'`
212217

213218
This attribute can be used to override other things, such as `name`, `description`
214219
and `args`.
215220

216-
217-
#### Override arguments
221+
### Override arguments
218222

219223
Similarly to `#[API\Field]`, `#[API\Argument]` allows to override the type of argument
220224
if the PHP type hint is not enough:
@@ -293,7 +297,6 @@ implementation configured according to your needs. In the following example, we
293297
because it offers useful concepts such as: invokables, aliases, factories and abstract
294298
factories. But any other PSR-11 container implementation could be used instead.
295299

296-
297300
The keys should be the whatever you use to refer to the type in your model. Typically,
298301
that would be either the FQCN of a PHP class "native" type such as `DateTimeImmutable`, or the
299302
FQCN of a PHP class implementing the GraphQL type, or directly the GraphQL type name:
@@ -431,7 +434,7 @@ possible to write custom filters and sorting.
431434

432435
#### Custom filters
433436

434-
A custom filer must extend `AbstractOperator`. This will allow to define custom arguments for
437+
A custom filer must extend `AbstractOperator`. This will allow to define custom arguments for
435438
the API, and then a method to build the DQL condition corresponding to the argument.
436439

437440
This would also allow to filter on joined relations by carefully adding joins when necessary.
@@ -472,12 +475,13 @@ use GraphQLTests\Doctrine\Blog\Sorting\UserName;
472475
#[API\Sorting([UserName::class])]
473476
final class Post extends AbstractModel
474477
```
478+
475479
## Limitations
476480

477481
### Namespaces
478482

479-
The `use` statement is not supported. So types in attributes or doc blocks should
480-
either be the FQCN or in the same namespace as the getter.
483+
The `use` statement is not supported. So types in attributes or doc blocks must
484+
be the FQCN, or the name of a user-defined custom types (but never the short name of an entity).
481485

482486
### Composite identifiers
483487

@@ -513,7 +517,6 @@ Those cases would probably end up being too complex to handle on the client-side
513517
instead to implement them as a custom filter on the server side, in order to hide complexity
514518
from the client and benefit from Doctrine's QueryBuilder full flexibility.
515519

516-
517520
### Sorting on join
518521

519522
Out of the box, it is not possible to sort by a field from a joined relation.

src/Attribute/AbstractAttribute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ abstract class AbstractAttribute implements ApiAttribute
2323
private bool $hasDefaultValue = false;
2424

2525
/**
26-
* @param null|string $type FQCN of PHP class implementing the GraphQL type
26+
* @param null|string $type FQCN of PHP class implementing the GraphQL type, see README.md#type-syntax
2727
*/
2828
public function __construct(
2929
private ?string $name,

src/Attribute/Argument.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
final class Argument extends AbstractAttribute
1717
{
1818
/**
19-
* @param null|string $type FQCN of PHP class implementing the GraphQL type
19+
* @param null|string $type FQCN of PHP class implementing the GraphQL type, see README.md#type-syntax
2020
*/
2121
public function __construct(
2222
?string $type = null,

src/Attribute/Field.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final class Field implements ApiAttribute
2222

2323
/**
2424
* @param null|string $name Can be used to alias the field
25-
* @param null|string $type FQCN of PHP class implementing the GraphQL type
25+
* @param null|string $type FQCN of PHP class implementing the GraphQL type, see README.md#type-syntax
2626
*/
2727
public function __construct(
2828
public ?string $name = null,

src/Attribute/FilterGroupCondition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
final class FilterGroupCondition implements ApiAttribute
1717
{
1818
/**
19-
* @param string $type FQCN of PHP class implementing the GraphQL type
19+
* @param string $type FQCN of PHP class implementing the GraphQL type, see README.md#type-syntax
2020
*/
2121
public function __construct(public readonly string $type)
2222
{

src/Attribute/Input.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
final class Input extends AbstractAttribute
1717
{
1818
/**
19-
* @param null|string $type FQCN of PHP class implementing the GraphQL type
19+
* @param null|string $type FQCN of PHP class implementing the GraphQL type, see README.md#type-syntax
2020
*/
2121
public function __construct(
2222
?string $type = null,

0 commit comments

Comments
 (0)