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
pydanticfor it - dataclass, although you can use the
dataclassessyntax to create classesgreffis 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file greff-0.0.6a0.tar.gz.
File metadata
- Download URL: greff-0.0.6a0.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.11.6 Linux/6.6.9-arch1-1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9bf1b5020b1929ff6d91d9f9281cdfed2da61b2b0d6fa2135a83ec1913f36337
|
|
| MD5 |
e806a0dde0c8801a37f115d273d407a8
|
|
| BLAKE2b-256 |
bf26561f0de2438566571d9ec73d19585cea508f2675964e894055d564dddf18
|
File details
Details for the file greff-0.0.6a0-py3-none-any.whl.
File metadata
- Download URL: greff-0.0.6a0-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.11.6 Linux/6.6.9-arch1-1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4dfa82cbbddb9b106ef730d4eb807f0964516167cee34b788bbf0851441bc604
|
|
| MD5 |
5150518566a9dd6424d7283f74be0c4f
|
|
| BLAKE2b-256 |
45f6001582256dd4b299d518e8bd173efe5f8b3210d43e7a9f10880397e85c90
|