You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 00:25:26