graphql-query


Namegraphql-query JSON
Version 1.3.1 PyPI version JSON
download
home_page
SummaryComplete Domain Specific Language (DSL) for GraphQL query in Python.
upload_time2024-02-19 13:43:10
maintainer
docs_urlNone
author
requires_python>=3.8
license
keywords code-generation codegen dsl graphql graphql-query graphql-query-builder pydantic python query-builder query-generation query-generator
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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)
[![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": "",
    "name": "graphql-query",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "code-generation,codegen,dsl,graphql,graphql-query,graphql-query-builder,pydantic,python,query-builder,query-generation,query-generator",
    "author": "",
    "author_email": "\"Denis A. Artyushin\" <artyushinden@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/c0/d4/744cee69af91d3d1a7f526c983fc618757818d9b39ab441d148c8845ef2b/graphql_query-1.3.1.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[![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": "",
    "summary": "Complete Domain Specific Language (DSL) for GraphQL query in Python.",
    "version": "1.3.1",
    "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": "85c8bfcc8d57b1fe71763f601b548a6937140cb23fe92101ea007d040021720b",
                "md5": "cc154dc2dde7ace773945624a50fe207",
                "sha256": "19f4d9d0110d98ad742144858770613477459fbebd3bbad595bcc81ce799a41e"
            },
            "downloads": -1,
            "filename": "graphql_query-1.3.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "cc154dc2dde7ace773945624a50fe207",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 13371,
            "upload_time": "2024-02-19T13:43:09",
            "upload_time_iso_8601": "2024-02-19T13:43:09.361345Z",
            "url": "https://files.pythonhosted.org/packages/85/c8/bfcc8d57b1fe71763f601b548a6937140cb23fe92101ea007d040021720b/graphql_query-1.3.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c0d4744cee69af91d3d1a7f526c983fc618757818d9b39ab441d148c8845ef2b",
                "md5": "7f7bb3d0fd4d68f05c56ceeba13a9ae4",
                "sha256": "fef5217c3eea068a846e57adb72bb77d29fe27740e9c6b5aca0d84b8f705c8e3"
            },
            "downloads": -1,
            "filename": "graphql_query-1.3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "7f7bb3d0fd4d68f05c56ceeba13a9ae4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 23898,
            "upload_time": "2024-02-19T13:43:10",
            "upload_time_iso_8601": "2024-02-19T13:43:10.887461Z",
            "url": "https://files.pythonhosted.org/packages/c0/d4/744cee69af91d3d1a7f526c983fc618757818d9b39ab441d148c8845ef2b/graphql_query-1.3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-19 13:43:10",
    "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"
}
        
Elapsed time: 0.18750s