GraphQLWebsocketCommunicator

A test communicator for GraphQL over Websockets.

import pytest
from strawberry.channels.testing import GraphQLWebsocketCommunicator
from myapp.asgi import application
@pytest.fixture
async def gql_communicator():
async with GraphQLWebsocketCommunicator(application, path="/graphql") as client:
yield client
async def test_subscribe_echo(gql_communicator):
async for res in gql_communicator.subscribe(
query='subscription { echo(message: "Hi") }'
):
assert res.data == {"echo": "Hi"}

Constructor:

Create a new communicator.

Signature:

def __init__(
self,
application: ASGIApplication,
path: str,
headers: List[Tuple[bytes, bytes]] | None = None,
protocol: str = GRAPHQL_TRANSPORT_WS_PROTOCOL,
connection_params: dict = ExprDict,
kwargs: Any = {},
) -> None:
...

Parameters:

  1. application:

    Your asgi application that encapsulates the strawberry schema.

    Type
    ASGIApplication
  2. path:

    the url endpoint for the schema.

    Type
    str
  3. headers:

    a list of tuples to be sent as headers to the server.

    Type
    List[Tuple[bytes, bytes]] | None
    Default
    None
  4. protocol:

    currently this supports graphql-transport-ws only.

    Type
    str
    Default
    GRAPHQL_TRANSPORT_WS_PROTOCOL
  5. connection_params:

    a dictionary of connection parameters to send to the server.

    Type
    dict
    Default
    ExprDict
  6. kwargs:

    Type
    Any
    Default
    {}

Methods:

Attributes:

  1. protocol:

  2. connection_params: