import strawberry from typing import Optional from strawberry.types import Info from app.graphql.types import UserType from app.models import User from app.graphql.context import CustomContext @strawberry.type class Query: @strawberry.field async def me(self, info: Info[CustomContext, None]) -> Optional[UserType]: # 从 context 获取当前登录用户 user = await info.context.get_current_user() # Strawberry 会自动将 SQLAlchemy 实例映射为 UserType return user @strawberry.field def get_user_by_handle(self, info: Info[CustomContext, None], handle: str) -> Optional[UserType]: db = info.context.db return db.query(User).filter(User.handle == handle).first()