diff --git a/docs/code_examples/httpx_async_trio.py b/docs/code_examples/httpx_async_trio.py new file mode 100644 index 00000000..058b952b --- /dev/null +++ b/docs/code_examples/httpx_async_trio.py @@ -0,0 +1,34 @@ +import trio + +from gql import Client, gql +from gql.transport.httpx import HTTPXAsyncTransport + + +async def main(): + + transport = HTTPXAsyncTransport(url="https://countries.trevorblades.com/graphql") + + # Using `async with` on the client will start a connection on the transport + # and provide a `session` variable to execute queries on this connection + async with Client( + transport=transport, + fetch_schema_from_transport=True, + ) as session: + + # Execute single query + query = gql( + """ + query getContinents { + continents { + code + name + } + } + """ + ) + + result = await session.execute(query) + print(result) + + +trio.run(main) diff --git a/gql/client.py b/gql/client.py index 5c1edffa..a79d4b72 100644 --- a/gql/client.py +++ b/gql/client.py @@ -22,6 +22,7 @@ ) import backoff +from anyio import fail_after from graphql import ( DocumentNode, ExecutionResult, @@ -1532,15 +1533,13 @@ async def _execute( ) # Execute the query with the transport with a timeout - result = await asyncio.wait_for( - self.transport.execute( + with fail_after(self.client.execute_timeout): + result = await self.transport.execute( document, variable_values=variable_values, operation_name=operation_name, **kwargs, - ), - self.client.execute_timeout, - ) + ) # Unserialize the result if requested if self.client.schema: diff --git a/setup.py b/setup.py index eb215b53..773aacc5 100644 --- a/setup.py +++ b/setup.py @@ -6,6 +6,7 @@ "graphql-core>=3.3.0a3,<3.4", "yarl>=1.6,<2.0", "backoff>=1.11.1,<3.0", + "anyio>=3.0,<5", ] console_scripts = [