Establish a GraphQL client connection to the engine.
| 64 | |
| 65 | |
| 66 | class ClientSession(ResourceManager): |
| 67 | """Establish a GraphQL client connection to the engine.""" |
| 68 | |
| 69 | def __init__(self, conn: ConnectParams, cfg: ConnectConfig | None = None): |
| 70 | super().__init__() |
| 71 | |
| 72 | if cfg is None: |
| 73 | cfg = ConnectConfig() |
| 74 | |
| 75 | transport = HTTPXAsyncTransport( |
| 76 | conn.url, |
| 77 | transport=TelemetryTransport(), |
| 78 | timeout=cfg.timeout, |
| 79 | auth=(conn.session_token, ""), |
| 80 | ) |
| 81 | |
| 82 | client = GraphQLClient( |
| 83 | transport=transport, |
| 84 | # Fetch the schema for DSL query building, but don't |
| 85 | # validate queries client-side. The server validates with |
| 86 | # a corrected PossibleFragmentSpreads rule that handles |
| 87 | # interface-implements-interface; the graphql-core library |
| 88 | # used here does not, causing false rejections for |
| 89 | # `... on SomeIface` inside `node(id:)` when the interface |
| 90 | # has no concrete implementors in this schema view. |
| 91 | fetch_schema_from_transport=True, |
| 92 | # We're using the timeout from the httpx transport. |
| 93 | execute_timeout=None, |
| 94 | ) |
| 95 | # Disable client-side query validation. See comment above. |
| 96 | client.validate = lambda _request: None # type: ignore[method-assign] |
| 97 | |
| 98 | self.client = retrying_client(client, cfg.retry) if cfg.retry else client |
| 99 | self._session: AsyncClientSession | None = None |
| 100 | |
| 101 | async def __aenter__(self) -> Self: |
| 102 | await self.start() |
| 103 | return self |
| 104 | |
| 105 | async def start(self) -> AsyncClientSession: |
| 106 | if self._session: |
| 107 | return self._session |
| 108 | |
| 109 | async with self.get_stack() as stack: |
| 110 | logger.debug("Establishing client session to GraphQL server") |
| 111 | |
| 112 | try: |
| 113 | session = await stack.enter_async_context(self.client) |
| 114 | except TransportConnectionFailed as e: |
| 115 | raise ClientConnectionError(str(e)) from e |
| 116 | except (TransportProtocolError, TransportServerError) as e: |
| 117 | msg = f"Got unexpected response from engine: {e}" |
| 118 | raise ClientConnectionError(msg) from e |
| 119 | except TransportQueryError as e: |
| 120 | # Only query during connection is the introspection query |
| 121 | # for building the schema. |
| 122 | msg = str(e) |
| 123 | # Extract only the error message. |