Skip to content

Commit c07c670

Browse files
committed
Upgrade attribute type option system
Introducing: - NullableType: allows to overwrite nullable class types as well - ListType: allows to define lists
1 parent 5860c16 commit c07c670

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+594
-343
lines changed

docs/usage.md

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ The following attributes can be used:
77
- `#[InputType]`
88
- `#[Type]`
99
- `#[Enum]`
10-
- `#[EnumValue]`
10+
- `#[EnumValue]`
1111
- `#[Field]`
1212
- `#[Arg]`
1313

@@ -61,12 +61,11 @@ Mutations and queries:
6161

6262
Both `#[Mutation]` and `#[Query]` attribute can be configured:
6363

64-
| Option | Description |
65-
|---------------|-------------------------------------------------------------------------------|
66-
| `name` | Set custom name of mutation or query (instead of based on class) |
67-
| `description` | Set description of the mutation or query, readable in the GraphQL schema |
68-
| `type` | Set custom return type; can be either a `#[Type]` FQCN or `ScalarType` (enum) |
69-
| `isRequired` | When a custom type is set, isRequired should be set as well |
64+
| Option | Description |
65+
|---------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
66+
| `name` | Set custom name of mutation or query (instead of based on class) |
67+
| `description` | Set description of the mutation or query, readable in the GraphQL schema |
68+
| `type` | Set custom return type; it can be:<br/>- A Type (FQCN)<br/>- A `ScalarType` (e.g. `ScalarType::Int`)<br/>- A `ListType` (e.g. `new ListType(ScalarType::Int)`)<br/>- A `NullableType` (e.g. `new NullableType(SomeType::class)`)<br/>- A combination of `ListType` and `NullableType` and a Type FQCN or `ScalarType` <br/>(e.g. `new NullableType(new ListType(ScalarType::String))`) |
7069

7170
## InputType
7271

@@ -272,12 +271,11 @@ final readonly class YourInputType
272271

273272
`#[Field]` attribute can be configured:
274273

275-
| Option | Description |
276-
|---------------|----------------------------------------------------------------------------------------|
277-
| `name` | Set custom name of field (instead of based on class) |
278-
| `description` | Set description of the field, readable in the GraphQL schema |
279-
| `type` | Set custom type; can be either a `#[Type]`, `#[InputType]` FQCN or `ScalarType` (enum) |
280-
| `isRequired` | When a custom type is set, isRequired should be set as well |
274+
| Option | Description |
275+
|---------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
276+
| `name` | Set custom name of field (instead of based on class) |
277+
| `description` | Set description of the field, readable in the GraphQL schema |
278+
| `type` | Set custom return type; it can be:<br/>- A Type (FQCN)<br/>- A `ScalarType` (e.g. `ScalarType::Int`)<br/>- A `ListType` (e.g. `new ListType(ScalarType::Int)`)<br/>- A `NullableType` (e.g. `new NullableType(SomeType::class)`)<br/>- A combination of `ListType` and `NullableType` and a Type FQCN or `ScalarType` <br/>(e.g. `new NullableType(new ListType(ScalarType::String))`) |
281279

282280
## Arg
283281

@@ -331,9 +329,8 @@ final readonly class YourType
331329

332330
`#[Arg]` attribute can be configured:
333331

334-
| Option | Description |
335-
|---------------|------------------------------------------------------------------------|
336-
| `name` | Set custom name of argument (instead of based on class) |
337-
| `description` | Set description of the argument, readable in the GraphQL schema |
338-
| `type` | Set custom type; can be either a `#[Type]` FQCN or `ScalarType` (enum) |
339-
| `isRequired` | When a custom type is set, isRequired should be set as well |
332+
| Option | Description |
333+
|---------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
334+
| `name` | Set custom name of argument (instead of based on class) |
335+
| `description` | Set description of the argument, readable in the GraphQL schema |
336+
| `type` | Set custom return type; it can be:<br/>- A Type (FQCN)<br/>- A `ScalarType` (e.g. `ScalarType::Int`)<br/>- A `ListType` (e.g. `new ListType(ScalarType::Int)`)<br/>- A `NullableType` (e.g. `new NullableType(SomeType::class)`)<br/>- A combination of `ListType` and `NullableType` and a Type FQCN or `ScalarType` <br/>(e.g. `new NullableType(new ListType(ScalarType::String))`) |

src/Attribute/Arg.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66

77
use Attribute;
88
use Jerowork\GraphqlAttributeSchema\Attribute\Option\ScalarType;
9+
use Jerowork\GraphqlAttributeSchema\Attribute\Option\Type;
910

1011
#[Attribute(Attribute::TARGET_PARAMETER)]
1112
final readonly class Arg implements BaseAttribute, TypedAttribute
1213
{
1314
/**
14-
* @param class-string|ScalarType|null $type
15+
* @param class-string|Type|ScalarType|null $type
1516
*/
1617
public function __construct(
1718
public ?string $name = null,
1819
public ?string $description = null,
19-
public string|ScalarType|null $type = null,
20-
public bool $isRequired = true,
20+
public string|Type|ScalarType|null $type = null,
2121
) {}
2222

2323
public function getName(): ?string
@@ -30,13 +30,8 @@ public function getDescription(): ?string
3030
return $this->description;
3131
}
3232

33-
public function getType(): string|ScalarType|null
33+
public function getType(): string|Type|ScalarType|null
3434
{
3535
return $this->type;
3636
}
37-
38-
public function isRequired(): bool
39-
{
40-
return $this->isRequired;
41-
}
4237
}

