easygraphql

A minimal library to interact with GraphQL from Python


Keywords
auth, graphql, headers, python
License
GPL-3.0
Install
pip install easygraphql==0.2311.0

Documentation

easygraphql

A minimal library to interact with GraphQL from Python

Installation

pip install easygraphql

Usage

Instantiation

from easygraphql import GraphQL

graphql = GraphQL('https://example.org/graphql')
query = '''
    query {
        hello
    }
'''
data, errors = graphql.execute(query)

Authentication

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'})

Examples

Query without variables

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"
            }
        ]
    }
}

Query with variables

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"
    }
}