From 6ee45e9cb06f60f9c1c7ee137583d9515a963e6c Mon Sep 17 00:00:00 2001 From: Leszek Hanusz Date: Fri, 15 Dec 2023 14:32:02 +0100 Subject: [PATCH 1/3] Adding httpx code example using trio --- docs/code_examples/httpx_async_trio.py | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 docs/code_examples/httpx_async_trio.py 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) From d04750eada861c58f4e482cc3fe5fe6bc408538a Mon Sep 17 00:00:00 2001 From: Leszek Hanusz Date: Fri, 15 Dec 2023 14:32:36 +0100 Subject: [PATCH 2/3] Using anyio for timeout to allow httpx transport to work with trio --- gql/client.py | 9 ++++----- setup.py | 1 + 2 files changed, 5 insertions(+), 5 deletions(-) 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..1957bc9a 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>=4.0,<5", ] console_scripts = [ From 51b14fc1d1730db46972a63f0e8524c958e51846 Mon Sep 17 00:00:00 2001 From: Leszek Hanusz Date: Fri, 15 Dec 2023 14:43:44 +0100 Subject: [PATCH 3/3] Allow anyio versions 3.x for Python version 3.7 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 1957bc9a..773aacc5 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ "graphql-core>=3.3.0a3,<3.4", "yarl>=1.6,<2.0", "backoff>=1.11.1,<3.0", - "anyio>=4.0,<5", + "anyio>=3.0,<5", ] console_scripts = [