Skip to content

Commit

Permalink
Added an option to pass connection params when initiating connection … (
Browse files Browse the repository at this point in the history
#3403)

* Added an option to pass connection params when initiating connection on strawberry channels test case

* Optimized code. Added test case. Added RELEASE.md

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* added back poetry.lock file

* updated release file

* updated RELEASE.md file

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* added connection params to docs

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* added connection params to docs

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
selvarajrajkanna and pre-commit-ci[bot] committed Mar 8, 2024
1 parent 364d152 commit 0c923bb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
15 changes: 15 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Release type: minor

This release adds support to allow passing `connection_params` as dictionary to `GraphQLWebsocketCommunicator` class when testing [channels integration](https://strawberry.rocks/docs/integrations/channels#testing)


### Example


```python
GraphQLWebsocketCommunicator(
application=application,
path="/graphql",
connection_params={"username": "strawberry"},
)
```
14 changes: 14 additions & 0 deletions docs/integrations/channels.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,20 @@ The HTTP and WebSockets protocol are handled by different base classes. HTTP
uses `GraphQLHTTPConsumer` and WebSockets uses `GraphQLWSConsumer`. Both of them
can be extended:

### Passing connection params

Connection parameters can be passed using the `connection_params` parameter of
the `GraphQLWebsocketCommunicator` class. This is particularily useful to test
websocket authentication.

```python
GraphQLWebsocketCommunicator(
application=application,
path="/graphql",
connection_params={"token": "strawberry"},
)
```

## GraphQLHTTPConsumer (HTTP)

### Options
Expand Down
6 changes: 5 additions & 1 deletion strawberry/channels/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def __init__(
path: str,
headers: Optional[List[Tuple[bytes, bytes]]] = None,
protocol: str = GRAPHQL_TRANSPORT_WS_PROTOCOL,
connection_params: dict = {},
**kwargs: Any,
):
"""
Expand All @@ -81,6 +82,7 @@ def __init__(
self.protocol = protocol
subprotocols = kwargs.get("subprotocols", [])
subprotocols.append(protocol)
self.connection_params = connection_params
super().__init__(application, path, headers, subprotocols=subprotocols)

async def __aenter__(self) -> Self:
Expand All @@ -99,7 +101,9 @@ async def gql_init(self) -> None:
res = await self.connect()
if self.protocol == GRAPHQL_TRANSPORT_WS_PROTOCOL:
assert res == (True, GRAPHQL_TRANSPORT_WS_PROTOCOL)
await self.send_json_to(ConnectionInitMessage().as_dict())
await self.send_json_to(
ConnectionInitMessage(payload=self.connection_params).as_dict()
)
response = await self.receive_json_from()
assert response == ConnectionAckMessage().as_dict()
else:
Expand Down
10 changes: 9 additions & 1 deletion tests/channels/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ async def communicator(
application = GraphQLWSConsumer.as_asgi(schema=schema, keep_alive_interval=50)

async with GraphQLWebsocketCommunicator(
protocol=request.param, application=application, path="/graphql"
protocol=request.param,
application=application,
path="/graphql",
connection_params={"strawberry": "Hi"},
) as client:
yield client

Expand All @@ -45,3 +48,8 @@ async def test_graphql_error(communicator):
query='subscription { error(message: "Hi") }'
):
assert res.errors[0].message == "Hi"


async def test_simple_connection_params(communicator):
async for res in communicator.subscribe(query="subscription { connectionParams }"):
assert res.data["connectionParams"] == "Hi"

0 comments on commit 0c923bb

Please sign in to comment.