Skip to content

Default variant for enums generated by fragment (union / interface types) #372

Closed
@visd0m

Description

@visd0m

I was wondering if this would be an appreciated feature.

I try to describe a real scenario in order to be clear 🤞 .

Given a query like this:

query {
    myBeautifulQuery {
        __typename
        ... on Foo {
            fooField
        }
        ... on Bar {
            barField
        }
    }
}

and a graphql schema like this:

union MyBeautifulQueryResponse =  Foo | Bar

The generated enum representing myBeautifulQuery Response looks like this:

#[serde(tag = "__typename")]
pub enum CanChangeSaveEffectiveDateSaveOnSaveActions {
    Foo(FooData),
    Bar(BarData),
}

If the enum covers completely the union variants everything is fine.

The problem arises if a variant is added to the union type declared in the schema and the types are not re-generated.

If a new variant is added, the enum is not correctly deserialized and a deserialization error occurs.

Speaking in terms of correctness, probably, the fact that an error occurs is fine.
Anyway sometimes a safer approach can be preferred.

Adding a default variant to the generated enum would avoid the deserialization error in case of new variants of MyBeautifulQueryResponse.

#[serde(tag = "__typename")]
pub enum CanChangeSaveEffectiveDateSaveOnSaveActions {
    Foo(FooData),
    Bar(BarData),
    #[serde(other)]
    SomethingElse
}

This ☝️ would avoid the deserialization error making possible to handle the unknown variant as desired.

It would be nice to be able to trigger this safer version of the generated code.

If this seems a desirable feature I (or some colleagues of mine) could work on it

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions