-
-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a546dfc
commit c6abc42
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
Release type: minor | ||
|
||
This release adds a new `on_ws_connect` method to all HTTP view integrations. | ||
The method is called when a `graphql-transport-ws` or `graphql-ws` connection is | ||
established and can be used to customize the connection acknowledgment behavior. | ||
|
||
This is particularly useful for authentication, authorization, and sending a | ||
custom acknowledgment payload to clients when a connection is accepted. For | ||
example: | ||
|
||
```python | ||
class MyGraphQLView(GraphQLView): | ||
async def on_ws_connect(self, context: Dict[str, object]): | ||
connection_params = context["connection_params"] | ||
|
||
if not isinstance(connection_params, dict): | ||
# Reject without a custom graphql-ws error payload | ||
raise ConnectionRejectionError() | ||
|
||
if connection_params.get("password") != "secret: | ||
# Reject with a custom graphql-ws error payload | ||
raise ConnectionRejectionError({"reason": "Invalid password"}) | ||
|
||
if username := connection_params.get("username"): | ||
# Accept with a custom acknowledgement payload | ||
return {"message": f"Hello, {username}!"} | ||
|
||
# Accept without a acknowledgement payload | ||
return await super().on_ws_connect(context) | ||
``` | ||
|
||
Take a look at our documentation to learn more. |