Skip to content
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

issue with enum transformation #116

Closed
Benoit-ROBIN opened this issue Mar 7, 2023 · 5 comments
Closed

issue with enum transformation #116

Benoit-ROBIN opened this issue Mar 7, 2023 · 5 comments

Comments

@Benoit-ROBIN
Copy link

Hi
I tried to have my enum generated in pascalCase (key and value), but in my case only the keys are transformed to pascalCase, the value stay in snakeCase.
I have seen this pr and I thought maybe it was overwritten accidentally ?

here my config

overwrite: true
schema: ${BACKEND_URL}
documents: 'src/**/*.{ts,tsx}'
generates:
  src/types/generated-ops.ts:
    plugins:
      - 'typescript'
      - 'typescript-operations'
      - 'typescript-react-apollo'
  src/types/fragmentTypes.json:
    plugins:
      - 'fragment-matcher'
  src/__tests__/generated-mocks.ts:
    plugins:
      - typescript-mock-data:
    config:
      typesFile: '../types/generated-ops.ts'
      generateLibrary: 'faker'
      typeNames: 'change-case-all#pascalCase'
      enumValues: 'change-case-all#pascalCase'
      transformUnderscore: false
      terminateCircularRelationships: true

I have set transformUnderscore to false because I need to keep underscore on my type's name.

@ardeois
Copy link
Owner

ardeois commented Mar 7, 2023

I'm not sure to follow.
The values are not changed because we only use enum keys.
What does the generated type looks like?

@Benoit-ROBIN
Copy link
Author

Pardon my english,
I need to set transformUnderscore to false because I have underscore in my type's name.
Because of this, the enum generated also keep underscore in key name.

For example, with the plugin typescript

enum PassPunchKind {
  MAXIMUM_DAILY_PUNCHES,
  MAXIMUM_MONTHLY_PUNCHES,
  MAXIMUM_MONTHLY_PUNCHES_ON_FIRM,
  MAXIMUM_MONTHLY_PUNCHES_ON_GYM
}

will be transform to

enum PassPunchKind {
  MaximumDailyPunches = 'MAXIMUM_DAILY_PUNCHES',
  MaximumMonthlyPunches = 'MAXIMUM_MONTHLY_PUNCHES',
  MaximumMonthlyPunchesOnFirm = 'MAXIMUM_MONTHLY_PUNCHES_ON_FIRM',
  MaximumMonthlyPunchesOnGym = 'MAXIMUM_MONTHLY_PUNCHES_ON_GYM'
}

but with typescript-mock-data the same enum will be used as

export const aPassPunchLimitation = (
  overrides?: Partial<PassPunchLimitation>,
  _relationshipsToOmit: Array<string> = [],
): PassPunchLimitation => {
  const relationshipsToOmit = [..._relationshipsToOmit, 'PassPunchLimitation'];
  return {
    isReached:
      overrides && overrides.hasOwnProperty('isReached')
        ? overrides.isReached!
        : true,
    kind:
      overrides && overrides.hasOwnProperty('kind')
        ? overrides.kind!
        : PassPunchKind.Maximum_Daily_Punches, // <<<<<------   HERE
    nextAvailabilityDate:
      overrides && overrides.hasOwnProperty('nextAvailabilityDate')
        ? overrides.nextAvailabilityDate!
        : 'alias',
    threshold:
      overrides && overrides.hasOwnProperty('threshold')
        ? overrides.threshold!
        : 6720,
  };
};

which will cause error because Maximum_Daily_Punches doesn't exist on PassPunchKind. Instead it should be PassPunchKind.MaximumDailyPunches.
I tried to force true as second parameter for createNameConverter on this line as in this PR and I have the result expected. So I thought maybe you should use another configuration parameter instead of transformUnderscore for the enum.

@ardeois
Copy link
Owner

ardeois commented Mar 15, 2023

I see, it seems like the PR you are mentionning fixes one issue but adds a new one.

@ardeois
Copy link
Owner

ardeois commented Mar 30, 2023

@Benoit-ROBIN The bug should be fixed now with PR #120
Version > 3.3.0

Can you confirm?

@ardeois ardeois closed this as completed Mar 30, 2023
@Benoit-ROBIN
Copy link
Author

@ardeois sorry for the response delay, it's all good, no errors. Thank you for your work 🙏 ❤️

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

No branches or pull requests

2 participants