SQLModel entities drive GraphQL API, REST DTOs, and MCP services — one model, many presentations.
pip install nexusx
For development exploration — rapidly validate entity relationships and data shapes.
# 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
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
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.