Skip to content

Commit 528636a

Browse files
authored
Feature httpx transport working with trio (#455)
1 parent a2f327f commit 528636a

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import trio
2+
3+
from gql import Client, gql
4+
from gql.transport.httpx import HTTPXAsyncTransport
5+
6+
7+
async def main():
8+
9+
transport = HTTPXAsyncTransport(url="https://countries.trevorblades.com/graphql")
10+
11+
# Using `async with` on the client will start a connection on the transport
12+
# and provide a `session` variable to execute queries on this connection
13+
async with Client(
14+
transport=transport,
15+
fetch_schema_from_transport=True,
16+
) as session:
17+
18+
# Execute single query
19+
query = gql(
20+
"""
21+
query getContinents {
22+
continents {
23+
code
24+
name
25+
}
26+
}
27+
"""
28+
)
29+
30+
result = await session.execute(query)
31+
print(result)
32+
33+
34+
trio.run(main)

gql/client.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
)
2323

2424
import backoff
25+
from anyio import fail_after
2526
from graphql import (
2627
DocumentNode,
2728
ExecutionResult,
@@ -1532,15 +1533,13 @@ async def _execute(
15321533
)
15331534

15341535
# Execute the query with the transport with a timeout
1535-
result = await asyncio.wait_for(
1536-
self.transport.execute(
1536+
with fail_after(self.client.execute_timeout):
1537+
result = await self.transport.execute(
15371538
document,
15381539
variable_values=variable_values,
15391540
operation_name=operation_name,
15401541
**kwargs,
1541-
),
1542-
self.client.execute_timeout,
1543-
)
1542+
)
15441543

15451544
# Unserialize the result if requested
15461545
if self.client.schema:

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"graphql-core>=3.3.0a3,<3.4",
77
"yarl>=1.6,<2.0",
88
"backoff>=1.11.1,<3.0",
9+
"anyio>=3.0,<5",
910
]
1011

1112
console_scripts = [

0 commit comments

Comments
 (0)