Skip to main content

hexastack-graphql

hexastack-graphql

Strawberry GraphQL presentation adapter and CQRS integration for Hexastack.

PyPI: hexastack-graphql Python 3.13+ Coverage License: Apache 2.0

1. Overview & Capabilities

hexastack-graphql brings the power and type-safety of Strawberry GraphQL into the Hexastack architecture:

  • Type-Safe GraphQL Schemas: Native Python dataclass-based GraphQL schema definition via Strawberry.
  • CQRS Integration via Context: Injects the rodi.Container, CommandBusPort, and QueryBusPort directly into Strawberry's Info.context (GraphQLContext).
  • Declarative Query & Mutation Registries:
    • @graphql_query_type and @graphql_mutation_type: Register whole type classes to be merged into root Query and Mutation types.
    • @graphql_query and @graphql_mutation: Register standalone resolver functions as top-level fields.
  • Dynamic Field Resolver Feature Flagging:
    • @feature_flag_field("flag_key", raise_error=True, fallback=...): Evaluates feature flags dynamically before executing field resolvers, raising a GraphQLError or returning a safe fallback value.
  • FastAPI Mount & GraphiQL Playground: Seamless mounting as a GraphQLRouter into FastAPI applications with interactive GraphiQL playground enabled.

2. Package Anatomy & Key Components

hexastack_graphql/
├── domain/          # GraphQLContext, GraphQLError, SchemaBuildingError
├── ports/           # GraphQLContextFactoryPort
├── adapters/        # create_graphql_router, mount_graphql_router (FastAPI integration)
└── infra/
    ├── bootstrap.py # GraphQLBootstrapper (order=35)
    ├── config.py    # HexastackGraphQLConfig
    ├── decorators.py# @graphql_query, @graphql_mutation, @graphql_query_type, @graphql_mutation_type, @feature_flag_field
    └── registries/  # schema.py (GraphQLSchemaRegistry)

Key Exports

Category Exports
Bootstrap GraphQLBootstrapper (order=35), HexastackGraphQLConfig
Context & Domain GraphQLContext, GraphQLError, SchemaBuildingError
Decorators @graphql_query, @graphql_mutation, @graphql_query_type, @graphql_mutation_type, @feature_flag_field
FastAPI Adapters create_graphql_router, mount_graphql_router
Registries GraphQLSchemaRegistry, get_schema_registry

3. Monorepo & Sibling Relationships

graph TD
    subgraph ClientRequests ["GraphQL Client Requests"]
        CLIENT["Web / Mobile GraphQL Clients"]
    end

    subgraph GraphQLAdapter ["hexastack-graphql"]
        SCHEMA["strawberry.Schema"]
        CTX["GraphQLContext (Container + Buses)"]
        ROUTER["GraphQLRouter (FastAPI integration)"]
    end

    subgraph ApplicationLayer ["hexastack-cqrs"]
        CBUS["CommandBusPort"]
        QBUS["QueryBusPort"]
    end

    subgraph WebServer ["hexastack-fastapi"]
        FASTAPI_APP["FastAPI Application"]
    end

    CLIENT --> ROUTER
    FASTAPI_APP --> ROUTER
    ROUTER --> SCHEMA
    SCHEMA --> CTX
    CTX -->|dispatches commands/queries to| CBUS
    CTX -->|dispatches commands/queries to| QBUS

Explicit Dependencies (Direct)

  • hexastack-core: DI container, configuration registry, base exceptions.
  • hexastack-cqrs: CommandBusPort and QueryBusPort for message dispatching.
  • strawberry-graphql>=0.260.0: Core GraphQL engine and schema generator.

Implied / Behavioral Relationships (DI-Mediated)

  • FastAPI Auto-Mounting: GraphQLBootstrapper (order=35) discovers the FastAPI instance created by FastApiBootstrapper (order=30) and attaches the GraphQLRouter automatically if auto_mount_fastapi=true.
  • CQRS Dispatching: Field resolvers receive info.context.query_bus and info.context.command_bus to delegate execution into the CQRS pipeline.

Optional Integrations (Extras)

  • [fastapi]: Installs hexastack-fastapi and fastapi>=0.141.1 for HTTP routing and GraphiQL playground.

4. Installation

# Standalone install
pip install hexastack-graphql

# With FastAPI integration
pip install "hexastack-graphql[fastapi]"

# Via umbrella package
pip install "hexastack[graphql]"

5. Configuration Reference

[hexastack.graphql]
path = "/graphql" # Route prefix for GraphQL endpoint
graphiql = true # Enable interactive GraphiQL web UI
allow_queries = true
allow_mutations = true
auto_mount_fastapi = true # Auto mount onto FastAPI application on bootstrap
title = "Hexastack GraphQL API"

6. Quickstart Example

from dataclasses import dataclass
import strawberry
from strawberry.types import Info
from hexastack_core.infra.bootstrap import bootstrap
from hexastack_cqrs.domain.query import Query
from hexastack_cqrs.infra.decorators import query_handler
from hexastack_graphql.domain.context import GraphQLContext
from hexastack_graphql.infra.decorators import graphql_query_type


# 1. Define CQRS Query & Handler
@dataclass(frozen=True)
class GetItemQuery(Query):
    item_id: str


@query_handler(GetItemQuery)
class GetItemHandler:
    def __call__(self, qry: GetItemQuery) -> dict:
        return {"id": qry.item_id, "name": f"Item {qry.item_id}"}


# 2. Define Strawberry GraphQL Type
@strawberry.type
class ItemType:
    id: str
    name: str


@graphql_query_type
class Query:
    @strawberry.field
    def item(self, info: Info[GraphQLContext, None], item_id: str) -> ItemType:
        res = info.context.query_bus.dispatch(GetItemQuery(item_id=item_id))
        return ItemType(id=res["id"], name=res["name"])


# 3. Bootstrap Runtime with GraphQL
runtime = bootstrap(packages_to_scan=[__name__])
schema = runtime.get("graphql_schema")

result = schema.execute_sync(
    '{ item(itemId: "123") { id name } }',
    context_value=GraphQLContext(
        container=runtime.container, query_bus=runtime.get("query_bus")
    ),
)
print(result.data)  # {'item': {'id': '123', 'name': 'Item 123'}}

Download files

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

Source Distribution

hexastack_graphql-0.1.0.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

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

hexastack_graphql-0.1.0-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

Details for the file hexastack_graphql-0.1.0.tar.gz.

File metadata

  • Download URL: hexastack_graphql-0.1.0.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","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":null}

File hashes

Hashes for hexastack_graphql-0.1.0.tar.gz
Algorithm Hash digest
SHA256 da1d58e4bd991b30c0e11b93d8e75960976dd8e4270b8a14d0ec7f102714078f
MD5 0b39b1f75d05a41699ea9a0a9b0ea371
BLAKE2b-256 d71f637e6f2f0b0ca42d7f015e7a5700e87d396db48f4c9df9abb5915a9703ac

See more details on using hashes here.

File details

Details for the file hexastack_graphql-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: hexastack_graphql-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","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":null}

File hashes

Hashes for hexastack_graphql-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ccc491bc290a7d66052d3b078cb79dcbe5a6bb5bea539a5543d1fce3302103bc
MD5 090f68ebc947fd37ddc5f34213eb9a88
BLAKE2b-256 8f139213e8a32540eb3ed78e508e8b17219e657c3b8a4afc765e802f5b3a738c

See more details on using hashes here.

Release history Release notifications | RSS feed

0.3.0

2 files

0.2.0

2 files

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page