Skip to content

Commit 61f4461

Browse files
committed
Add a Security section
1 parent cd77981 commit 61f4461

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

core/graphql.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,42 @@ Or order your results like:
135135
}
136136
}
137137
```
138+
139+
## Security (`access_control`)
140+
141+
To add a security layer to your queries and mutations, follow the [security](security.md) documentation.
142+
143+
If your security needs differ between REST and GraphQL, add the particular parts in the `graphql` key.
144+
145+
In the example below, we want the same security rules as in REST, but we also want to allow an admin to delete a book in GraphQL only.
146+
Please note it's not possible to update a book in GraphQL because the `update` operation is not defined.
147+
148+
```php
149+
<?php
150+
// api/src/Entity/Book.php
151+
152+
namespace App\Entity;
153+
154+
use ApiPlatform\Core\Annotation\ApiResource;
155+
156+
/**
157+
* @ApiResource(
158+
* attributes={"access_control"="is_granted('ROLE_USER')"},
159+
* collectionOperations={
160+
* "post"={"access_control"="is_granted('ROLE_ADMIN')", "access_control_message"="Only admins can add books."}
161+
* },
162+
* itemOperations={
163+
* "get"={"access_control"="is_granted('ROLE_USER') and object.owner == user", "access_control_message"="Sorry, but you are not the book owner."}
164+
* },
165+
* graphql={
166+
* "query"={"access_control"="is_granted('ROLE_USER') and object.owner == user"},
167+
* "delete"={"access_control"="is_granted('ROLE_ADMIN')"},
168+
* "create"={"access_control"="is_granted('ROLE_ADMIN')"}
169+
* }
170+
* )
171+
*/
172+
class Book
173+
{
174+
// ...
175+
}
176+
```

0 commit comments

Comments
 (0)