|
| 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 | +``` |
0 commit comments