Skip to content

A minimal library to interact with GraphQL from Python

License

Notifications You must be signed in to change notification settings

labteral/easygraphql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A minimal library to interact with GraphQL from Python

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published