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: listandoperation: strwhich allows for set-operations like_invalues: listandoperation: 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gql_dynamic_query_builder-0.1.0.tar.gz.
File metadata
- Download URL: gql_dynamic_query_builder-0.1.0.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e21df10962a74876f250a36842d91e2e08b2ce36892df4e82994e2f1683f4ef5
|
|
| MD5 |
9e10bc3b3c93bb18d19a39e251e586d4
|
|
| BLAKE2b-256 |
78ac7476601556249a142f146c8166a9a564463aa0f84b1bf475b792d88d31bb
|
File details
Details for the file gql_dynamic_query_builder-0.1.0-py3-none-any.whl.
File metadata
- Download URL: gql_dynamic_query_builder-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb0f0b2adf0fb4238795af9c171afcddd40652585bb1a4e5e4cd7618aa3cd4be
|
|
| MD5 |
e6e134d64457f183f36a2d819736067e
|
|
| BLAKE2b-256 |
a21f29b130eff2d83dcc16bd8a9461f7ca2c8d81676d72c30de6faf9f242ced1
|