ardeois / graphql-codegen-typescript-mock-data

[GraphQL Codegen Plugin](https://github.com/dotansimha/graphql-code-generator) for building mock data based on the schema.
MIT License
132 stars 47 forks source link

issue with enum transformation #116

Closed Benoit-ROBIN closed 1 year ago

Benoit-ROBIN commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

ardeois commented 1 year ago

@Benoit-ROBIN The bug should be fixed now with PR https://github.com/ardeois/graphql-codegen-typescript-mock-data/pull/120 Version > 3.3.0

Can you confirm?

Benoit-ROBIN commented 1 year ago

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