strawberry-graphql/strawberry-django

Cannot return Interface from mutation

开放

#797 创建于 2025年10月13日

 (1 条评论) (0 个反应) (0 位负责人)Python (152 个派生)auto 404
enhancementgood first issuehelp wanted

仓库指标

星标
 (496 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

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.

贡献者指南