gql.transport.aiohttp_websockets

class gql.transport.aiohttp_websockets.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, ExecutionResult | None]
async put(item: Tuple[str, ExecutionResult | None]) None
async set_exception(exception: Exception) None
class gql.transport.aiohttp_websockets.AIOHTTPWebsocketsTransport(url: str | URL, *, subprotocols: Collection[str] | None = None, heartbeat: float | None = None, auth: BasicAuth | None = None, origin: str | None = None, params: Mapping[str, str] | None = None, headers: Mapping[str, str] | Mapping[istr, str] | CIMultiDict | CIMultiDictProxy | Iterable[Tuple[str | istr, str]] | None = None, proxy: str | URL | None = None, proxy_auth: BasicAuth | None = None, proxy_headers: Mapping[str, str] | Mapping[istr, str] | CIMultiDict | CIMultiDictProxy | Iterable[Tuple[str | istr, str]] | None = None, ssl: SSLContext | Literal[False] | Fingerprint | None = None, websocket_close_timeout: float = 10.0, receive_timeout: float | None = None, ssl_close_timeout: int | float | None = 10, connect_timeout: int | float | None = 10, close_timeout: int | float | None = 10, ack_timeout: int | float | None = 10, keep_alive_timeout: int | float | None = None, init_payload: Dict[str, Any] = {}, ping_interval: int | float | None = None, pong_timeout: int | float | None = None, answer_pings: bool = True, client_session_args: Dict[str, Any] | None = None, connect_args: Dict[str, Any] = {})

Bases: AsyncTransport

APOLLO_SUBPROTOCOL: str = 'graphql-ws'
GRAPHQLWS_SUBPROTOCOL: str = 'graphql-transport-ws'
__init__(url: str | URL, *, subprotocols: Collection[str] | None = None, heartbeat: float | None = None, auth: BasicAuth | None = None, origin: str | None = None, params: Mapping[str, str] | None = None, headers: Mapping[str, str] | Mapping[istr, str] | CIMultiDict | CIMultiDictProxy | Iterable[Tuple[str | istr, str]] | None = None, proxy: str | URL | None = None, proxy_auth: BasicAuth | None = None, proxy_headers: Mapping[str, str] | Mapping[istr, str] | CIMultiDict | CIMultiDictProxy | Iterable[Tuple[str | istr, str]] | None = None, ssl: SSLContext | Literal[False] | Fingerprint | None = None, websocket_close_timeout: float = 10.0, receive_timeout: float | None = None, ssl_close_timeout: int | float | None = 10, connect_timeout: int | float | None = 10, close_timeout: int | float | None = 10, ack_timeout: int | float | None = 10, keep_alive_timeout: int | float | None = None, init_payload: Dict[str, Any] = {}, ping_interval: int | float | None = None, pong_timeout: int | float | None = None, answer_pings: bool = True, client_session_args: Dict[str, Any] | None = 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’.

  • subprotocols – list of subprotocols sent to the backend in the ‘subprotocols’ http header. By default: both apollo and graphql-ws subprotocols.

  • heartbeat (float) – Send low level ping message every heartbeat seconds and wait pong response, close connection if pong response is not received. The timer is reset on any data reception.

  • auth – An object that represents HTTP Basic Authorization. BasicAuth (optional)

  • origin (str) – Origin header to send to server(optional)

  • params

    Mapping, iterable of tuple of key/value pairs or string to be sent as parameters in the query string of the new request. Ignored for subsequent redirected requests (optional)

    Allowed values are:

    • collections.abc.Mapping e.g. dict, multidict.MultiDict or multidict.MultiDictProxy

    • collections.abc.Iterable e.g. tuple or list

    • str with preferably url-encoded content (Warning: content will not be encoded by aiohttp)

  • headers – HTTP Headers that sent with every request May be either iterable of key-value pairs or Mapping (e.g. dict, CIMultiDict).

  • proxy – Proxy URL, str or URL (optional)

  • proxy_auth (aiohttp.BasicAuth) – an object that represents proxy HTTP Basic Authorization (optional)

  • ssl – SSL validation mode. True for default SSL check (ssl.create_default_context() is used), False for skip SSL certificate validation, aiohttp.Fingerprint for fingerprint validation, ssl.SSLContext for custom SSL certificate validation.

  • websocket_close_timeout (float) – Timeout for websocket to close. 10 seconds by default

  • receive_timeout (float) – Timeout for websocket to receive complete message. None (unlimited) seconds by default

  • ssl_close_timeout – Timeout in seconds to wait for the ssl connection to close properly

  • 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.

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

  • ping_interval – Delay in seconds between pings sent by the client to the backend for the graphql-ws protocol. None (by default) means that we don’t send pings. Note: there are also pings sent by the underlying websockets protocol. See the keepalive documentation for more information about this.

  • pong_timeout – Delay in seconds to receive a pong from the backend after we sent a ping (only for the graphql-ws protocol). By default equal to half of the ping_interval.

  • answer_pings – Whether the client answers the pings from the backend (for the graphql-ws protocol). By default: True

  • client_session_args – Dict of extra args passed to aiohttp.ClientSession

  • connect_args – Dict of extra args passed to aiohttp.ClientSession.ws_connect

ping_received: Event

ping_received is an asyncio Event which will fire each time a ping is received with the graphql-ws protocol

pong_received: Event

pong_received is an asyncio Event which will fire each time a pong is received with the graphql-ws protocol

async send_ping(payload: Any | None = None) None

Send a ping message for the graphql-ws protocol

async send_pong(payload: Any | None = None) None

Send a pong message for the graphql-ws protocol

async connect() None

Coroutine used to create a connection to the specified address

async close() None

Coroutine used to Close an established connection

async wait_closed() None
async execute(document: DocumentNode, variable_values: Dict[str, Any] | None = None, operation_name: str | None = 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 subscribe(document: DocumentNode, variable_values: Dict[str, Any] | None = None, operation_name: str | None = None, send_stop: bool | None = 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.