SQLModel entities drive GraphQL API, REST DTOs, and MCP services — one model, many presentations.
pip install sqlmodel-nexus
For development exploration — rapidly validate entity relationships and data shapes.
# 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
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!
For production data delivery — build stable REST endpoints with typed DTOs.
# 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
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
ER diagrams, GraphQL, REST DTOs, and MCP — all driven by SQLModel entities.
SQLModel entities + non-ORM relationships, visualized as Mermaid ER diagrams.
@query / @mutation decorators auto-generate SDL with DataLoader batch loading.
DefineSubset + implicit auto-loading — declarative REST response construction.
post_* for aggregations, ExposeAs / SendTo for cross-layer data flow.
Expose your SQLModel APIs to AI agents via Model Context Protocol.
Business logic as RpcService — serve via MCP and FastAPI from one definition.
Start from entities, extend as needed. Each stage builds on the last.
Works with your existing frameworks and tools.
Start with a single @query decorator. Scale to full-stack when you're ready.