Skip to content

[typescript-resolvers] Enums ValueMaps don't get generated anymore #3619

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

Open
Tracked by #8296 ...
darkbasic opened this issue Mar 10, 2020 · 5 comments
Open
Tracked by #8296 ...

[typescript-resolvers] Enums ValueMaps don't get generated anymore #3619

darkbasic opened this issue Mar 10, 2020 · 5 comments
Assignees
Labels
core Related to codegen core/cli kind/enhancement New feature or request plugins

Comments

@darkbasic
Copy link
Contributor

Apollo allows you to customize the internal values of your GraphQL enums: https://www.apollographql.com/docs/apollo-server/schema/scalars-enums/

That means that in you resolvers you will do something like this:

const resolvers: Resolvers = {
  Sex: {
    MALE: 0,
    FEMALE: 1,
  },
};

Unfortunately that will break the generated types because in Resolvers there will be no property set for the enum field.

Older versions of the codegen generated a ValueMap for each enum so you could do something like this:

type EnumResolvers = {
  Sex?: SexValueMap;
};

const resolvers: Resolvers & EnumResolvers = {
  Sex: {
    MALE: 0,
    FEMALE: 1,
  },
};

where the ValueMap was something like this:

type SexValueMap = {
  MALE: Sex,
  FEMALE: Sex,
};

Since codegen v1 this doesn't work anymore.

It would be nice if support for enums with custom internal values would be re-introduced, but in my opinion it should be done in a slightly different way:

  1. The ValueMap should map to the exact value:
type SexValueMap = {
  MALE: Sex.MALE,
  FEMALE: Sex.FEMALE,
};
  1. The value map should be automatically added to the Resolvers type, in order to avoid doing the following:
type EnumResolvers = {
  Sex?: SexValueMap;
};

const resolvers: Resolvers & EnumResolvers = {}
@dotansimha
Copy link
Owner

Thank you for reporting this @darkbasic .
@kamilkisiela what do you think? I think we can add it to the signature, shouldn't be too complicated.

@dotansimha dotansimha changed the title [Regression] enums ValueMaps don't get generated anymore [typescript-resolvers][Regression] enums ValueMaps don't get generated anymore Mar 11, 2020
@dotansimha dotansimha changed the title [typescript-resolvers][Regression] enums ValueMaps don't get generated anymore [typescript-resolvers] Enums ValueMaps don't get generated anymore Mar 11, 2020
@dotansimha dotansimha added kind/enhancement New feature or request and removed enhancement labels Jun 20, 2021
@charlypoly charlypoly added the core Related to codegen core/cli label Nov 3, 2022
@Javadebi
Copy link

Javadebi commented Mar 1, 2023

Is there any solution to fix it?

@Javadebi
Copy link

Javadebi commented Mar 1, 2023

Another problem I have is when i'm trying to use the variable in for example if. sex: Sex
and I say if(sex === 'male') gives type error because in generated schema its defined sex === 'MALE'
is there any solution to this?

@darkbasic
Copy link
Contributor Author

darkbasic commented Mar 2, 2023

You want to use if (sex === Sex.MALE) otherwise you will lose most of the benefits anyway.

@Javadebi
Copy link

Javadebi commented Mar 6, 2023

You want to use if (sex === Sex.MALE) otherwise you will lose most of the benefits anyway.

Exactly right now I'm asserting type to values for example I'm saying if(sex === ('male' as Sex)) and I know its very bad but i don't have any other solution

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

No branches or pull requests

5 participants