2

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 = ....)
patrick
  • 6,533
  • 7
  • 45
  • 66
Kirill
  • 43
  • 4

2 Answers2

3

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

patrick
  • 6,533
  • 7
  • 45
  • 66
1

To combine multiple mutations, create one class Mutation and give it multiple methods, each one annotated with @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:

@strawberry.type
class Query:
    name: str
    stock: int
David S.
  • 457
  • 2
  • 13