Skip to main content

gql-query-builder

This is a GraphQL query builder.
Use with method chain.

Install

pip install gql-query-builder

Usage

  • query
from gql_query_builder import GqlQuery

query = GqlQuery().fields(['name']).query('hero').operation().generate()
print(query)
"""
query {
    hero {
        name
    }
}
"""
  • mutation
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.
#Syntax

fields(
    fields: List[str] = [],
    name: str = '',
    condition_expression: str = ''
)
  • query()
    build query fields.
#Syntax

query(
    name: str = '',
    alias: str = '',
    input: Dict[str, Union[str, int]] = {}
)
  • operation()
    build operation fields.
#Syntax

operation(
    query_type: str = 'query',
    name: str = '',
    input: Dict[str, Union[str, int]] = {},
    queries: List[str] = []
)
  • fragment()
    build fragment fields.
#Syntax

fragment(
    name: str,
    interface: str
)
  • generate()
    generate query.
#Syntax

generate()

Examples

  • Nesting fields
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
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
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
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
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
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
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
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
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
        }
    }
}
"""

Release files for gql-query-builder-with-pyproject 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gql-query-builder-with-pyproject 0.1.8
File Size Uploaded
gql-query-builder-with-pyproject-0.1.8.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gql-query-builder-with-pyproject 0.1.8
File Interpreter ABI Platform
gql_query_builder_with_pyproject-0.1.8-py3-none-any.whl Python 3 none any Details

Total release size: 9.4 kB

Release files / gql-query-builder-with-pyproject-0.1.8.tar.gz

Download URL gql-query-builder-with-pyproject-0.1.8.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
1a2c85e902aaf71464a2618a42841ca4246cfdf484577f77c3dcfb87c6752b6b
BLAKE2b-256 checksum
How to use checksums
fdc20a5030490c9a8d18d2f8cba7fefba81531875410074ee1256534f05e75ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.4

Release files / gql_query_builder_with_pyproject-0.1.8-py3-none-any.whl

Download URL gql_query_builder_with_pyproject-0.1.8-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7d1c585dfdbd58188a9b2e04bb622bf5fbcd84c60ce1ade512a19c386469b49d
BLAKE2b-256 checksum
How to use checksums
ccb4da1e6041004e5db1ab7e72f03fe3f06f44c25ed1fd4b4aa63404af823212
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.4

Release history Release notifications | RSS feed

This release

0.1.8 This release

2 release files

0.1.5

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page