Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/youyo/gql-query-builder
This is a GraphQL query builder.
https://github.com/youyo/gql-query-builder
graphql graphql-client python query-builder
Last synced: 3 months ago
JSON representation
This is a GraphQL query builder.
- Host: GitHub
- URL: https://github.com/youyo/gql-query-builder
- Owner: youyo
- License: mit
- Created: 2019-09-08T18:57:43.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-09-02T09:05:46.000Z (over 2 years ago)
- Last Synced: 2024-09-28T21:22:28.586Z (4 months ago)
- Topics: graphql, graphql-client, python, query-builder
- Language: Python
- Homepage:
- Size: 19.5 KB
- Stars: 35
- Watchers: 4
- Forks: 10
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# 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 GqlQueryquery = GqlQuery().fields(['name']).query('hero').operation().generate()
print(query)
"""
query {
hero {
name
}
}
"""
```- mutation
```python
from gql_query_builder import GqlQueryquery = 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
#Syntaxfields(
fields: List[str] = [],
name: str = '',
condition_expression: str = ''
)
```- `query()`
build query fields.```python
#Syntaxquery(
name: str = '',
alias: str = '',
input: Dict[str, Union[str, int]] = {}
)
```- `operation()`
build operation fields.```python
#Syntaxoperation(
query_type: str = 'query',
name: str = '',
input: Dict[str, Union[str, int]] = {},
queries: List[str] = []
)
```- `fragment()`
build fragment fields.```python
#Syntaxfragment(
name: str,
interface: str
)
```- `generate()`
generate query.```python
#Syntaxgenerate()
```## Examples
- Nesting fields
```python
from gql_query_builder import GqlQueryfield_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 GqlQueryquery = 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 GqlQueryquery = 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 GqlQueryquery_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 GqlQueryfield_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 GqlQueryquery_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 GqlQueryfield_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 GqlQueryfield_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
}
}
}
"""
```