GraphQL Query DSL for Python
graphql-query is a Python library that provides a complete Domain Specific Language (DSL) for constructing GraphQL queries programmatically. It allows developers to build complex GraphQL queries, mutations, and fragments using Python objects, simplifying the process of generating valid GraphQL syntax. The library is actively maintained with regular updates, currently at version 1.4.0.
Common errors
-
TypeError: 'list' object cannot be interpreted as an integer
cause Attempting to pass a list or an incorrectly structured value where a single scalar value is expected for a GraphQL argument or field property in the `graphql-query` DSL.fixVerify that the `value` argument in `Argument(name='...', value=...)` or other field properties matches the expected scalar type (e.g., string, int, bool, float) or correct list structure if a list is intended. -
graphql_query.exceptions.InvalidGraphqlQuery: Field 'someField' must have a selection of subfields. Did you mean 'someField { ... }'?cause A `Field` object was created without specifying nested `fields` for a non-leaf GraphQL field, which is required by GraphQL syntax.fixFor any field that returns an object type (not a scalar), ensure you pass a list of `Field` objects to its `fields` argument, e.g., `Field(name='user', fields=[Field(name='id'), Field(name='name')])`. -
PydanticUserError: Pydantic V1 has been removed from Pydantic V2.
cause Your environment has Pydantic V2 installed, but some part of your application (or an older version of `graphql-query`) is trying to import or use Pydantic V1 components, which are no longer directly available or have changed APIs.fixEnsure `graphql-query` is at version 1.3.0 or higher. If you have other Pydantic-dependent libraries, you may need to update them or use Pydantic V2's compatibility features (like `pydantic.v1`) carefully. The `bump-pydantic` tool can help migrate your codebase.
Warnings
- breaking Migration to Pydantic v2 occurred in graphql-query v1.2.0 and became the default in v1.3.0. Projects using Pydantic v1.x will encounter compatibility issues.
- breaking Python 3.7 support was dropped in version 1.3.1. The library now requires Python 3.8 or newer.
- deprecated The `graphql-core` dependency was removed in v1.1.1, simplifying the dependency tree. If your project explicitly relied on `graphql-core` through `graphql-query`'s transitive dependencies, you might need to add it directly if still required for other purposes.
- gotcha When defining query arguments with Python types (e.g., `value='some string'`), the library infers GraphQL types. For complex types or specific GraphQL scalar types, ensure the Python value is compatible or explicitly castable, as mismatches can lead to invalid GraphQL queries.
Install
-
pip install graphql-query
Imports
- Query
from graphql_query import Query
- Field
from graphql_query import Field
- Argument
from graphql_query import Argument
Quickstart
from graphql_query import Query, Field, Argument
# Example 1: Simple Query
simple_query = Query(
name="myQuery",
fields=[
Field(name="hero", fields=[Field(name="name")]),
]
)
print(f"Simple Query: {simple_query.render()}")
# Expected output: { myQuery { hero { name } } }
# Example 2: Query with Arguments
query_with_args = Query(
name="character",
arguments=[
Argument(name="id", value="1000")
],
fields=[
Field(name="name"),
Field(name="appearsIn")
]
)
print(f"Query with Arguments: {query_with_args.render()}")
# Expected output: query character($id: String = "1000") { character(id: $id) { name appearsIn } }