A minimal library to interact with GraphQL from Python
pip install easygraphql
from easygraphql import GraphQL
graphql = GraphQL('https://example.org/graphql')
query = '''
query {
hello
}
'''
data, errors = graphql.execute(query)
You can set global headers that will be added to all your GraphQL operations:
graphql.set_headers({'Authorization': 'Bearer xxxxx'})
You can also unset them:
graphql.unset_headers()
Or directly provide them on every execution:
data, errors = graphql.execute(query, headers={'Authorization': 'Bearer xxxxx'})
from easygraphql import GraphQL
graphql = GraphQL('https://swapi-graphql.netlify.com/.netlify/functions/index')
query = '''
query {
allFilms {
films {
title
director
}
}
}
'''
data, errors = graphql.execute(query)
{
"allFilms": {
"films": [
{
"title": "A New Hope",
"director": "George Lucas"
},
{
"title": "The Empire Strikes Back",
"director": "Irvin Kershner"
},
{
"title": "Return of the Jedi",
"director": "Richard Marquand"
},
{
"title": "The Phantom Menace",
"director": "George Lucas"
},
{
"title": "Attack of the Clones",
"director": "George Lucas"
},
{
"title": "Revenge of the Sith",
"director": "George Lucas"
},
{
"title": "The Force Awakens",
"director": "J. J. Abrams"
}
]
}
}
from easygraphql import GraphQL
graphql = GraphQL('https://swapi-graphql.netlify.com/.netlify/functions/index')
query = '''
query GetFilm($id: ID!){
film(id: $id) {
title
director
}
}
'''
variables = {"id": "ZmlsbXM6MQ=="}
data, errors = graphql.execute(query, variables)
{
"film": {
"title": "A New Hope",
"director": "George Lucas"
}
}