-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathquickstart.py
More file actions
82 lines (58 loc) · 1.97 KB
/
Copy pathquickstart.py
File metadata and controls
82 lines (58 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from pydantic import BaseModel
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from sqlalchemy.pool import StaticPool
from sqlmodel import Field, Relationship, SQLModel
from sqlmodel.ext.asyncio.session import AsyncSession
from nexusx import AutoQueryConfig, GraphQLHandler
class BaseEntity(SQLModel):
pass
class Team(BaseEntity, table=True):
id: int | None = Field(default=None, primary_key=True)
name: str
heroes: list["Hero"] = Relationship(back_populates="team")
class Hero(BaseEntity, table=True):
id: int | None = Field(default=None, primary_key=True)
name: str
team_id: int | None = Field(default=None, foreign_key="team.id")
team: Team | None = Relationship(back_populates="heroes")
engine = create_async_engine(
"sqlite+aiosqlite:///:memory:",
poolclass=StaticPool,
)
session_factory = async_sessionmaker(
engine,
class_=AsyncSession,
expire_on_commit=False,
)
handler = GraphQLHandler(
base=BaseEntity,
session_factory=session_factory,
auto_query_config=AutoQueryConfig(),
)
@asynccontextmanager
async def lifespan(_app: FastAPI):
async with engine.begin() as connection:
await connection.run_sync(SQLModel.metadata.create_all)
async with session_factory() as session:
team = Team(name="Avengers")
session.add(team)
await session.flush()
session.add(Hero(name="Spider-Man", team_id=team.id))
await session.commit()
try:
yield
finally:
await handler.aclose()
await engine.dispose()
app = FastAPI(lifespan=lifespan)
class GraphQLRequest(BaseModel):
query: str
@app.get("/graphql", response_class=HTMLResponse)
async def graphiql() -> str:
return handler.get_graphiql_html()
@app.post("/graphql")
async def graphql(request: GraphQLRequest):
return await handler.execute(request.query)