Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
visd0m opened this issue Jun 26, 2021 · 4 comments
Closed
Labels
enhancement New feature or request

Comments

@visd0m
Copy link
Contributor

visd0m commented Jun 26, 2021

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

@tomhoule
Copy link
Member

Yes I think it would be valuable. I don't know if you are using the latest released version or the main github branch, but there are substantial differences between the two. A new stable release will go out soon and I can't remember if it touched that part of the code, but I'd assume not. So yes, a PR would be welcome :)

@tomhoule tomhoule added the enhancement New feature or request label Jun 26, 2021
@tomhoule
Copy link
Member

There's the question of whether it should be configurable, and if yes what the default should be. I'm not sure.

@visd0m
Copy link
Contributor Author

visd0m commented Jun 26, 2021

Yes I think it would be valuable. I don't know if you are using the latest released version or the main github branch, but there are substantial differences between the two. A new stable release will go out soon and I can't remember if it touched that part of the code, but I'd assume not. So yes, a PR would be welcome :)

Yes I am aware of the differences 👍 .

Glad to hear about the new release!

So I guess we'll work on it in the next days.

@dehann
Copy link

dehann commented Feb 12, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants