Skip to content

GraphQL unions break when using class prefixes #2925

@siminn-arnorgj

Description

@siminn-arnorgj

Describe the bug

When using the --class-name-prefix option with GraphQL, type unions are broken.

To Reproduce

type Apple {
  ripe: Boolean!
}

type Orange {
  size: Float!
}

union Fruit = Apple | Orange

type FruitBasket {
  fruits: [Fruit!]!
}

Used commandline:

$ datamodel-codegen --input ./apples.graphql --input-file-type graphql --class-name-prefix ABC

Current behavior

class ABCApple(BaseModel):
    ripe: ABCBoolean
    typename__: Literal['Apple'] | None = Field('Apple', alias='__typename')


class ABCOrange(BaseModel):
    size: ABCFloat
    typename__: Literal['Orange'] | None = Field('Orange', alias='__typename')


ABCFruit: TypeAlias = Union[
    'Apple',
    'Orange',
]


class ABCFruitBasket(BaseModel):
    fruits: list[ABCFruit]
    typename__: Literal['FruitBasket'] | None = Field('FruitBasket', alias='__typename')

Expected behavior

The union type should reference the prefixed class names:

Version:

  • OS: MacOS 26.2
  • Python version: 3.13.9
  • datamodel-code-generator version: 507c33f

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions