如何在Strawberry GraphQL中合并多个Mutation?
合并Strawberry GraphQL中的多个Mutation并配置Schema
你只需要把多个Mutation类里的方法整合到同一个@strawberry.type装饰的类中,再将这个类传入Schema的mutations参数即可,具体操作如下:
1. 合并Mutation方法
将原来Mutation1和Mutation2中的mutation方法迁移到同一个类里:
@strawberry.type class Mutation: @strawberry.mutation def login(self, email: str, password: str) -> LoginResult: # 在此编写login的业务逻辑 pass @strawberry.mutation def create_post(self, title: str, text: str) -> CreatePostResult: # 在此编写create_post的业务逻辑 pass
2. 配置Schema
直接将合并后的Mutation类赋值给strawberry.Schema的mutations参数:
schema = strawberry.Schema(mutations=Mutation)
Strawberry会自动识别这个类中所有被@strawberry.mutation装饰的方法,将它们作为Schema中的mutation操作。
内容的提问来源于stack exchange,提问作者Kirill
相关产品推荐
相关产品推荐

