Skip to content

Commit 84d0dfd

Browse files
committed
Allow to specify subprotocols in the websockets transport
1 parent 12fc895 commit 84d0dfd

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

docs/transports/websockets.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ The websockets transport supports both:
88
- the `Apollo websockets transport protocol`_.
99
- the `GraphQL-ws websockets transport protocol`_
1010

11-
It will detect the backend supported protocol from the response http headers returned.
11+
It will propose both subprotocols to the backend and detect the supported protocol
12+
from the response http headers returned by the backend.
13+
14+
.. note::
15+
For some backends (graphql-ws without backwards compatibility), it may be necessary to specify
16+
only one subprotocol to the backend. It can be done by using
17+
:code:`subprotocols=[WebsocketsTransport.GRAPHQLWS_SUBPROTOCOL]`
18+
or :code:`subprotocols=[WebsocketsTransport.APOLLO_SUBPROTOCOL]` in the transport arguments.
1219

1320
This transport allows to do multiple queries, mutations and subscriptions on the same websocket connection.
1421

gql/transport/websockets.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44
from contextlib import suppress
55
from ssl import SSLContext
6-
from typing import Any, Dict, Optional, Tuple, Union, cast
6+
from typing import Any, Dict, List, Optional, Tuple, Union, cast
77

88
from graphql import DocumentNode, ExecutionResult, print_ast
99
from websockets.datastructures import HeadersLike
@@ -46,6 +46,7 @@ def __init__(
4646
pong_timeout: Optional[Union[int, float]] = None,
4747
answer_pings: bool = True,
4848
connect_args: Dict[str, Any] = {},
49+
subprotocols: Optional[List[Subprotocol]] = None,
4950
) -> None:
5051
"""Initialize the transport with the given parameters.
5152
@@ -71,6 +72,9 @@ def __init__(
7172
(for the graphql-ws protocol).
7273
By default: True
7374
:param connect_args: Other parameters forwarded to websockets.connect
75+
:param subprotocols: list of subprotocols sent to the
76+
backend in the 'subprotocols' http header.
77+
By default: both apollo and graphql-ws subprotocols.
7478
"""
7579

7680
super().__init__(
@@ -105,10 +109,13 @@ def __init__(
105109
"""pong_received is an asyncio Event which will fire each time
106110
a pong is received with the graphql-ws protocol"""
107111

108-
self.supported_subprotocols = [
109-
self.APOLLO_SUBPROTOCOL,
110-
self.GRAPHQLWS_SUBPROTOCOL,
111-
]
112+
if subprotocols is None:
113+
self.supported_subprotocols = [
114+
self.APOLLO_SUBPROTOCOL,
115+
self.GRAPHQLWS_SUBPROTOCOL,
116+
]
117+
else:
118+
self.supported_subprotocols = subprotocols
112119

113120
async def _wait_ack(self) -> None:
114121
"""Wait for the connection_ack message. Keep alive messages are ignored"""

tests/conftest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,9 @@ async def client_and_graphqlws_server(graphqlws_server):
465465
# Generate transport to connect to the server fixture
466466
path = "/graphql"
467467
url = f"ws://{graphqlws_server.hostname}:{graphqlws_server.port}{path}"
468-
sample_transport = WebsocketsTransport(url=url)
468+
sample_transport = WebsocketsTransport(
469+
url=url, subprotocols=[WebsocketsTransport.GRAPHQLWS_SUBPROTOCOL],
470+
)
469471

470472
async with Client(transport=sample_transport) as session:
471473

0 commit comments

Comments
 (0)