gql.transport.websockets_base

class gql.transport.websockets_base.ListenerQueue(query_id: int, send_stop: bool)

Bases: object

Special queue used for each query waiting for server answers

If the server is stopped while the listener is still waiting, Then we send an exception to the queue and this exception will be raised to the consumer once all the previous messages have been consumed from the queue

__init__(query_id: int, send_stop: bool) None
async get() Tuple[str, Optional[ExecutionResult]]
async put(item: Tuple[str, Optional[ExecutionResult]]) None
async set_exception(exception: Exception) None
class gql.transport.websockets_base.WebsocketsTransportBase(url: str, headers: Optional[Union[Headers, Mapping[str, str], Iterable[Tuple[str, str]], SupportsKeysAndGetItem]] = None, ssl: Union[SSLContext, bool] = False, init_payload: Dict[str, Any] = {}, connect_timeout: Optional[Union[int, float]] = 10, close_timeout: Optional[Union[int, float]] = 10, ack_timeout: Optional[Union[int, float]] = 10, keep_alive_timeout: Optional[Union[int, float]] = None, connect_args: Dict[str, Any] = {})

Bases: AsyncTransport

abstract Async Transport used to implement different websockets protocols.

This transport uses asyncio and the websockets library in order to send requests on a websocket connection.

__init__(url: str, headers: Optional[Union[Headers, Mapping[str, str], Iterable[Tuple[str, str]], SupportsKeysAndGetItem]] = None, ssl: Union[SSLContext, bool] = False, init_payload: Dict[str, Any] = {}, connect_timeout: Optional[Union[int, float]] = 10, close_timeout: Optional[Union[int, float]] = 10, ack_timeout: Optional[Union[int, float]] = 10, keep_alive_timeout: Optional[Union[int, float]] = None, connect_args: Dict[str, Any] = {}) None

Initialize the transport with the given parameters.

Parameters
  • url – The GraphQL server URL. Example: ‘wss://server.com:PORT/graphql’.

  • headers – Dict of HTTP Headers.

  • ssl – ssl_context of the connection. Use ssl=False to disable encryption

  • init_payload – Dict of the payload sent in the connection_init message.

  • connect_timeout – Timeout in seconds for the establishment of the websocket connection. If None is provided this will wait forever.

  • close_timeout – Timeout in seconds for the close. If None is provided this will wait forever.

  • ack_timeout – Timeout in seconds to wait for the connection_ack message from the server. If None is provided this will wait forever.

  • keep_alive_timeout – Optional Timeout in seconds to receive a sign of liveness from the server.

  • connect_args – Other parameters forwarded to websockets.connect

payloads: Dict[str, Any]

payloads is a dict which will contain the payloads received for example with the graphql-ws protocol: ‘ping’, ‘pong’, ‘connection_ack’

async subscribe(document: DocumentNode, variable_values: Optional[Dict[str, Any]] = None, operation_name: Optional[str] = None, send_stop: Optional[bool] = True) AsyncGenerator[ExecutionResult, None]

Send a query and receive the results using a python async generator.

The query can be a graphql query, mutation or subscription.

The results are sent as an ExecutionResult object.

async execute(document: DocumentNode, variable_values: Optional[Dict[str, Any]] = None, operation_name: Optional[str] = None) ExecutionResult

Execute the provided document AST against the configured remote server using the current session.

Send a query but close the async generator as soon as we have the first answer.

The result is sent as an ExecutionResult object.

async connect() None

Coroutine which will:

  • connect to the websocket address

  • send the init message

  • wait for the connection acknowledge from the server

  • create an asyncio task which will be used to receive and parse the websocket answers

Should be cleaned with a call to the close coroutine

async close() None

Coroutine used to Close an established connection

async wait_closed() None