Skip to main content

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 classes greff 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


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.5a0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

greff-0.0.5a0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file greff-0.0.5a0.tar.gz.

File metadata

  • Download URL: greff-0.0.5a0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.6 Linux/6.6.7-arch1-1

File hashes

Hashes for greff-0.0.5a0.tar.gz
Algorithm Hash digest
SHA256 85bf35f7fdf44083766e39aa8f8bc317b3e7125886604958395364bfeba09b63
MD5 289c3121ce71a1bac625c356e7ad1fe7
BLAKE2b-256 4d081ba8f2839af040ada1b2b13954d3da62dbf88e828eeab1c6afbb938d7403

See more details on using hashes here.

File details

Details for the file greff-0.0.5a0-py3-none-any.whl.

File metadata

  • Download URL: greff-0.0.5a0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.6 Linux/6.6.7-arch1-1

File hashes

Hashes for greff-0.0.5a0-py3-none-any.whl
Algorithm Hash digest
SHA256 d6e4800e8904b8d9fd08a4522c1b5f6e4de13937240f649b05113e5a7aff097e
MD5 4dcd063b1c95922b5a56fe11df005cbb
BLAKE2b-256 ab1857d5bd3f84089813523a458c7a3989bcc97ac19b1ed81fb239ff15eacf80

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page