GraphQLWSConsumer
A channels websocket consumer for GraphQL.
This handles the connections, then hands off to the appropriate handler based on the subprotocol.
To use this, place it in your ProtocolTypeRouter for your channels project, e.g:
from strawberry.channels import GraphQLHttpRouterfrom channels.routing import ProtocolTypeRouterfrom django.core.asgi import get_asgi_application
application = ProtocolTypeRouter({ "http": URLRouter([ re_path("^graphql", GraphQLHTTPRouter(schema=schema)), re_path("^", get_asgi_application()), ]), "websocket": URLRouter([ re_path("^ws/graphql", GraphQLWebSocketRouter(schema=schema)), ]),})
Constructor:
Signature:
def __init__( self, schema: BaseSchema, keep_alive: bool = False, keep_alive_interval: float = 1, debug: bool = False, subscription_protocols: Tuple[str, str] = (GRAPHQL_TRANSPORT_WS_PROTOCOL, GRAPHQL_WS_PROTOCOL), connection_init_wait_timeout: datetime.timedelta | None = None,) -> None: ...
Parameters:
-
schema:
- Type
-
BaseSchema
-
keep_alive:
- Type
-
bool
- Default
-
False
-
keep_alive_interval:
- Type
-
float
- Default
-
1
-
debug:
- Type
-
bool
- Default
-
False
-
subscription_protocols:
- Type
-
Tuple[str, str]
- Default
-
(GRAPHQL_TRANSPORT_WS_PROTOCOL, GRAPHQL_WS_PROTOCOL)
-
connection_init_wait_timeout:
- Type
-
datetime.timedelta | None
- Default
-
None
Methods:
-
pick_preferred_protocol
Signature:
def pick_preferred_protocol(self, accepted_subprotocols: Sequence[str]) -> str | None:...Parameters:
-
accepted_subprotocols:
- Type
-
Sequence[str]
-
-
connect
Signature:
def connect(self) -> None:... -
receive
Signature:
def receive(self, args: str = (), kwargs: Any = {}) -> None:...Parameters:
-
args:
- Type
-
str
- Default
-
()
-
kwargs:
- Type
-
Any
- Default
-
{}
-
-
receive_json
Signature:
def receive_json(self, content: Any, kwargs: Any = {}) -> None:...Parameters:
-
content:
- Type
-
Any
-
kwargs:
- Type
-
Any
- Default
-
{}
-
-
disconnect
Signature:
def disconnect(self, code: int) -> None:...Parameters:
-
code:
- Type
-
int
-
-
get_root_value
Signature:
def get_root_value(self, request: ChannelsConsumer) -> RootValue | None:...Parameters:
-
request:
- Type
-
ChannelsConsumer
-
-
get_context
Signature:
def get_context(self, request: ChannelsConsumer, connection_params: Any) -> Context:...Parameters:
-
request:
- Type
-
ChannelsConsumer
-
connection_params:
- Type
-
Any
-
Attributes:
-
graphql_transport_ws_handler_class:
-
graphql_ws_handler_class:
-
connection_init_wait_timeout:
-
schema:
-
keep_alive:
-
keep_alive_interval:
-
debug:
-
protocols: