Skip to content
Open Source · SQLModel · Python 3.10+

From SQLModel to API, Progressively

SQLModel entities drive GraphQL API, REST DTOs, and MCP services — one model, many presentations.

pip install nexusx

Rapid GraphQL API Generation

For development exploration — rapidly validate entity relationships and data shapes.

Without nexusx
# Model already exists — but you redeclare it as SDL, field by field
@strawberry.type
class PostType:
    id: strawberry.ID
    title: str
    @strawberry.field
    async def author(self, info) -> UserType | None:
        return await info.context["author_loader"].load(self.author_id)

# + a custom DataLoader per relationship, or N+1 kills you
class AuthorLoader(DataLoader):
    async def batch_load_fn(self, keys): ...
 # ↑ repeat for every entity × relationship
With nexusx
class Post(SQLModel, table=True):
    id: int | None = Field(primary_key=True)
    title: str
    author_id: int = Field(foreign_key="user.id")
    author: User | None = Relationship()

    @query
    async def get_all(cls, limit: int = 10) -> list['Post']: ...
 # SDL + resolvers + DataLoader — all derived from this one class

Declarative REST Endpoint Construction

For production data delivery — build stable REST endpoints with typed DTOs.

Manual query + assembly
# Per-endpoint: manual SQL, N+1, dict munging
async def get_sprints():
    sprints = await session.exec(select(Sprint))
    result = []
    for s in sprints:
        tasks = await session.exec(
            select(Task).where(Task.sprint_id == s.id))
        for t in tasks:
            t.owner = await session.get(User, t.owner_id)
 # N+1 queries, fragile dict construction
Declarative DTO + auto-loading
class UserDTO(DefineSubset):
    __subset__ = (User, ("id", "name"))

class TaskDTO(DefineSubset):
    __subset__ = (Task, ("id", "title", "owner_id"))
    owner: UserDTO | None = None   # auto-loaded

class SprintDTO(DefineSubset):
    __subset__ = (Sprint, ("id", "name"))
    tasks: list[TaskDTO] = []      # auto-loaded

 # 1 query per relationship, zero N+1

Everything you need, from model to production

ER diagrams, GraphQL, REST DTOs, and MCP — all driven by SQLModel entities.

ER Diagram

SQLModel entities + non-ORM relationships, visualized as Mermaid ER diagrams.

GraphQL Auto-Generation

@query / @mutation decorators auto-generate SDL with DataLoader batch loading.

Core API DTOs

DefineSubset + implicit auto-loading — declarative REST response construction.

Derived Fields & Cross-Layer

post_* for aggregations, ExposeAs / SendTo for cross-layer data flow.

MCP Integration

Expose your SQLModel APIs to AI agents via Model Context Protocol.

RPC Services

Business logic as RpcService — serve via MCP and FastAPI from one definition.

Progressive development path

Start from entities, extend as needed. Each stage builds on the last.

1 SQLModel Entities
2 ER Diagram
3 GraphQL API
4 Core API DTOs
5 MCP / RPC

Built for your stack

Works with your existing frameworks and tools.

Ready to go from model to API?

Start with a single @query decorator. Scale to full-stack when you're ready.