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