Skip to main content

This is a GraphQL query builder. This is a fork with pyproject to make installing possible on newer tools

Project description

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 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
        }
    }
}
"""

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gql-query-builder-with-pyproject-0.1.5.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file gql-query-builder-with-pyproject-0.1.5.tar.gz.

File metadata

File hashes

Hashes for gql-query-builder-with-pyproject-0.1.5.tar.gz
Algorithm Hash digest
SHA256 fbe95b976823ff82d52e62bed44367df4be03394f422573ec61eac91a50a915c
MD5 904644ad9609493f6cc040f4fa83b0d8
BLAKE2b-256 2b1494cb9cd8e101381b8a579f34608fadfac56da45a70f26b8fcf8a2866fdda

See more details on using hashes here.

File details

Details for the file gql_query_builder_with_pyproject-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for gql_query_builder_with_pyproject-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 15ee696895cb6d9de274525649e4f68cb04092ac8e73458cca39cbcae5fcad36
MD5 dbf7560f72e09cf08f0d4fe87916f834
BLAKE2b-256 f4f5e34c7256072f8be4862918c2b7fd314046f5265252ce11551bd31a8ea1a1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page