# graphql-query
[![tag](https://img.shields.io/github/v/tag/denisart/graphql-query)](https://github.com/denisart/graphql-query)
[![last-commit](https://img.shields.io/github/last-commit/denisart/graphql-query/master)](https://github.com/denisart/graphql-query/commits/master)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/graphql-query)](https://pypi.python.org/pypi/graphql-query)
[![license](https://img.shields.io/github/license/denisart/graphql-query)](https://github.com/denisart/graphql-query/blob/master/LICENSE)
---
**graphql_query** is a complete Domain Specific Language (DSL) for GraphQL query in Python. With this package
you can to
- generate a correct GraphQL query string from a python classes;
- use and share similar Arguments, Variables and e.t.c between different queries;
- easily add new fields to your query;
- add Fragments and Directives to queries;
- generate **graphql_query** classes from pydantic data-model;
The documentation for **graphql_query** can be found at [https://denisart.github.io/graphql-query/](https://denisart.github.io/graphql-query/).
## Quickstart
Install with pip
```bash
pip install graphql_query
```
### Simple query
Code for the simple query
```graphql
{
hero {
name
}
}
```
it is
```python
from graphql_query import Operation, Query
hero = Query(name="hero", fields=["name"])
operation = Operation(type="query", queries=[hero])
print(operation.render())
"""
query {
hero {
name
}
}
"""
```
The `render` method for the `graphql_query.Operation` object
just returns the final string with a query. Inside the `fields` array of the `graphql_query.Query` object
you can use
- `str` (a field name);
- object of `graphql_query.Field` type;
- `graphql_query.Fragment` and `graphql_query.InlineFragment`.
### Arguments, Variables and Directives
For generation of the following query
```graphql
query Hero($episode: Episode, $withFriends: Boolean!) {
hero(episode: $episode) {
name
friends @include(if: $withFriends) {
name
}
}
}
```
we can use the following python code
```python
from graphql_query import Argument, Directive, Field, Operation, Query, Variable
episode = Variable(name="episode", type="Episode")
withFriends = Variable(name="withFriends", type="Boolean!")
arg_episode = Argument(name="episode", value=episode)
arg_if = Argument(name="if", value=withFriends)
hero = Query(
name="hero",
arguments=[arg_episode],
fields=[
"name",
Field(
name="friends",
fields=["name"],
directives=[Directive(name="include", arguments=[arg_if])]
)
]
)
operation = Operation(
type="query",
name="Hero",
variables=[episode, withFriends],
queries=[hero]
)
print(operation.render())
"""
query Hero(
$episode: Episode
$withFriends: Boolean!
) {
hero(
episode: $episode
) {
name
friends @include(
if: $withFriends
) {
name
}
}
}
"""
```
You can find other examples in the documentation.
Raw data
{
"_id": null,
"home_page": null,
"name": "graphql-query",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "code-generation, codegen, dsl, graphql, graphql-query, graphql-query-builder, pydantic, python, query-builder, query-generation, query-generator",
"author": null,
"author_email": "\"Denis A. Artyushin\" <artyushinden@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/8b/64/377beef6c10b798f2ece54cfd3577db20102176c3a155469b92b4a3e3881/graphql_query-1.4.0.tar.gz",
"platform": null,
"description": "# graphql-query\n\n[![tag](https://img.shields.io/github/v/tag/denisart/graphql-query)](https://github.com/denisart/graphql-query)\n[![last-commit](https://img.shields.io/github/last-commit/denisart/graphql-query/master)](https://github.com/denisart/graphql-query/commits/master)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/graphql-query)](https://pypi.python.org/pypi/graphql-query)\n[![license](https://img.shields.io/github/license/denisart/graphql-query)](https://github.com/denisart/graphql-query/blob/master/LICENSE)\n\n---\n\n**graphql_query** is a complete Domain Specific Language (DSL) for GraphQL query in Python. With this package\nyou can to\n\n- generate a correct GraphQL query string from a python classes;\n- use and share similar Arguments, Variables and e.t.c between different queries;\n- easily add new fields to your query;\n- add Fragments and Directives to queries;\n- generate **graphql_query** classes from pydantic data-model;\n\nThe documentation for **graphql_query** can be found at [https://denisart.github.io/graphql-query/](https://denisart.github.io/graphql-query/).\n\n## Quickstart\n\nInstall with pip\n\n```bash\npip install graphql_query\n```\n\n### Simple query\n\nCode for the simple query\n\n```graphql\n{\n hero {\n name\n }\n}\n```\n\nit is\n\n```python\nfrom graphql_query import Operation, Query\n\nhero = Query(name=\"hero\", fields=[\"name\"])\noperation = Operation(type=\"query\", queries=[hero])\n\nprint(operation.render())\n\"\"\"\nquery {\n hero {\n name\n }\n}\n\"\"\"\n```\n\nThe `render` method for the `graphql_query.Operation` object\njust returns the final string with a query. Inside the `fields` array of the `graphql_query.Query` object\nyou can use\n\n- `str` (a field name);\n- object of `graphql_query.Field` type;\n- `graphql_query.Fragment` and `graphql_query.InlineFragment`.\n\n### Arguments, Variables and Directives\n\nFor generation of the following query\n\n```graphql\nquery Hero($episode: Episode, $withFriends: Boolean!) {\n hero(episode: $episode) {\n name\n friends @include(if: $withFriends) {\n name\n }\n }\n}\n```\n\nwe can use the following python code\n\n```python\nfrom graphql_query import Argument, Directive, Field, Operation, Query, Variable\n\nepisode = Variable(name=\"episode\", type=\"Episode\")\nwithFriends = Variable(name=\"withFriends\", type=\"Boolean!\")\n\narg_episode = Argument(name=\"episode\", value=episode)\narg_if = Argument(name=\"if\", value=withFriends)\n\nhero = Query(\n name=\"hero\",\n arguments=[arg_episode],\n fields=[\n \"name\",\n Field(\n name=\"friends\",\n fields=[\"name\"],\n directives=[Directive(name=\"include\", arguments=[arg_if])]\n )\n ]\n)\noperation = Operation(\n type=\"query\",\n name=\"Hero\",\n variables=[episode, withFriends],\n queries=[hero]\n)\nprint(operation.render())\n\"\"\"\nquery Hero(\n $episode: Episode\n $withFriends: Boolean!\n) {\n hero(\n episode: $episode\n ) {\n name\n friends @include(\n if: $withFriends\n ) {\n name\n }\n }\n}\n\"\"\"\n```\n\nYou can find other examples in the documentation.\n",
"bugtrack_url": null,
"license": null,
"summary": "Complete Domain Specific Language (DSL) for GraphQL query in Python.",
"version": "1.4.0",
"project_urls": {
"Documentation": "https://denisart.github.io/graphql-query/",
"Homepage": "https://github.com/denisart/graphql-query",
"Source": "https://github.com/denisart/graphql-query"
},
"split_keywords": [
"code-generation",
" codegen",
" dsl",
" graphql",
" graphql-query",
" graphql-query-builder",
" pydantic",
" python",
" query-builder",
" query-generation",
" query-generator"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "47edec732d18dd016eb4d4fa590e392d22dd35c93f26f17f709596deeb780497",
"md5": "00b3cb9ca2ff7abb0084e24ebf79662f",
"sha256": "376ed550a7812425befbefb870daa21ce1696590fcb78c015215a43a5d7e51b7"
},
"downloads": -1,
"filename": "graphql_query-1.4.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "00b3cb9ca2ff7abb0084e24ebf79662f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 13389,
"upload_time": "2024-07-31T10:50:04",
"upload_time_iso_8601": "2024-07-31T10:50:04.055948Z",
"url": "https://files.pythonhosted.org/packages/47/ed/ec732d18dd016eb4d4fa590e392d22dd35c93f26f17f709596deeb780497/graphql_query-1.4.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "8b64377beef6c10b798f2ece54cfd3577db20102176c3a155469b92b4a3e3881",
"md5": "e93672cfdd6fb8c8cc224f9d3760021c",
"sha256": "1cfe5eeaad8b0ed67ac3d9c4023ee9743851f98c6b2f673c67088cf42ebb57bb"
},
"downloads": -1,
"filename": "graphql_query-1.4.0.tar.gz",
"has_sig": false,
"md5_digest": "e93672cfdd6fb8c8cc224f9d3760021c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 26178,
"upload_time": "2024-07-31T10:50:05",
"upload_time_iso_8601": "2024-07-31T10:50:05.249170Z",
"url": "https://files.pythonhosted.org/packages/8b/64/377beef6c10b798f2ece54cfd3577db20102176c3a155469b92b4a3e3881/graphql_query-1.4.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-31 10:50:05",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "denisart",
"github_project": "graphql-query",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "graphql-query"
}