Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 164 additions & 21 deletions core/graphql.md
Original file line number Diff line number Diff line change
Expand Up @@ -804,17 +804,22 @@ to allow a client to receive pushed realtime data from the server.
In API Platform, the built-in subscription support is handled by using
[Mercure](https://mercure.rocks/) as its underlying protocol.

### Enable Update Subscriptions for a Resource
### Enable Subscriptions for a Resource

To enable update subscriptions for a resource, these conditions have to be met:
To enable GraphQL subscriptions for a resource, these conditions have to be met:

- the
[Mercure hub and bundle need to be installed and configured](mercure.md#installing-mercure-support).
- Mercure needs to be enabled for the resource.
- the `update` mutation needs to be enabled for the resource.
- the subscription needs to be enabled for the resource.
- at least one subscription operation needs to be enabled for the resource.

For instance, your resource should look like this:
API Platform provides two GraphQL subscription operation types:

- `Subscription`: subscribes to updates for a specific item.
- `SubscriptionCollection`: subscribes to future events affecting a collection.

For instance, your resource could expose both an item subscription and a collection subscription:

<code-selector>

Expand All @@ -826,10 +831,12 @@ namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GraphQl\Mutation;
use ApiPlatform\Metadata\GraphQl\Subscription;
use ApiPlatform\Metadata\GraphQl\SubscriptionCollection;

#[ApiResource(mercure: true, graphQlOperations: [
new Mutation(name: 'update'),
new Subscription()
new Subscription(name: 'update'),
new SubscriptionCollection(name: 'update_collection'),
])]
class Book
{
Expand All @@ -844,7 +851,10 @@ resources:
graphQlOperations:
ApiPlatform\Metadata\GraphQl\Mutation:
name: update
ApiPlatform\Metadata\GraphQl\Subscription: ~
ApiPlatform\Metadata\GraphQl\Subscription:
name: update
ApiPlatform\Metadata\GraphQl\SubscriptionCollection:
name: update_collection
```

```xml
Expand All @@ -856,29 +866,28 @@ resources:
<resource class="App\Entity\Book">
<graphQlOperations>
<graphQlOperation class="ApiPlatform\Metadata\GraphQl\Mutation" name="update" />
<graphQlOperation class="ApiPlatform\Metadata\GraphQl\Subscription" />
<graphQlOperation class="ApiPlatform\Metadata\GraphQl\Subscription" name="update" />
<graphQlOperation class="ApiPlatform\Metadata\GraphQl\SubscriptionCollection" name="update_collection" />
</graphQlOperations>
</resource>
</resources>
```

</code-selector>

### Subscribe
### Subscribe to an Item

Doing a subscription is very similar to doing a query:
An item subscription is very similar to doing a query:

```graphql
{
subscription {
updateBookSubscribe(input: { id: "/books/1", clientSubscriptionId: "myId" }) {
book {
title
isbn
}
mercureUrl
clientSubscriptionId
subscription {
updateBookSubscribe(input: { id: "/books/1", clientSubscriptionId: "myId" }) {
book {
title
isbn
}
mercureUrl
clientSubscriptionId
}
}
```
Expand All @@ -889,18 +898,75 @@ As you can see, you need to pass the **IRI** of the resource as argument. See