pythondjangographqlstrawberry-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 = ....)

Solution

  • In Strawberry GraphQL you can combine queries and mutations by extending them, for example you can do the following:

    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