strawberry-graphql / strawberry-django

Strawberry GraphQL Django extension
https://strawberry.rocks/docs/django
MIT License
411 stars 118 forks source link

Getting batch create mutations to work #336

Open tjni opened 1 year ago

tjni commented 1 year ago

Describe the Bug

In my app, I would like to have something like:

@strawberry.django.input(Criterion)
class CriterionInput:
  ...

@strawberry.type
class Mutation:
  create_criterion: List[Criterion] = strawberry.django.mutations.create(CriterionInput, handle_django_errors=True)

This generates a mutation with schema:

createCriterion(data: CriterionInput!): CreateCriterionPayload!

I expect this to have a schema of:

createCriterion(data: [CriterionInput!]!): [CreateCriterionPayload!]!

because if handle_django_errors is removed, the mutation has schema:

createCriterion(data: [CriterionInput!]!): [Criterion!]!

I'm able to get the behavior I want by wrapping payload union in DjangoMutationBase#resolve_type with StrawberryList if the original resolved type is an instance of StrawberryList, but I'm not certain it's the correct solution.

I'm still quite new to Strawberry and GraphQL, so apologies in advance if I'm using the API incorrectly.

Upvote & Fund

Fund with Polar

bellini666 commented 1 year ago

You are using the API correctly, the issue is that handle_django_errors=True is not handling batch creations atm.

I'll try to implement that soon :)