greff


Namegreff JSON
Version 0.0.6a0 PyPI version JSON
download
home_pagehttps://github.com/dsal3389/greff
Summaryrich graphql client in python
upload_time2024-01-05 21:04:38
maintainer
docs_urlNone
authordaniel sonbolian
requires_python>=3.10,<4.0
licenseMIT
keywords graphql
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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
```py
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
```sh
<class '__main__.Author'> Michael Crichton
```

### why did it return `Author` instance and not `ParentAuthor` instance?

if you look closely at the response
```gql
{
  "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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/dsal3389/greff",
    "name": "greff",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10,<4.0",
    "maintainer_email": "",
    "keywords": "graphql",
    "author": "daniel sonbolian",
    "author_email": "dsal3389@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/bf/26/561f0de2438566571d9ec73d19585cea508f2675964e894055d564dddf18/greff-0.0.6a0.tar.gz",
    "platform": null,
    "description": "# greff\nI wanted an animal name, but most of them already used in pypi :(, the name pronounced `greph`\n\n## what is it\n`greff` creates python classes from `graphql` response.\n\n## what is it not\n * doesn't check for typing, you have `pydantic` for it\n * dataclass, although you can use the `dataclasses` syntax to create classes `greff` is for graphql and it adds graphql attributes behind the sence\n\n## current stage\nin the current stage its not ready for release, it is only possible\n(for now) to query data\n\n## vision / example\n```py\nimport requests\nimport greff\n\n\n@greff.define_type(queryname=\"authors\")\nclass ParentAuthor(greff.Model):\n    name: str\n    age: int = 0\n\n\n@greff.define_type()\nclass Author(ParentAuthor):\n    extra_field: str\n\n\n@greff.define_type()\nclass SimpleAuthor(ParentAuthor):\n    pass\n\n\nclass MyGraphqlClient(greff.HTTPClient):\n\n  # we implement the graphql posting function ourself\n  # since it can very from diffrent implementations\n  def request(self, query: str) -> dict:\n      response = requests.post(\"http://localhost:8000/graphql\", json={\"query\": query}, verify=False)\n      response.raise_for_status()\n      return response.json()\n\n\n# create our charryplate graphql client\ngraphql = MyGraphqlClient()\n\n# graphql equivelent\n# query {\n#   authors {\n#     ... frag\n#   }\n# }\n# fragment frag on Author {\n#   name\n# }\n#\nquery = graphql.query((\n        (ParentAuthor, (\n            ParentAuthor.query.name,\n        )),\n    ),\n)\n\n# graphql response\n# {\n#   \"data\": {\n#     \"authors\": [\n#       {\n#         \"name\": \"Michael Crichton\",\n#         \"__typename\": \"Author\"\n#       }\n#     ]\n#   }\n# }\nfor author in query:\n    print(type(author), author.name)\n```\n\noutput\n```sh\n<class '__main__.Author'> Michael Crichton\n```\n\n### why did it return `Author` instance and not `ParentAuthor` instance?\n\nif you look closely at the response\n```gql\n{\n  \"data\": {\n    \"authors\": [\n      {\n        \"name\": \"Michael Crichton\",\n        \"__typename\": \"Author\"\n      }\n    ]\n  }\n}\n```\n\n`ParentAuthor` is implementor for `Author` and `SmallAuthor` and in our query the `__typename`\nreturned `Author`, so greff automatically created the correct python instace\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "rich graphql client in python",
    "version": "0.0.6a0",
    "project_urls": {
        "Homepage": "https://github.com/dsal3389/greff",
        "Repository": "https://github.com/dsal3389/greff"
    },
    "split_keywords": [
        "graphql"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "45f6001582256dd4b299d518e8bd173efe5f8b3210d43e7a9f10880397e85c90",
                "md5": "5150518566a9dd6424d7283f74be0c4f",
                "sha256": "4dfa82cbbddb9b106ef730d4eb807f0964516167cee34b788bbf0851441bc604"
            },
            "downloads": -1,
            "filename": "greff-0.0.6a0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5150518566a9dd6424d7283f74be0c4f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10,<4.0",
            "size": 7412,
            "upload_time": "2024-01-05T21:04:37",
            "upload_time_iso_8601": "2024-01-05T21:04:37.427974Z",
            "url": "https://files.pythonhosted.org/packages/45/f6/001582256dd4b299d518e8bd173efe5f8b3210d43e7a9f10880397e85c90/greff-0.0.6a0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bf26561f0de2438566571d9ec73d19585cea508f2675964e894055d564dddf18",
                "md5": "e806a0dde0c8801a37f115d273d407a8",
                "sha256": "9bf1b5020b1929ff6d91d9f9281cdfed2da61b2b0d6fa2135a83ec1913f36337"
            },
            "downloads": -1,
            "filename": "greff-0.0.6a0.tar.gz",
            "has_sig": false,
            "md5_digest": "e806a0dde0c8801a37f115d273d407a8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10,<4.0",
            "size": 6048,
            "upload_time": "2024-01-05T21:04:38",
            "upload_time_iso_8601": "2024-01-05T21:04:38.663469Z",
            "url": "https://files.pythonhosted.org/packages/bf/26/561f0de2438566571d9ec73d19585cea508f2675964e894055d564dddf18/greff-0.0.6a0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-05 21:04:38",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "dsal3389",
    "github_project": "greff",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "greff"
}
        
Elapsed time: 2.26298s