Skip to content

Commit 3c5987d

Browse files
committed
Complete documentation
1 parent 82ad8f1 commit 3c5987d

File tree

1 file changed

+62
-28
lines changed

1 file changed

+62
-28
lines changed

README.md

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ $schema = new Schema([
9696
## Usage
9797

9898
The public API is limited to the public methods on `Types` and the annotations.
99-
So the it's the constructor and:
99+
So that's the constructor and:
100100

101101
- `$types->get()` to get either an `ObjectType` from an entity or any other
102102
custom types (eg: `string` or mapped type)
@@ -119,8 +119,8 @@ existing type hints and dock blocks should cover the majority of cases.
119119

120120
### Exclude a field
121121

122-
All getters are included by default in the type. But it can be specified
123-
otherwise for each getter. To exclude a sensitive field from ever being exposed
122+
All getters, and setters, are included by default in the type. But it can be specified
123+
otherwise for each method. To exclude a sensitive field from ever being exposed
124124
through the API, use `@API\Exclude`:
125125

126126
```php
@@ -136,7 +136,7 @@ public function getPassword(): string
136136
}
137137
```
138138

139-
### Override scalar types
139+
### Override output types
140140

141141
Even if a getter returns a PHP scalar type, such as `string`, it might be preferable
142142
to override the type with a custom GraphQL type. This is typically useful for enum
@@ -156,29 +156,23 @@ public function getStatus(): string
156156
}
157157
```
158158

159-
That annotation can be used to override other things, such as `name`, `description`
160-
and `args`.
161-
162-
### Custom types
163-
164-
By default all PHP scalar types and Doctrine collection are automatically detected
165-
and mapped to a GraphQL type. However if some getter return custom types, such
166-
as `DateTime`, or a custom class, then it will have to be configured beforehand:
159+
The type must be the PHP class implementing the GraphQL type (see
160+
[limitations](#limitations)). The declaration can be defined as nullable and/or as
161+
an array with one the following syntaxes (PHP style or GraphQL style):
167162

168-
```php
169-
$mapping = [
170-
DateTime::class => DateTimeType::class,
171-
];
172-
173-
$types = new Types($entityManager, $mapping);
163+
- `?MyType`
164+
- `null|MyType`
165+
- `MyType|null`
166+
- `MyType[]`
167+
- `?MyType[]`
168+
- `null|MyType[]`
169+
- `MyType[]|null`
174170

175-
// Build schema...
176-
```
171+
This annotation can be used to override other things, such as `name`, `description`
172+
and `args`.
177173

178-
That way it is not necessary to annotate every single getter returning one of the
179-
configured type. It will be mapped automatically.
180174

181-
### Override arguments
175+
#### Override arguments
182176

183177
Similarly to `@API\Field`, `@API\Argument` allows to override the type of argument
184178
if the PHP type hint is not enough:
@@ -199,6 +193,46 @@ public function getPosts(?string $status = Post::STATUS_PUBLIC): Collection
199193
Once again, it also allows to override other things such as `name`, `description`
200194
and `defaultValue`.
201195

196+
### Override input types
197+
198+
`@API\Input` is the opposite of `@API\Field` and can be used to override things for
199+
input types (setters), typically for validations purpose. This would look like:
200+
201+
```php
202+
/**
203+
* Set status
204+
*
205+
* @API\Input(type="GraphQLTests\Doctrine\Blog\Types\PostStatusType")
206+
*
207+
* @param string $status
208+
*/
209+
public function setStatus(string $status = self::STATUS_PUBLIC): void
210+
{
211+
$this->status = $status;
212+
}
213+
```
214+
215+
This annotation also supports `name`, `description`, and `defaultValue`.
216+
217+
### Custom types
218+
219+
By default all PHP scalar types and Doctrine collection are automatically detected
220+
and mapped to a GraphQL type. However if some getter return custom types, such
221+
as `DateTime`, or a custom class, then it will have to be configured beforehand:
222+
223+
```php
224+
$mapping = [
225+
DateTime::class => DateTimeType::class,
226+
];
227+
228+
$types = new Types($entityManager, $mapping);
229+
230+
// Build schema...
231+
```
232+
233+
That way it is not necessary to annotate every single getter returning one of the
234+
configured type. It will be mapped automatically.
235+
202236
### Entities as input arguments
203237

204238
If a getter takes an entity as parameter, then a specialized `InputType` will
@@ -217,7 +251,7 @@ You may also get an input type for an entity by using `Types::getInput()`:
217251

218252
```php
219253
// Custom InputType
220-
$userIdType = $types->getInput(User::class);
254+
$userInputType = $types->getInput(User::class);
221255
```
222256

223257
And then use it like so in your resolver:
@@ -250,7 +284,7 @@ adapt the database schema.
250284

251285
[Doctrine GraphQL Mapper](https://github.com/rahuljayaraman/doctrine-graphql) has
252286
been an inspiration to write this package. While the goals are similar, the way
253-
it works is different. Annotations are spread between properties and getter, but
254-
we work only on getter. Setup seems slightly more complex, but might be more
255-
flexible. We built on conventions and widespread use of PHP 7.1 type hinting
256-
to have an easier out-of-the-box experience.
287+
it works is different. In Doctrine GraphQL Mapper, annotations are spread between
288+
properties and methods, but we work only on methods. Setup seems slightly more complex,
289+
but might be more flexible. We built on conventions and widespread use of PHP 7.1
290+
type hinting to have an easier out-of-the-box experience.

0 commit comments

Comments
 (0)