Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add on_connect/on_disconnect hooks to ASGI app, support connection context #488

Merged
merged 4 commits into from
Mar 22, 2021

Conversation

rafalp
Copy link
Contributor

@rafalp rafalp commented Mar 12, 2021

Adds on_connect and on_disconnect options to ASGI GraphQL app and introduces connection context.

on_connect

on_connect is optional callable that is called when GraphQL client opens WebSocket connection with the server. It's called with two arguments: WebSocket instance and Any value with connection payload. It can be used to extract supported data from connection's payload to the current scope:

def on_connect(ws: WebSocket, payload: Any):
    if not isinstance(payload, dict):
        return

    ws.scope["auth"] = get_user_from_token(payload.get("auth_token"))


def get_context_value(request):
    if request.scope.type == "websocket":
        return {"auth": request.scope["auth"]}
    
    return {"auth": get_user_from_token(request.headers.get("authorization")}

As bonus, users may raise new WebSocketConnectionError from on_connect to get nice error masse sent to client:

from ariadne.asgi import WebSocketConnectionError


def on_connect(ws: WebSocket, payload: Any):
    if not isinstance(payload, dict):
        raise WebSocketConnectionError({"message": "payload object is required", "code": "payload.value_error"})

    ws.scope["auth"] = get_user_from_token(payload.get("auth_token"))

on_disconnect

on_disconnect is optional callable that is called when GraphQL client closes websocket connection. Receives one argument: WebSocket instance.

def on_disconnect(ws):
    # get some data from WS scope and maybe log it somewhere or set user from `ws.scope` to offline?

Supersedes #406, #403 and #355.

@rafalp rafalp self-assigned this Mar 12, 2021
@rafalp rafalp added enhancement New feature or request roadmap Feature that we want to have included labels Mar 12, 2021
@rafalp rafalp added this to the Ariadne 0.14 milestone Mar 12, 2021
@lgtm-com
Copy link

lgtm-com bot commented Mar 12, 2021

This pull request introduces 1 alert when merging 2c0a2de into a732a7c - view on LGTM.com

new alerts:

  • 1 for Unused import

@rafalp rafalp force-pushed the feature/subscription-connections branch from 2c0a2de to 27d6071 Compare March 19, 2021 11:28
@codecov
Copy link

codecov bot commented Mar 19, 2021

Codecov Report

Merging #488 (1612d11) into master (903d217) will increase coverage by 0.02%.
The diff coverage is 99.24%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #488      +/-   ##
==========================================
+ Coverage   98.16%   98.19%   +0.02%     
==========================================
  Files         104      104              
  Lines        5125     5253     +128     
==========================================
+ Hits         5031     5158     +127     
- Misses         94       95       +1     
Impacted Files Coverage Δ
ariadne/asgi.py 95.49% <97.36%> (+0.28%) ⬆️
tests/asgi/test_websockets.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 903d217...1612d11. Read the comment docs.

@lgtm-com
Copy link

lgtm-com bot commented Mar 19, 2021

This pull request introduces 1 alert when merging 27d6071 into 903d217 - view on LGTM.com

new alerts:

  • 1 for Unused import

@rafalp rafalp requested review from bogdal and salwator March 19, 2021 17:34
@rafalp rafalp marked this pull request as ready for review March 19, 2021 17:44
@rafalp rafalp merged commit 04ed6d1 into master Mar 22, 2021
@rafalp rafalp deleted the feature/subscription-connections branch March 22, 2021 11:10
@ammurdoch
Copy link

This is great! When can y'all get this released?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request roadmap Feature that we want to have included
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants