/
graphql-subscriptions.md
104 lines (84 loc) · 3.13 KB
/
graphql-subscriptions.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
---
title: "GraphQL Subscriptions"
date: 2021-09-01
tags: [""]
description: ""
menu:
main:
parent: "GraphQL"
weight: 11
---
Tyk **natively** supports also GraphQL subscriptions, so you can expose your full range of GQL operations using Tyk Gateway. Subscriptions support was added in `v4.0.0` in which *graphql-ws* protocol support was introduced.
With the release of Tyk `v4.3.0` the number of supported subscription protocols has been extended.
In Tyk subscriptions are using the [WebSocket transport](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API) for connections between the client and Gateway. For connections between Gateway and upstream WebSockets or [SSE](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) can be used.
### Supported transports and protocols
| Transport | Protocol |
|------------|--------------------------------------------------------------------------------------------------------------------------|
| WebSockets | [graphql-ws](http://github.com/apollographql/subscriptions-transport-ws) (default, no longer maintained) |
| WebSockets | [graphql-transport-ws](http://github.com/enisdenjo/graphql-ws) |
| HTTP | [Server-Sent Events (SSE)](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) |
#### Setting up subscription types via API definition
Subscription types or subscription transports/protocols are set inside the graphql section of the API definition.
Depending on whether you want to configure GraphQL proxy-only, UDG, or GraphQL Federation there are different places for the configuration option.
The values for subscription types are the same on all API types:
- `graphql-ws`
- `graphql-transport-ws`
- `sse` (Server-Sent Events)
##### GraphQL Proxy
```
{
...,
"graphql": {
...,
"proxy": {
...,
"subscription_type": "graphql-ws"
}
}
}
```
##### Universal Data Graph
```
{
...,
"graphql": {
...,
"engine": {
...,
"data_sources": [
...,
{
...,
"subscription_type": "sse"
}
]
}
}
}
```
##### Federation
```
{
...,
"graphql": {
...,
"supergraph": {
...,
"subgraphs": [
...,
{
...,
"subscription_type": "graphql-transport-ws"
}
]
}
}
}
```
{{< note >}}
**Note**
If the upstream subscription GraphQL API is protected please enable the authentication via query params to pass the header through.
{{< /note >}}
There is no need to enable subscriptions separately. They are supported alongside GraphQL as a standard. The only requirement for subscriptions to work is to [enable WebSockets]({{< ref "graphql/graphql-websockets.md" >}}) in your Tyk Gateway configuration file.
Here's a general sequence diagram showing how subscriptions in Tyk work exactly:
{{< img src="img/dashboard/graphql/tyk-subscriptions-workflow.png" alt="Tyk Subscriptions workflow" >}}