strawberry-graphql/strawberry-django

Cannot return Interface from mutation

Aberta

#797 aberto em 13 de out. de 2025

 (1 comentário) (0 reação) (0 responsável)Python (152 forks)auto 404
enhancementgood first issuehelp wanted

Métricas do repositório

Stars
 (496 estrelas)
Métricas de merge de PR
 (Métricas PR pendentes)

Description

Feature Request Type

  • Core functionality
  • Alteration (enhancement/optimization) of existing feature(s)
  • New behavior

Description

When returning an interface from a mutation like so -

@strawberry.type
class ProjectsMutation:
    """A collection of project-related GraphQL mutations."""

    update_external_project: ExternalProject = mutations.update_external_project(
        description="Updates an external project."
    )

    # ...

    # NB: The below mutations act on the `Project` interface. Since the mutations already return a union
    #     with `OperationInfo`, and interfaces are prohibited in unions, we must expand the full `Project` type.

    update_project: Project = mutations.update_project(
        description="Updates a generic project."
    )

    # ...

This raises an error that Interfaces cannot form part of a union.

https://spec.graphql.org/September2025/#sec-Unions.Type-Validation "The member types of a Union type must all be Object base types; Scalar, Interface and Union types must not be > member types of a Union. Similarly, wrapping types must not be member types of a Union."

I can see that the union.types both Project | OperationInfo, but since Project is an interface, this assertion fails.

Workaround

Explicitly type the mutation with concrete types rather than interface, e.g.

update_project: WebProject | ExternalProject | ... = mutations.update_project(
    description="Updates a generic project."
)

Solution

Detect when an interface is being unioned with the OperationInfo here: https://github.com/strawberry-graphql/strawberry-django/blob/4c7a47de32390d434e8b04e919449216183bb22d/strawberry_django/mutations/fields.py#L143-L155

And automatically spread any Interfaces in types_.

tl;dr - replace interface Project with its concrete types, e.g. WebProject | ExternalProject | ... | OperationInfo.

Guia do colaborador