Skip to content

Commit 4eac9bf

Browse files
committed
Adding doc for ID class
1 parent e07b172 commit 4eac9bf

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

docs/type_mapping.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,50 @@ Scalar PHP types can be type-hinted to the corresponding GraphQL types:
2727

2828
GraphQL comes with a native "ID" type. PHP has no such type.
2929

30-
TODO: develop a ID PHP class type? (for input types?)
30+
If you want to expose an "ID" type in your GraphQL model, you have 2 solutions:
3131

32+
### Solution 1: force the outputType
33+
34+
```php
35+
/**
36+
* @Field(outputType="ID")
37+
*/
38+
public function getId(): string
39+
{
40+
41+
}
42+
```
43+
44+
Using the "outputType" attribute of the `@Field` annotation, you can force the output type to "ID".
45+
You can learn more about [forcing output types in the "custom output types" documentation](custom_output_types.md).
46+
47+
### Solution 2: use the "ID" class
48+
49+
```php
50+
use TheCodingMachine\GraphQL\Controllers\Types\ID;
51+
52+
/**
53+
* @Field
54+
*/
55+
public function getId(): ID
56+
{
57+
58+
}
59+
```
60+
61+
Note that you can also use the "ID" class as an input type:
62+
63+
```php
64+
use TheCodingMachine\GraphQL\Controllers\Types\ID;
65+
66+
/**
67+
* @Mutation
68+
*/
69+
public function save(ID $id, string $name): Product
70+
{
71+
72+
}
73+
```
3274

3375
## Mapping of dates
3476

0 commit comments

Comments
 (0)