-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathrequests_sync_dsl.py
31 lines (24 loc) · 1.04 KB
/
requests_sync_dsl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from gql import Client
from gql.dsl import DSLQuery, DSLSchema, dsl_gql
from gql.transport.requests import RequestsHTTPTransport
transport = RequestsHTTPTransport(
url="https://countries.trevorblades.com/",
verify=True,
retries=3,
)
client = Client(transport=transport, fetch_schema_from_transport=True)
# Using `with` on the sync client will start a connection on the transport
# and provide a `session` variable to execute queries on this connection.
# Because we requested to fetch the schema from the transport,
# GQL will fetch the schema just after the establishment of the first session
with client as session:
# We should have received the schema now that the session is established
assert client.schema is not None
# Instantiate the root of the DSL Schema as ds
ds = DSLSchema(client.schema)
# Create the query using dynamically generated attributes from ds
query = dsl_gql(
DSLQuery(ds.Query.continents.select(ds.Continent.code, ds.Continent.name))
)
result = session.execute(query)
print(result)