techedemic
techedemic

Reputation: 351

Graphql Codegen - Enum with custom key => value

I use graphql-codegen to generate type files.

As an example, let's say I have the following in my schema.graphql file:

enum cities {
 JOHANNESBURG
 CAIRO
 NEW_YORK
 LONDON
 BEIJING
}

The output in my generated-types.ts file is as follows:

export enum cities {
 Johannesburg = 'JOHANNESBURG'
 Cairo = 'CAIRO'
 NewYork = 'NEW_YORK'
 London = 'LONDON'
 Beijing = 'BEIJING'
}

Is there a way for me to 'override' the value of the enum before codegen runs? Perhaps something as follows (which I obviously tried):

enum cities {
 JOHANNESBURG: 'JNB'
 CAIRO: 'CAI'
 NEW_YORK: 'NYC'
 LONDON: 'LON'
 BEIJING: 'BEI'
}

which in turn should produce:

export enum cities {
 Johannesburg = 'JNB'
 Cairo = 'CAI'
 NewYork = 'NYC'
 London = 'LON'
 Beijing = 'BEI'
}

Upvotes: 3

Views: 7614

Answers (1)

Ewe Tek Min
Ewe Tek Min

Reputation: 865

not sure if I'm too late, but you probably can use the enumValues to customize your internal enum values. Here is the documentation. For example:

./types.ts

export enum cities {
 Johannesburg = 'JNB'
 Cairo = 'CAI'
 NewYork = 'NYC'
 London = 'LON'
 Beijing = 'BEI'
}

./codegen.yml

generates:
  src/graphql.types.ts:
    config:
      useIndexSignature: true
      enumValues:
        cities: ./types#cities // path to your custom types
    plugins:
      - typescript
      - typescript-resolvers

Upvotes: 7

Related Questions