fullfill graphql usertype; change db session to dep injection
This commit is contained in:
@@ -21,6 +21,19 @@ if not DATABASE_URL:
|
||||
engine = create_engine(
|
||||
DATABASE_URL,
|
||||
pool_recycle=3600,
|
||||
pool_pre_ping=True
|
||||
pool_pre_ping=True,
|
||||
connect_args={
|
||||
"keepalives": 1,
|
||||
"keepalives_idle": 30,
|
||||
"keepalives_interval": 10,
|
||||
"keepalives_count": 5
|
||||
}
|
||||
)
|
||||
SessionLocal = sessionmaker(bind=engine)
|
||||
SessionLocal = sessionmaker(bind=engine)
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
@@ -11,7 +11,7 @@ class Query:
|
||||
@strawberry.field
|
||||
async def me(self, info: Info[CustomContext, None]) -> Optional[UserType]:
|
||||
# 从 context 获取当前登录用户
|
||||
user = await info.context.get_current_user()
|
||||
user = await info.context. xzz()
|
||||
# Strawberry 会自动将 SQLAlchemy 实例映射为 UserType
|
||||
return user
|
||||
|
||||
|
||||
+22
-4
@@ -4,17 +4,35 @@ from datetime import datetime
|
||||
from strawberry.types import Info
|
||||
|
||||
from app.utils.format import get_image_url
|
||||
from app.graphql.context import CustomContext
|
||||
|
||||
@strawberry.type
|
||||
class UserType:
|
||||
id: strawberry.ID
|
||||
id: str
|
||||
email: str
|
||||
name: Optional[str]
|
||||
name: str
|
||||
avatar: str
|
||||
handle: Optional[str]
|
||||
avatar: Optional[str]
|
||||
|
||||
# 互动统计字段
|
||||
following_count: int
|
||||
follower_count: int
|
||||
|
||||
# 统计汇总字段
|
||||
owner_project_count: int
|
||||
owner_likes_count: int
|
||||
owner_favorites_count: int
|
||||
owner_shares_count: int
|
||||
|
||||
admin_project_count: int
|
||||
admin_likes_count: int
|
||||
admin_favorites_count: int
|
||||
admin_shares_count: int
|
||||
|
||||
user_project_count: int
|
||||
user_likes_count: int
|
||||
user_favorites_count: int
|
||||
user_shares_count: int
|
||||
|
||||
created_at: datetime
|
||||
|
||||
# 动态解析字段:例如自动处理头像的绝对路径
|
||||
|
||||
Reference in New Issue
Block a user