Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature httpx transport working with trio #455

Merged
merged 4 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading