rich graphql client in python
Project description
greff
I wanted an animal name, but most of them already used in pypi :(, the name pronounced greph
what is it
greff
creates python classes from graphql
response.
what is it not
- doesn't check for typing, you have
pydantic
for it - dataclass, although you can use the
dataclasses
syntax to create classesgreff
is for graphql and it adds graphql attributes behind the sence
current stage
in the current stage its not ready for release, it is only possible (for now) to query data
vision / example
import requests
import greff
@greff.define_type(queryname="authors")
class ParentAuthor(greff.Model):
name: str
age: int = 0
@greff.define_type()
class Author(ParentAuthor):
extra_field: str
@greff.define_type()
class SimpleAuthor(ParentAuthor):
pass
class MyGraphqlClient(greff.HTTPClient):
# we implement the graphql posting function ourself
# since it can very from diffrent implementations
def request(self, query: str) -> dict:
response = requests.post("http://localhost:8000/graphql", json={"query": query}, verify=False)
response.raise_for_status()
return response.json()
# create our charryplate graphql client
graphql = MyGraphqlClient()
# graphql equivelent
# query {
# authors {
# ... frag
# }
# }
# fragment frag on Author {
# name
# }
#
query = graphql.query((
(ParentAuthor, (
ParentAuthor.query.name,
)),
),
)
# graphql response
# {
# "data": {
# "authors": [
# {
# "name": "Michael Crichton",
# "__typename": "Author"
# }
# ]
# }
# }
for author in query:
print(type(author), author.name)
output
<class '__main__.Author'> Michael Crichton
why did it return Author
instance and not ParentAuthor
instance?
if you look closely at the response
{
"data": {
"authors": [
{
"name": "Michael Crichton",
"__typename": "Author"
}
]
}
}
ParentAuthor
is implementor for Author
and SmallAuthor
and in our query the __typename
returned Author
, so greff automatically created the correct python instace
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
greff-0.0.6a0.tar.gz
(6.0 kB
view hashes)