Skip to content

Commit a3da246

Browse files
committed
Docs: Add scalar type
1 parent dabb512 commit a3da246

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

docs/todo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ This library is still work in progress, and misses some valuable features:
55
- ~~Overwrite type via attributes~~
66
- ~~Allow simple lists (array type)~~
77
- ~~Make AST serializable (cacheable)~~
8+
- ~~Handle `DateTime` and `DateTimeImmutable`~~
89
- Connection, edge, nodes (see https://relay.dev/graphql/connections.htm)
9-
- Handle `DateTime` and `DateTimeImmutable`
1010
- GraphQL interfaces, inheritance
1111
- Inject autowiring services
1212
- Subscriptions

docs/usage.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The following attributes can be used:
1414
- [#[EnumValue]](#enum)
1515
- [#[Field]](#field)
1616
- [#[Arg]](#arg)
17+
- [#[Scalar]](#scalar)
1718

1819
See below for more information about each attribute:
1920

@@ -341,3 +342,55 @@ final readonly class YourType
341342
| `name` | Set custom name of argument (instead of based on class) |
342343
| `description` | Set description of the argument, readable in the GraphQL schema |
343344
| `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))`) |
345+
346+
### #[Scalar]
347+
Webonyx/graphql-php supports 4 native scalar types:
348+
- string
349+
- integer
350+
- boolean
351+
- float
352+
353+
Note: Scalar types can be used for input and output.
354+
355+
You can create your own custom scalar types with the attribute `#[Scalar]`.
356+
357+
```php
358+
use Jerowork\GraphqlAttributeSchema\Attribute\Scalar;
359+
use Jerowork\GraphqlAttributeSchema\Type\ScalarType;
360+
361+
#[Scalar]
362+
final readonly class CustomScalar implements ScalarType
363+
{
364+
public static function serialize(mixed $value): string
365+
{
366+
// ...
367+
}
368+
369+
public static function deserialize(string $value): mixed
370+
{
371+
// ...
372+
}
373+
}
374+
```
375+
376+
This custom scalar type can then be defined as type with option `type` within other attributes (e.g. `#[Field]`, `#[Mutation]`).
377+
The `type` option can be omitted when using `alias` in `#[Scalar]`, see options section below.
378+
379+
#### Requirements
380+
Custom scalar types:
381+
- must implement `ScalarType`.
382+
383+
#### Options
384+
| Option | Description |
385+
|---------------|-----------------------------------------------------------------------------------|
386+
| `name` | Set custom name of scalar type (instead of based on class) |
387+
| `description` | Set description of the scalar type, readable in the GraphQL schema |
388+
| `alias` | Map scalar type to another class, which removes the need to use the `type` option |
389+
390+
#### Custom ScalarType: DateTimeImmutable
391+
*GraphQL Attribute Schema* already has a custom scalar type built-in: [DateTimeType](../src/Type/DateTimeType.php).
392+
393+
With this custom type, `DateTimeImmutable` can be used out-of-the-box (without any `type` option definition).
394+
395+
When building the `Parser` with the `ParserFactory`, this custom scalar type is already registered.
396+
If not, add `DateTimeType` as a `$customTypes` in the `Parser` construct.

0 commit comments

Comments
 (0)