简体   繁体   English

Сombine 突变在草莓 Graphql

[英]Сombine mutations in one in Strawberry Graphql

How to combine mutations in one?如何将突变合二为一?

@strawberry.type
class Mutation1:
    @strawberry.mutation
    def login(self, email: str, password: str) -> LoginResult:
@strawberry.type
class Mutation2:
    @strawberry.mutation
    def create_post(self, title: str, text: str) -> CreatePostResult:
schema = strawberry.Schema(mutations = ....)

To combine multiple mutations, create one class Mutation and give it multiple methods, each one annotated with @strawberry.mutation :要组合多个突变,请创建一个class Mutation并为其提供多种方法,每种方法都使用@strawberry.mutation进行注释:

@strawberry.type 
class Mutation: 
    @strawberry.mutation 
    def login(self, email: str, password: str) -> LoginResult:
        pass
    @strawberry.mutation 
    def create_post(self, title: str, text: str) -> CreatePostResult:
        pass
# Finally register both Mutation and Query (if defined) to the schema:
schema = strawberry.Schema(query=Query, mutation=Mutation)

Similarly, to combine multiple queries, create one class Query and give it multiple fields :同样,要组合多个查询,请创建一个class Query并为其提供多个fields

@strawberry.type
class Query:
    name: str
    stock: int

In Strawberry GraphQL you can combine queries and mutations by extending them, for example you can do the following:Strawberry GraphQL 中,您可以通过扩展查询和突变来组合它们,例如,您可以执行以下操作:

import strawberry



@strawberry.type
class Query:
    hi: str 


@strawberry.type
class Mutation1:
    @strawberry.mutation
    def login(self, email: str, password: str) -> str:
        return "hi"

@strawberry.type
class Mutation2:
    @strawberry.mutation
    def create_post(self, title: str, text: str) -> str:
        ...

@strawberry.type
class Mutation(Mutation1, Mutation2):
    ...

schema = strawberry.Schema(query=Query, mutation=Mutation)

Here's the example in the playground: https://play.strawberry.rocks/?gist=a9117f949453d980f3d88bf88462fd30这是操场上的示例: https://play.strawberry.rocks/?gist=a9117f949453d980f3d88bf88462fd30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM