GraphQL Clients in Python
The simplest approach uses requests - GraphQL is just HTTP with JSON:
import requests
query = """
query {
user(id: 1) {
name
email
}
}
"""
response = requests.post(
"https://api.example.com/graphql",
json={"query": query}
)
data = response.json()
print(data["data"]["user"]["name"])
For more features, use a dedicated client like gql:
from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport
transport = RequestsHTTPTransport(url="https://api.example.com/graphql")
client = Client(transport=transport)
query = gql("{ user(id: 1) { name } }")
result = client.execute(query)
The gql library adds schema validation, better error handling, and async support.
I cover Python GraphQL clients in my GraphQL with Python course.