iron_gql generates typed async GraphQL clients and runtime helpers from schemas for Python services
Project description
iron_gql
iron_gql is a lightweight GraphQL code generator and runtime that turns schema SDL and real query documents into typed Python clients powered by Pydantic models. Use it to wire GraphQL APIs into services, CLIs, background jobs, or tests without hand-writing boilerplate.
Key Features
- Query discovery.
generate_gql_packagescans your codebase for calls that look like<package>_gql("""..."""), validates each statement, and emits a module with strongly typed helpers. - Typed inputs and results. Generated Pydantic models mirror every selection set, enum, and input object referenced by the discovered queries.
- Async runtime.
runtime.GQLClientspeaks to GraphQL endpoints overgql+httpxand can shortcut network hops when pointed at an ASGI app. - Deterministic validation.
graphql-coreenforces schema compatibility and rejects duplicate operation names with incompatible bodies.
Package Layout
generator.py– orchestrates query discovery, validation, and module rendering.parser.py– converts GraphQL AST into typed helper structures consumed by the renderer.runtime.py– provides the asyncGQLClient, the reusableGQLQuerybase class, and value serialization helpers.
Getting Started
- Describe your schema. Point
generate_gql_packageat an SDL file (schema.graphql). Include whichever root types you rely on (query, mutation, subscription). - Author queries where they live. Import the future helper and wrap your GraphQL statement:
from myapp.gql.client import client_gql get_user = client_gql( """ query GetUser($id: ID!) { user(id: $id) { id name } } """ )
The generator infers the helper name (client_gql) from the package path you ask it to build. - Generate the client module.
from pathlib import Path from iron_gql.generator import generate_gql_package generate_gql_package( schema_path=Path("schema.graphql"), package_full_name="myapp.gql.client", base_url_import="myapp.config:GRAPHQL_URL", scalars={"ID": "builtins:str"}, to_camel_fn_full_name="myapp.inflection:to_camel", to_snake_fn=my_project_to_snake, debug_path=Path("iron_gql/debug/myapp.gql.client"), src_path=Path("."), )
The call writesmyapp/gql/client.pycontaining:- an async client singleton,
- Pydantic result and input models,
- a query class per operation with typed
executemethods, - overloads for the helper function so editors can infer return types.
- Call your API.
async def fetch_user(user_id: str): query = get_user.with_headers({"Authorization": "Bearer token"}) result = await query.execute(id=user_id) return result.user
Customization Hooks
- Scalar mapping. Provide
scalars={"DateTime": "datetime:datetime"}to map schema scalars onto importable Python types. Unknown scalars fall back toobjectwith a log warning. - Naming conventions. Supply
to_camel_fn_full_name(module:path string) and ato_snake_fncallable to align casing with your ownalias_generator. - Endpoint configuration.
base_url_importis written verbatim into the generated module; point it at a global string, config object, or helper that returns the GraphQL endpoint.
Runtime Highlights
GQLClientaccepts ASGItarget_appso you can reuse the runtime for production HTTP calls or in-process ASGI execution.GQLQuery.with_headersandGQLQuery.with_file_uploadsclone the query object, making per-call customization trivial.Uploadscalars map togql.FileVarfor multipart file handling.serialize_varconverts nested Pydantic models, dicts, lists, and primitives into JSON-friendly structures for variable payloads.
Validation and Troubleshooting
- Errors identify the file and line where the problematic statement lives.
- Duplicate operation names must share identical bodies; rename or consolidate to resolve the conflict.
Project details
Release history Release notifications | RSS feed
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 iron_gql-0.2.6.tar.gz.
File metadata
- Download URL: iron_gql-0.2.6.tar.gz
- Upload date:
- Size: 15.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
082f95fb2f28eb458a8d619849c8cb65b52b5b76bd065e2ff568eafbdc4b6e5b
|
|
| MD5 |
219cbac68d46659b9b62a2376b0ee74b
|
|
| BLAKE2b-256 |
6b029de3203c3cb957518d6158b4b03cad1b60ce3d9a3077bdadbac8d61ac564
|
File details
Details for the file iron_gql-0.2.6-py3-none-any.whl.
File metadata
- Download URL: iron_gql-0.2.6-py3-none-any.whl
- Upload date:
- Size: 17.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c3b51cdc2a70dcdafb761f8f9a9036e3247354324b0bb73635311956ecd8c69
|
|
| MD5 |
68f82120d9a41ba42d484a23765d9752
|
|
| BLAKE2b-256 |
d139686e716d6c3ae4bc50c8d63e58136ae039169c2178205b8897eb9ea29cad
|