https://github.com/labteral/easygraphql
A minimal library to interact with GraphQL from Python
https://github.com/labteral/easygraphql
auth graphql headers python
Last synced: 8 months ago
JSON representation
A minimal library to interact with GraphQL from Python
- Host: GitHub
- URL: https://github.com/labteral/easygraphql
- Owner: labteral
- License: gpl-3.0
- Created: 2020-01-04T13:01:48.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-11-29T13:56:51.000Z (over 2 years ago)
- Last Synced: 2024-08-08T16:12:44.197Z (over 1 year ago)
- Topics: auth, graphql, headers, python
- Language: Python
- Homepage:
- Size: 19.5 KB
- Stars: 5
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# easygraphql
A minimal library to interact with GraphQL from Python
## Installation
```bash
pip install easygraphql
```
## Usage
### Instantiation
```python
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:
```python
graphql.set_headers({'Authorization': 'Bearer xxxxx'})
```
You can also unset them:
```python
graphql.unset_headers()
```
Or directly provide them on every execution:
```python
data, errors = graphql.execute(query, headers={'Authorization': 'Bearer xxxxx'})
```
## Examples
### Query without variables
```python
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)
```
```json
{
"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
```python
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)
```
```json
{
"film": {
"title": "A New Hope",
"director": "George Lucas"
}
}
```