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

Setting the enumValues and typenames to keep is not working and the value is always in pascal-case #102

Closed zshlomyz closed 1 year ago

zshlomyz commented 1 year ago

Hi, My config:

overwrite: true
schema: "../../server/api/graphql/schemas/*.graphqls"
documents: "src/**/*.graphql"
generates:
  src/generated/graphql.ts:
    plugins:
      - "typescript"
      - "typescript-operations"
      - "typescript-apollo-angular":
          addExplicitOverride: true
  src/generated/mocks.ts:
    plugins:
      - typescript-mock-data:
          typesFile: './graphql.ts'
          terminateCircularRelationships: true
          listElementCount: 5
          enumValues: keep
          typenames: keep
          scalars:
            AWSTimestamp: unix_time # gets translated to casual.unix_time

As u can see, I've settled the typenames and enumValues to keep, but yet, the mock data is generated using a pascal-case like this - MyEnum.MY_ENUM_VAL instead of MyEnum.MyEnumKey when my enum looks like that:

export enum MyEnum { MyEnumKey: MY_ENUM_VAL }

I suspect that it related to the issue that the generated value use the value of the enum instead of the key, as u can see in this line - https://github.com/ardeois/graphql-codegen-typescript-mock-data/blob/2521deac47f7974b5af9241c1ca5e3c8e266115a/src/index.ts#L50 Am I correct? should I create a PR to fix that? Thanks

ardeois commented 1 year ago

Sorry for the late reply @zshlomyz So I've looked into it and enumValues actually mean enumKeys. Values are set by the typescript generated file because we reference it. This is equivalent to enumValues config from nameConvention in typescript plugin config

Assuming we have the following schema:

enum MyEnum {
  MY_ENUM_VAL
}

type SomeType {
  enum: MyEnum
}

and the following config:

overwrite: true
schema: schema.graphql
documents:
  - 'src/**/*.ts'
  - 'src/**/*.tsx'
  - '!src/**/generated/*'
generates:
  ./src/types/generated/graphql.ts:
    plugins:
      - 'typescript'
      - 'typescript-operations'
    config:
      namingConvention:
        enumValues: keep # <-- equivalent of 'enumValues: keep' in 'typescript-mock-data' plugin
  src/mocks/generated/graphql.ts:
    plugins:
      - typescript-mock-data:
          enumValues: keep
          typesFile: '../../types/generated/graphql.ts'

Here are the generated files I've got: Types:

export enum MyEnum {
    MY_ENUM_VAL = 'MY_ENUM_VAL',
}

export type SomeType = {
    __typename?: 'SomeType';
    enum?: Maybe<MyEnum>;
};

Mock:


export const aSomeType = (overrides?: Partial<SomeType>): SomeType => {
    return {
        enum: overrides && overrides.hasOwnProperty('enum') ? overrides.enum! : MyEnum.MY_ENUM_VAL,
    };
};

As you can see the mock file uses MyEnum.MY_ENUM_VAL to keep the same behaviour as typescript plugin. If you want pascal case, just remove enumValues config it's the default (or set it to pascal-case#pascalCase)

zshlomyz commented 1 year ago

Thanks @ardeois !!

ardeois commented 1 year ago

Fixes in version 2.7.0