strawberry-graphql/strawberry-django

Cannot return Interface from mutation

Aperta

#797 aperta il 13 ott 2025

 (1 commento) (0 reazioni) (0 assegnatari)Python (152 fork)auto 404
enhancementgood first issuehelp wanted

Metriche repository

Star
 (496 stelle)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

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.

Guida contributor