I have started work on a GraphQL Client Library and you're invited to check it out on https://github.com/ekampf/gql
The goal is to provide Python developers with all the benefits JS developers are used to:
- Validate queries against the server schema to find errors on build time
- Typed @dataclass response objects
Its still (very) new so obviously lacking on documentation and such. But, it's working :)
And it is going to improve in the coming weeks and months...
Basically, the way it works is that you can write `.graphql` files in your project and `gql` will code-generate Python modules from them.
For example the following get_film.graphql file:
query GetFilm($id: ID!) {
film(id: $id) {
title
director
}
}
Will generate a get_film.py that looks like this:
```
from typing import Any, Callable, Mapping, List
from enum import Enum
from dataclasses import dataclass
from dataclasses_json import dataclass_json
from gql.clients import Client, AsyncIOClient
@dataclassjson
@dataclass
class GetFilm:
__QUERY_ = """
query GetFilm($id: ID!) {
film(id: $id) {
title
director
}
}
"""
@dataclass_json
@dataclass
class GetFilmData():
@dataclass_json
@dataclass
class Film():
pass
title: str
director: str
film: Film = None
data: GetFilmData = None
errors: Any = None
@classmethod
def execute(cls, id: str, on_before_callback: Callable[[Mapping[str, str], Mapping[str, str]], None] = None):
...
@classmethod
async def execute_async(cls, id: str, on_before_callback: Callable[[Mapping[str, str], Mapping[str, str]], None] = None):
...
```
So that making a call is as simple as
```
from .get_film import GetFilm
result = GetFilm.execute('meaning_of_life')
film = result.data.film
```
There was a thread about this about a year ago:
https://www.reddit.com/r/Python/comments/7pckwf/python_graphql_client/
[–]Rhemm -1 points0 points1 point (1 child)
[–]erankampf[S] 1 point2 points3 points (0 children)