Skip to content

Commit

Permalink
Feature httpx transport working with trio (#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
leszekhanusz authored Dec 15, 2023
1 parent a2f327f commit 528636a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
34 changes: 34 additions & 0 deletions docs/code_examples/httpx_async_trio.py
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 4 additions & 5 deletions gql/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)

import backoff
from anyio import fail_after
from graphql import (
DocumentNode,
ExecutionResult,
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit 528636a

Please sign in to comment.