Skip to main content

Dynamic GraphQL query builder

Project description

About this project

This project emerged from the frustrating experience when trying to write gql queries with dynamic filters. In some cases, think of a UI to beautifully display database contents, one desires to be able to set filters dynamically if some value is provided, yet just ignore it if not.

This behavior was guaranteed by Hasura versions 1.3.3v and below - but disabled by default in later versions, as described here.

The env var HASURA_GRAPHQL_V1_BOOLEAN_NULL_COLLAPSE preserves this functionality globally. However, this as well is not always desired, when a more fine-grained control over when a value is strictly necessary for where conditions, and when not, is of importance.

This project therefore provides a lightweight query builder that takes a simple gql query string as input and inserts the respective clauses if a value is presented, and does nothing, if not. This avoids nasty, error-prone, string concatenations while avoiding heavy-weight ASTs and the necessity of schema declarations as with gql-DSL.

Usage

All a user should be interacting with is located in the src.api package. As of now it only contains the GQLDynamicQueryBuilder which is the heart of the project and provides all the necessary functionality. To extend a query by an optional where clause one can do the following:

    query = """
        query TestQuery {
            product {
                name
                brand
            }
        }
    """
    
    builder = GQLDynamicQueryBuilder(query)
    builder = builder.with_where_clause(
                table_name='product', 
                field_name='name', 
                value='tomato', 
                operation='_ilike',
                skip_if_none=True
              )
    result = builder.build() # returns the transformed query as a string

This also works for queries with existing where clauses and other query parameters (e.g. limit). To access nested fields, simply use '.' as the delimiter:

    builder.with_where_clause(
        'product', 
        'brand.name', # will build brand { name: ...
        'ABC', 
        '_eq',
        skip_if_none=True
    )
    result = builder.build()

Furthermore, it is also possible to provide the following to with_where_clause:

  • values: list and operation: str which allows for set-operations like _in
  • values: list and operation: list[str] which allows for multiple operations for the same field (e.g. timestamp {_gte: <ts1> lt: <ts2>})

As a fallback also explicit where clauses are supported via table_name: clause dictionaries:

    builder.with_where_clauses(
        {'product': 'name: {_ilike : "tomato"}'}
    )
    result = builder.build()

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_dynamic_query_builder-1.0.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

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

gql_dynamic_query_builder-1.0.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file gql_dynamic_query_builder-1.0.0.tar.gz.

File metadata

File hashes

Hashes for gql_dynamic_query_builder-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f11f4c2a225fb686b40e1f05eb05414eb57c0d668027f97224e2e5a23e8f66d0
MD5 d5bf68e44c7f13156c9b905562fa2635
BLAKE2b-256 9d69a2032996f51b866a9fd6944308e4642abc26a48ee1426e88b1d6b409659d

See more details on using hashes here.

File details

Details for the file gql_dynamic_query_builder-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for gql_dynamic_query_builder-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3c76141158b945c3147a5a73ed8a6caf9a1df4f7ce02d50cc7bd1062fd676557
MD5 e86503b27d52271a1985b69c14226644
BLAKE2b-256 5ae28730015b0553d612a5ac6b10bf9fa3cab9f2b1c24f2f05f2ea5cb67def16

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