Skip to content

Commit a97a3da

Browse files
committed
Add async example to README
1 parent 474fdee commit a97a3da

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

README.md

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ pip install "gql[all]"
5757
5858
## Usage
5959

60-
### Basic usage
60+
### Sync usage
6161

6262
```python
63-
from gql import gql, Client
63+
from gql import Client, gql
6464
from gql.transport.aiohttp import AIOHTTPTransport
6565

6666
# Select your transport with a defined url endpoint
6767
transport = AIOHTTPTransport(url="https://countries.trevorblades.com/")
6868

6969
# Create a GraphQL client using the defined transport
70-
client = Client(transport=transport, fetch_schema_from_transport=True)
70+
client = Client(transport=transport)
7171

7272
# Provide a GraphQL query
7373
query = gql(
@@ -97,6 +97,47 @@ $ python basic_example.py
9797
> python environments (as with Jupyter which uses IPython) an asyncio event loop is created for you. In that case you
9898
> should use instead the [async usage example](https://gql.readthedocs.io/en/latest/async/async_usage.html#async-usage).
9999
100+
### Async usage
101+
102+
```python
103+
import asyncio
104+
105+
from gql import Client, gql
106+
from gql.transport.aiohttp import AIOHTTPTransport
107+
108+
109+
async def main():
110+
111+
# Select your transport with a defined url endpoint
112+
transport = AIOHTTPTransport(url="https://countries.trevorblades.com/graphql")
113+
114+
# Create a GraphQL client using the defined transport
115+
client = Client(transport=transport)
116+
117+
# Provide a GraphQL query
118+
query = gql(
119+
"""
120+
query getContinents {
121+
continents {
122+
code
123+
name
124+
}
125+
}
126+
"""
127+
)
128+
129+
# Using `async with` on the client will start a connection on the transport
130+
# and provide a `session` variable to execute queries on this connection
131+
async with client as session:
132+
133+
# Execute the query
134+
result = await session.execute(query)
135+
print(result)
136+
137+
138+
asyncio.run(main())
139+
```
140+
100141
## Contributing
101142
See [CONTRIBUTING.md](CONTRIBUTING.md)
102143

0 commit comments

Comments
 (0)