Skip to content

Commit 5860a08

Browse files
committed
First commit
1 parent e624963 commit 5860a08

9 files changed

+233
-0
lines changed

README.md

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# easygraphql
2+
A minimal library to interact with GraphQL from Python
3+
4+
## Installation
5+
```bash
6+
pip install easygraphql
7+
```
8+
9+
## Usage
10+
11+
### Instantiation
12+
```python
13+
from easygraphql import GraphQL
14+
15+
graphql = GraphQL('https://example.org/graphql')
16+
query = '''
17+
query {
18+
hello
19+
}
20+
'''
21+
result = graphql.execute(query)
22+
```
23+
24+
### Authentication
25+
You can set global headers that will be added to all your GraphQL operations:
26+
```python
27+
graphql.set_headers({'Authorization': 'Bearer xxxxx'})
28+
```
29+
30+
You can also unset them:
31+
```python
32+
graphql.unset_headers()
33+
```
34+
35+
Or directly provide them on every execution:
36+
```python
37+
result = graphql.execute(query, headers={'Authorization': 'Bearer xxxxx'})
38+
```
39+
40+
## Examples
41+
42+
### Query without variables
43+
```python
44+
from easygraphql import GraphQL
45+
46+
graphql = GraphQL('https://swapi-graphql.netlify.com/.netlify/functions/index')
47+
query = '''
48+
query {
49+
allFilms {
50+
films {
51+
title
52+
director
53+
}
54+
}
55+
}
56+
'''
57+
result = graphql.execute(query)
58+
```
59+
60+
```json
61+
{
62+
"allFilms": {
63+
"films": [
64+
{
65+
"title": "A New Hope",
66+
"director": "George Lucas"
67+
},
68+
{
69+
"title": "The Empire Strikes Back",
70+
"director": "Irvin Kershner"
71+
},
72+
{
73+
"title": "Return of the Jedi",
74+
"director": "Richard Marquand"
75+
},
76+
{
77+
"title": "The Phantom Menace",
78+
"director": "George Lucas"
79+
},
80+
{
81+
"title": "Attack of the Clones",
82+
"director": "George Lucas"
83+
},
84+
{
85+
"title": "Revenge of the Sith",
86+
"director": "George Lucas"
87+
},
88+
{
89+
"title": "The Force Awakens",
90+
"director": "J. J. Abrams"
91+
}
92+
]
93+
}
94+
}
95+
```
96+
97+
### Query with variables
98+
```python
99+
from easygraphql import GraphQL
100+
101+
graphql = GraphQL('https://swapi-graphql.netlify.com/.netlify/functions/index')
102+
query = '''
103+
query GetFilm($id: ID!){
104+
film(id: $id) {
105+
title
106+
director
107+
}
108+
}
109+
'''
110+
variables = {"id": "ZmlsbXM6MQ=="}
111+
result = graphql.execute(query, variables)
112+
```
113+
114+
```json
115+
{
116+
"film": {
117+
"title": "A New Hope",
118+
"director": "George Lucas"
119+
}
120+
}
121+
```

easygraphql/__init__.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from .easygraphql import *
5+
6+
__version__ = '0.0.1b0'

easygraphql/easygraphql.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import requests
5+
6+
7+
class GraphQL:
8+
def __init__(self, endpoint, headers=None):
9+
self.endpoint = endpoint
10+
self.headers = headers
11+
12+
def set_headers(self, headers):
13+
self.headers = headers
14+
15+
def unset_headers(self):
16+
self.headers = None
17+
18+
def execute(self, operation, variables=None, headers=None):
19+
payload = {'query': operation}
20+
21+
if variables is not None:
22+
payload['variables'] = variables
23+
24+
if headers is None:
25+
headers = self.headers
26+
27+
response = requests.post(self.endpoint, json=payload, headers=headers)
28+
29+
try:
30+
return response.json()['data']
31+
except KeyError:
32+
return response.json()['errors']

examples/query_film_variables.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from easygraphql import GraphQL
5+
import json
6+
7+
graphql = GraphQL('https://swapi-graphql.netlify.com/.netlify/functions/index')
8+
query = '''
9+
query GetFilm($id: ID!){
10+
film(id: $id) {
11+
title
12+
director
13+
}
14+
}
15+
'''
16+
variables = {"id": "ZmlsbXM6MQ=="}
17+
result = graphql.execute(query, variables)
18+
print(json.dumps(result, indent=4))

examples/query_films.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from easygraphql import GraphQL
5+
import json
6+
7+
graphql = GraphQL('https://swapi-graphql.netlify.com/.netlify/functions/index')
8+
query = '''
9+
query {
10+
allFilms {
11+
films {
12+
title
13+
director
14+
}
15+
}
16+
}
17+
'''
18+
result = graphql.execute(query)
19+
print(json.dumps(result, indent=4))

install.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
rm dist/*
3+
python3 setup.py install

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
twine
2+
requests

setup.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from setuptools import setup
5+
from setuptools import find_packages
6+
import easygraphql
7+
8+
setup(
9+
name='easygraphql',
10+
version=easygraphql.__version__,
11+
description='A minimal library to interact with GraphQL from Python',
12+
url='https://github.com/brunneis/easygraphql',
13+
author='Rodrigo Martínez Castaño',
14+
author_email='[email protected]',
15+
license='GNU General Public License v3 (GPLv3)',
16+
packages=find_packages(),
17+
zip_safe=False,
18+
classifiers=[
19+
"Development Status :: 4 - Beta",
20+
"Environment :: Console",
21+
"Intended Audience :: Developers",
22+
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
23+
"Operating System :: POSIX :: Linux",
24+
"Programming Language :: Python :: 3.6",
25+
"Programming Language :: Python :: Implementation :: PyPy",
26+
"Topic :: Software Development :: Libraries :: Python Modules",
27+
],
28+
install_requires=['']) # Dependencies

upload.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
python3 setup.py bdist_wheel
3+
twine upload dist/*.whl
4+

0 commit comments

Comments
 (0)