# gql-query-builder
![](https://github.com/youyo/gql-query-builder/workflows/Publish%20python%20package/badge.svg)
This is a GraphQL query builder.
Use with method chain.
## Install
```
pip install gql-query-builder
```
## Usage
- query
```python
from gql_query_builder import GqlQuery
query = GqlQuery().fields(['name']).query('hero').operation().generate()
print(query)
"""
query {
hero {
name
}
}
"""
```
- mutation
```python
from gql_query_builder import GqlQuery
query = GqlQuery().fields(['stars', 'commentary']).query('createReview', input={"episode": "$ep", "review": "$review"}).operation('mutation', name='CreateReviewForEpisode', input={"$ep": "Episode!", "$review": "ReviewInput!"}).generate()
print(query)
"""
mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
createReview(episode: $ep, review: $review) {
stars
commentary
}
}
"""
```
### Methods
- `fields()`
build response fields.
```python
#Syntax
fields(
fields: List[str] = [],
name: str = '',
condition_expression: str = ''
)
```
- `query()`
build query fields.
```python
#Syntax
query(
name: str = '',
alias: str = '',
input: Dict[str, Union[str, int]] = {}
)
```
- `operation()`
build operation fields.
```python
#Syntax
operation(
query_type: str = 'query',
name: str = '',
input: Dict[str, Union[str, int]] = {},
queries: List[str] = []
)
```
- `fragment()`
build fragment fields.
```python
#Syntax
fragment(
name: str,
interface: str
)
```
- `generate()`
generate query.
```python
#Syntax
generate()
```
## Examples
- Nesting fields
```python
from gql_query_builder import GqlQuery
field_friends = GqlQuery().fields(['name'], name='friends').generate()
query = GqlQuery().fields(['name', field_friends]).query('hero').operation('query').generate()
print(query)
"""
query {
hero {
name
friends {
name
}
}
}
"""
```
- Query with input
```python
from gql_query_builder import GqlQuery
query = GqlQuery().fields(['name', 'height']).query('human', input={"id": '"1000"'}).operation().generate()
print(query)
"""
query {
human(id: "1000") {
name
height
}
}
"""
```
- Query with nested input
```python
from gql_query_builder import GqlQuery
GqlQuery().fields(['name', 'height']).query('human', input={"input": {"data": {"id": "1000", "name": "test"}}}).operation().generate()
"""
query{
human(input: {data: {id: "1000", name: "test"}}){
human{
name,
height
}
}
}
"""
```
- Query with input and arguments
```python
from gql_query_builder import GqlQuery
query = GqlQuery().fields(['name', 'height(unit: FOOT)']).query('human', input={"id": '"1000"'}).operation().generate()
print(query)
"""
query {
human(id: "1000") {
name
height(unit: FOOT)
}
}
"""
```
- Alias
```python
from gql_query_builder import GqlQuery
query_empirehero = GqlQuery().fields(['name']).query('hero', alias='empireHero', input={"episode": 'EMPIRE'}).generate()
query_jedihero = GqlQuery().fields(['name']).query('hero', alias='jediHero', input={"episode": 'JEDI'}).generate()
query = GqlQuery().operation('query', queries=[query_empirehero, query_jedihero]).generate()
print(query)
"""
query {
empireHero: hero(episode: EMPIRE) {
name
}
jediHero: hero(episode: JEDI) {
name
}
}
"""
```
- Fragments
```python
from gql_query_builder import GqlQuery
field_friends = GqlQuery().fields(['name'], name='friends').generate()
query = GqlQuery().fields(['name', 'appearsIn', field_friends]).fragment('comparisonFields', 'Character').generate()
print(query)
"""
fragment comparisonFields on Character {
name
appearsIn
friends {
name
}
}
"""
```
- Refer to fragments
```python
from gql_query_builder import GqlQuery
query_leftComparison = GqlQuery().fields(['...comparisonFields']).query('hero', alias='leftComparison', input={"episode": "EMPIRE"}).generate()
query_rightComparison = GqlQuery().fields(['...comparisonFields']).query('hero', alias='rightComparison', input={"episode": "JEDI"}).generate()
query = GqlQuery().operation('query', queries=[query_leftComparison, query_rightComparison]).generate()
print(query)
"""
query {
leftComparison: hero(episode: EMPIRE) {
...comparisonFields
}
rightComparison: hero(episode: JEDI) {
...comparisonFields
}
}
"""
```
- Query with variables
```python
from gql_query_builder import GqlQuery
field_friends = GqlQuery().fields(['name'], name='friends').generate()
query = GqlQuery().fields(['name', field_friends]).query('hero', input={"episode": "$episode"}).operation('query', name='HeroNameAndFriends', input={"$episode": "Episode"}).generate()
print(query)
"""
query HeroNameAndFriends($episode: Episode) {
hero(episode: $episode) {
name
friends {
name
}
}
}
"""
```
- Directives
```python
from gql_query_builder import GqlQuery
field_friends = GqlQuery().fields(['name'], name='friends @include(if: $withFriends)').generate()
query = GqlQuery().fields(['name', field_friends]).query('hero', input={"episode": "$episode"}).operation('query', name='Hero', input={"$episode": "Episode", "$withFriends": "Boolean!"}).generate()
print(query)
"""
query Hero($episode: Episode, $withFriends: Boolean!) {
hero(episode: $episode) {
name
friends @include(if: $withFriends) {
name
}
}
}
"""
```
Raw data
{
"_id": null,
"home_page": null,
"name": "gql-query-builder-with-pyproject",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": "graphql, gql, query-builder",
"author": null,
"author_email": "youyo <1003ni2@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/fd/c2/0a5030490c9a8d18d2f8cba7fefba81531875410074ee1256534f05e75ca/gql-query-builder-with-pyproject-0.1.8.tar.gz",
"platform": null,
"description": "# gql-query-builder\r\n\r\n![](https://github.com/youyo/gql-query-builder/workflows/Publish%20python%20package/badge.svg)\r\n\r\nThis is a GraphQL query builder. \r\nUse with method chain.\r\n\r\n## Install\r\n\r\n```\r\npip install gql-query-builder\r\n```\r\n\r\n## Usage\r\n\r\n- query\r\n\r\n```python\r\nfrom gql_query_builder import GqlQuery\r\n\r\nquery = GqlQuery().fields(['name']).query('hero').operation().generate()\r\nprint(query)\r\n\"\"\"\r\nquery {\r\n hero {\r\n name\r\n }\r\n}\r\n\"\"\"\r\n```\r\n\r\n- mutation\r\n\r\n```python\r\nfrom gql_query_builder import GqlQuery\r\n\r\nquery = GqlQuery().fields(['stars', 'commentary']).query('createReview', input={\"episode\": \"$ep\", \"review\": \"$review\"}).operation('mutation', name='CreateReviewForEpisode', input={\"$ep\": \"Episode!\", \"$review\": \"ReviewInput!\"}).generate()\r\nprint(query)\r\n\"\"\"\r\nmutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {\r\n createReview(episode: $ep, review: $review) {\r\n stars\r\n commentary\r\n }\r\n}\r\n\"\"\"\r\n```\r\n\r\n### Methods\r\n\r\n- `fields()` \r\n build response fields.\r\n\r\n```python\r\n#Syntax\r\n\r\nfields(\r\n fields: List[str] = [],\r\n name: str = '',\r\n condition_expression: str = ''\r\n)\r\n```\r\n\r\n- `query()` \r\n build query fields.\r\n\r\n```python\r\n#Syntax\r\n\r\nquery(\r\n name: str = '',\r\n alias: str = '',\r\n input: Dict[str, Union[str, int]] = {}\r\n)\r\n```\r\n\r\n- `operation()` \r\n build operation fields.\r\n\r\n```python\r\n#Syntax\r\n\r\noperation(\r\n query_type: str = 'query',\r\n name: str = '',\r\n input: Dict[str, Union[str, int]] = {},\r\n queries: List[str] = []\r\n)\r\n```\r\n\r\n- `fragment()` \r\n build fragment fields.\r\n\r\n```python\r\n#Syntax\r\n\r\nfragment(\r\n name: str,\r\n interface: str\r\n)\r\n```\r\n\r\n- `generate()` \r\n generate query.\r\n\r\n```python\r\n#Syntax\r\n\r\ngenerate()\r\n```\r\n\r\n## Examples\r\n\r\n- Nesting fields\r\n\r\n```python\r\nfrom gql_query_builder import GqlQuery\r\n\r\nfield_friends = GqlQuery().fields(['name'], name='friends').generate()\r\nquery = GqlQuery().fields(['name', field_friends]).query('hero').operation('query').generate()\r\nprint(query)\r\n\"\"\"\r\nquery {\r\n hero {\r\n name\r\n friends {\r\n name\r\n }\r\n }\r\n}\r\n\"\"\"\r\n```\r\n\r\n- Query with input\r\n\r\n```python\r\nfrom gql_query_builder import GqlQuery\r\n\r\nquery = GqlQuery().fields(['name', 'height']).query('human', input={\"id\": '\"1000\"'}).operation().generate()\r\nprint(query)\r\n\"\"\"\r\nquery {\r\n human(id: \"1000\") {\r\n name\r\n height\r\n }\r\n}\r\n\"\"\"\r\n```\r\n\r\n- Query with nested input\r\n\r\n```python\r\nfrom gql_query_builder import GqlQuery\r\nGqlQuery().fields(['name', 'height']).query('human', input={\"input\": {\"data\": {\"id\": \"1000\", \"name\": \"test\"}}}).operation().generate()\r\n\"\"\"\r\nquery{\r\n human(input: {data: {id: \"1000\", name: \"test\"}}){\r\n human{\r\n name, \r\n height\r\n }\r\n }\r\n}\r\n\"\"\"\r\n```\r\n\r\n- Query with input and arguments\r\n\r\n```python\r\nfrom gql_query_builder import GqlQuery\r\n\r\nquery = GqlQuery().fields(['name', 'height(unit: FOOT)']).query('human', input={\"id\": '\"1000\"'}).operation().generate()\r\nprint(query)\r\n\"\"\"\r\nquery {\r\n human(id: \"1000\") {\r\n name\r\n height(unit: FOOT)\r\n }\r\n}\r\n\"\"\"\r\n```\r\n\r\n- Alias\r\n\r\n```python\r\nfrom gql_query_builder import GqlQuery\r\n\r\nquery_empirehero = GqlQuery().fields(['name']).query('hero', alias='empireHero', input={\"episode\": 'EMPIRE'}).generate()\r\nquery_jedihero = GqlQuery().fields(['name']).query('hero', alias='jediHero', input={\"episode\": 'JEDI'}).generate()\r\nquery = GqlQuery().operation('query', queries=[query_empirehero, query_jedihero]).generate()\r\nprint(query)\r\n\"\"\"\r\nquery {\r\n empireHero: hero(episode: EMPIRE) {\r\n name\r\n }\r\n jediHero: hero(episode: JEDI) {\r\n name\r\n }\r\n}\r\n\"\"\"\r\n```\r\n\r\n- Fragments\r\n\r\n```python\r\nfrom gql_query_builder import GqlQuery\r\n\r\nfield_friends = GqlQuery().fields(['name'], name='friends').generate()\r\nquery = GqlQuery().fields(['name', 'appearsIn', field_friends]).fragment('comparisonFields', 'Character').generate()\r\nprint(query)\r\n\"\"\"\r\nfragment comparisonFields on Character {\r\n name\r\n appearsIn\r\n friends {\r\n name\r\n }\r\n}\r\n\"\"\"\r\n```\r\n\r\n- Refer to fragments\r\n\r\n```python\r\nfrom gql_query_builder import GqlQuery\r\n\r\nquery_leftComparison = GqlQuery().fields(['...comparisonFields']).query('hero', alias='leftComparison', input={\"episode\": \"EMPIRE\"}).generate()\r\nquery_rightComparison = GqlQuery().fields(['...comparisonFields']).query('hero', alias='rightComparison', input={\"episode\": \"JEDI\"}).generate()\r\nquery = GqlQuery().operation('query', queries=[query_leftComparison, query_rightComparison]).generate()\r\nprint(query)\r\n\"\"\"\r\nquery {\r\n leftComparison: hero(episode: EMPIRE) {\r\n ...comparisonFields\r\n }\r\n rightComparison: hero(episode: JEDI) {\r\n ...comparisonFields\r\n }\r\n}\r\n\"\"\"\r\n```\r\n\r\n- Query with variables\r\n\r\n```python\r\nfrom gql_query_builder import GqlQuery\r\n\r\nfield_friends = GqlQuery().fields(['name'], name='friends').generate()\r\nquery = GqlQuery().fields(['name', field_friends]).query('hero', input={\"episode\": \"$episode\"}).operation('query', name='HeroNameAndFriends', input={\"$episode\": \"Episode\"}).generate()\r\nprint(query)\r\n\"\"\"\r\nquery HeroNameAndFriends($episode: Episode) {\r\n hero(episode: $episode) {\r\n name\r\n friends {\r\n name\r\n }\r\n }\r\n}\r\n\"\"\"\r\n```\r\n\r\n- Directives\r\n\r\n```python\r\nfrom gql_query_builder import GqlQuery\r\n\r\nfield_friends = GqlQuery().fields(['name'], name='friends @include(if: $withFriends)').generate()\r\nquery = GqlQuery().fields(['name', field_friends]).query('hero', input={\"episode\": \"$episode\"}).operation('query', name='Hero', input={\"$episode\": \"Episode\", \"$withFriends\": \"Boolean!\"}).generate()\r\nprint(query)\r\n\"\"\"\r\nquery Hero($episode: Episode, $withFriends: Boolean!) {\r\n hero(episode: $episode) {\r\n name\r\n friends @include(if: $withFriends) {\r\n name\r\n }\r\n }\r\n}\r\n\"\"\"\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "This is a GraphQL query builder. This is a fork with pyproject to make installing easier",
"version": "0.1.8",
"project_urls": {
"Homepage": "https://github.com/rtaycher/gql-query-builder"
},
"split_keywords": [
"graphql",
" gql",
" query-builder"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "ccb4da1e6041004e5db1ab7e72f03fe3f06f44c25ed1fd4b4aa63404af823212",
"md5": "36dbccedd77b4d5552d4c24f19861a33",
"sha256": "7d1c585dfdbd58188a9b2e04bb622bf5fbcd84c60ce1ade512a19c386469b49d"
},
"downloads": -1,
"filename": "gql_query_builder_with_pyproject-0.1.8-py3-none-any.whl",
"has_sig": false,
"md5_digest": "36dbccedd77b4d5552d4c24f19861a33",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 4694,
"upload_time": "2024-11-27T11:37:53",
"upload_time_iso_8601": "2024-11-27T11:37:53.977407Z",
"url": "https://files.pythonhosted.org/packages/cc/b4/da1e6041004e5db1ab7e72f03fe3f06f44c25ed1fd4b4aa63404af823212/gql_query_builder_with_pyproject-0.1.8-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "fdc20a5030490c9a8d18d2f8cba7fefba81531875410074ee1256534f05e75ca",
"md5": "a669f4d9bd2c6f237b4c67c3208cc793",
"sha256": "1a2c85e902aaf71464a2618a42841ca4246cfdf484577f77c3dcfb87c6752b6b"
},
"downloads": -1,
"filename": "gql-query-builder-with-pyproject-0.1.8.tar.gz",
"has_sig": false,
"md5_digest": "a669f4d9bd2c6f237b4c67c3208cc793",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 4676,
"upload_time": "2024-11-27T11:37:52",
"upload_time_iso_8601": "2024-11-27T11:37:52.274444Z",
"url": "https://files.pythonhosted.org/packages/fd/c2/0a5030490c9a8d18d2f8cba7fefba81531875410074ee1256534f05e75ca/gql-query-builder-with-pyproject-0.1.8.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-27 11:37:52",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "rtaycher",
"github_project": "gql-query-builder",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "gql-query-builder-with-pyproject"
}