-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql_example.py
51 lines (40 loc) · 1.31 KB
/
graphql_example.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Libraries required
import os
from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport
from src.coffees import IcedCoffee
# Get API key from environment variable, if necessary
# API_KEY = os.environ["API_KEY"]
# Set up API URL and GraphQL query
url = f"https://api.sampleapis.com/coffee/graphql"
query = gql(
"""
query getIcedCoffees {
allIceds(perPage: 10) {
title
id
description
ingredients
image
}
}
"""
)
# Select your transport with a defined url endpoint
transport = AIOHTTPTransport(url=url)
# Create a GraphQL client using the defined transport
client = Client(transport=transport, fetch_schema_from_transport=True)
# Execute the query on the transport
result = client.execute(query)
# Cycle through the response
for coffee in result["allIceds"]:
print(f"{coffee['title']}: {coffee['description']}")
print(f"You make this with: {coffee['ingredients']}\n")
# Even better, use a dataclass we setup specifically for
# this data we imported from the src/coffees.py file.
# Let's use a list comprehension to construct a list of coffees
iced_coffees = [IcedCoffee(**coffee) for coffee in result["allIceds"]]
# Cycle through the response
for coffee in iced_coffees:
print(f"{coffee.title}: {coffee.description}")
print(f"You make this with: {coffee.ingredients}\n")