src/Attribute/Field.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66

77
use Attribute;
88
use Jerowork\GraphqlAttributeSchema\Attribute\Option\ScalarType;
9+
use Jerowork\GraphqlAttributeSchema\Attribute\Option\Type;
910

1011
#[Attribute(Attribute::TARGET_PROPERTY|Attribute::TARGET_METHOD)]
1112
final readonly class Field implements BaseAttribute, TypedAttribute
1213
{
1314
/**
14-
* @param class-string|ScalarType|null $type
15+
* @param class-string|Type|ScalarType|null $type
1516
*/
1617
public function __construct(
1718
public ?string $name = null,
1819
public ?string $description = null,
19-
public string|ScalarType|null $type = null,
20-
public bool $isRequired = true,
20+
public string|Type|ScalarType|null $type = null,
2121
) {}
2222

2323
public function getName(): ?string
@@ -30,13 +30,8 @@ public function getDescription(): ?string
3030
return $this->description;
3131
}
3232

33-
public function getType(): string|ScalarType|null
33+
public function getType(): string|Type|ScalarType|null
3434
{
3535
return $this->type;
3636
}
37-
38-
public function isRequired(): bool
39-
{
40-
return $this->isRequired;
41-
}
4237
}

src/Attribute/Mutation.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66

77
use Attribute;
88
use Jerowork\GraphqlAttributeSchema\Attribute\Option\ScalarType;
9+
use Jerowork\GraphqlAttributeSchema\Attribute\Option\Type;
910

1011
#[Attribute(Attribute::TARGET_CLASS)]
1112
final readonly class Mutation implements BaseAttribute, TypedAttribute
1213
{
1314
/**
14-
* @param class-string|ScalarType|null $type
15+
* @param class-string|Type|ScalarType|null $type
1516
*/
1617
public function __construct(
1718
public ?string $name = null,
1819
public ?string $description = null,
19-
public string|ScalarType|null $type = null,
20-
public bool $isRequired = true,
20+
public string|Type|ScalarType|null $type = null,
2121
) {}
2222

2323
public function getName(): ?string
@@ -30,13 +30,8 @@ public function getDescription(): ?string
3030
return $this->description;
3131
}
3232

33-
public function getType(): string|ScalarType|null
33+
public function getType(): string|Type|ScalarType|null
3434
{
3535
return $this->type;
3636
}
37-
38-
public function isRequired(): bool
39-
{
40-
return $this->isRequired;
41-
}
4237
}

src/Attribute/Option/ListType.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jerowork\GraphqlAttributeSchema\Attribute\Option;
6+
7+
final readonly class ListType implements Type
8+
{
9+
/**
10+
* @param class-string|Type|ScalarType $type
11+
*/
12+
public function __construct(
13+
public string|Type|ScalarType $type,
14+
) {}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jerowork\GraphqlAttributeSchema\Attribute\Option;
6+
7+
final readonly class NullableType implements Type
8+
{
9+
/**
10+
* @param class-string|Type|ScalarType $type
11+
*/
12+
public function __construct(
13+
public string|Type|ScalarType $type,
14+
) {}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jerowork\GraphqlAttributeSchema\Attribute\Option;
6+
7+
final readonly class ObjectType implements Type
8+
{
9+
/**
10+
* @param class-string $className
11+
*/
12+
public function __construct(
13+
public string $className,
14+
) {}
15+
}

src/Attribute/Option/Type.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Jerowork\GraphqlAttributeSchema\Attribute\Option;
6+
7+
interface Type {}

src/Attribute/Query.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66

77
use Attribute;
88
use Jerowork\GraphqlAttributeSchema\Attribute\Option\ScalarType;
9+
use Jerowork\GraphqlAttributeSchema\Attribute\Option\Type;
910

1011
#[Attribute(Attribute::TARGET_CLASS)]
1112
final readonly class Query implements BaseAttribute, TypedAttribute
1213
{
1314
/**
14-
* @param class-string|ScalarType|null $type
15+
* @param class-string|Type|ScalarType|null $type
1516
*/
1617
public function __construct(
1718
public ?string $name = null,
1819
public ?string $description = null,
19-
public string|ScalarType|null $type = null,
20-
public bool $isRequired = true,
20+
public string|Type|ScalarType|null $type = null,
2121
) {}
2222

2323
public function getName(): ?string
@@ -30,13 +30,8 @@ public function getDescription(): ?string
3030
return $this->description;
3131
}
3232

33-
public function getType(): string|ScalarType|null
33+
public function getType(): string|Type|ScalarType|null
3434
{
3535
return $this->type;
3636
}
37-
38-
public function isRequired(): bool
39-
{
40-
return $this->isRequired;
41-
}
4237
}

src/Attribute/TypedAttribute.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
namespace Jerowork\GraphqlAttributeSchema\Attribute;
66

77
use Jerowork\GraphqlAttributeSchema\Attribute\Option\ScalarType;
8+
use Jerowork\GraphqlAttributeSchema\Attribute\Option\Type;
89

910
interface TypedAttribute
1011
{
1112
/**
12-
* @return class-string|ScalarType|null
13+
* @return class-string|Type|ScalarType|null
1314
*/
14-
public function getType(): string|ScalarType|null;
15-
16-
public function isRequired(): bool;
15+
public function getType(): string|Type|ScalarType|null;
1716
}

0 commit comments

Comments
 (0)