Skip to content
Open Source · SQLModel · Python 3.10+

From Model to API, Progressively

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

pip install sqlmodel-nexus

SQLModel + @query = GraphQL API

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

Without sqlmodel-nexus
# Write SDL by hand
type Query {
  posts(limit: Int): [Post]!
}
type Post {
  id: Int!
  title: String!
  author: User
}
# + resolver + DataLoader + N+1 fix
 # Dozens of lines of boilerplate
With sqlmodel-nexus
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 + DataLoader auto-generated!

DefineSubset + implicit auto-loading = REST DTO

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.