Typescript codegen from graphql schema has types issues

Following the steps outlined here: How To Generate TypeScript Types From GraphQL - DatoCMS I have set up a codegen script to generate a types file. That’s all working nicely!

However, some of the generated types have some issues:

export enum CategoryModelOrderBy {
  CreatedAtAsc = '_createdAt_ASC',
  CreatedAtDesc = '_createdAt_DESC',
  FirstPublishedAtAsc = '_firstPublishedAt_ASC',
  FirstPublishedAtDesc = '_firstPublishedAt_DESC',
  IsValidAsc = '_isValid_ASC',
  IsValidDesc = '_isValid_DESC',
  PublicationScheduledAtAsc = '_publicationScheduledAt_ASC',
  PublicationScheduledAtDesc = '_publicationScheduledAt_DESC',
  PublishedAtAsc = '_publishedAt_ASC',
  PublishedAtDesc = '_publishedAt_DESC',
  StatusAsc = '_status_ASC',
  StatusDesc = '_status_DESC',
  UnpublishingScheduledAtAsc = '_unpublishingScheduledAt_ASC',
  UnpublishingScheduledAtDesc = '_unpublishingScheduledAt_DESC',
  UpdatedAtAsc = '_updatedAt_ASC',
  UpdatedAtDesc = '_updatedAt_DESC',
  CategoryIdAsc = 'categoryId_ASC',
  CategoryIdDesc = 'categoryId_DESC',
  CreatedAtAsc = 'createdAt_ASC',
  CreatedAtDesc = 'createdAt_DESC',
  IdAsc = 'id_ASC',
  IdDesc = 'id_DESC',
  TitleAsc = 'title_ASC',
  TitleDesc = 'title_DESC',
  UpdatedAtAsc = 'updatedAt_ASC',
  UpdatedAtDesc = 'updatedAt_DESC'
}

As you can see there are multiple enum keys for UpdatedAtAsc (and the other system fields), with multiple possible values, so this may have unpredictable behaviour.

1 Like

I’ve found a solution for this, which involves setting a custom naming function - Duplicate identifier on Typescript type generation due to _ prefix · Issue #6925 · dotansimha/graphql-code-generator · GitHub

Using this, I can get the generator to produce the following types output:

export enum CategoryModelOrderBy {
  _CreatedAtAsc = '_createdAt_ASC',
  _CreatedAtDesc = '_createdAt_DESC',
  _FirstPublishedAtAsc = '_firstPublishedAt_ASC',
  _FirstPublishedAtDesc = '_firstPublishedAt_DESC',
  _IsValidAsc = '_isValid_ASC',
  _IsValidDesc = '_isValid_DESC',
  _PublicationScheduledAtAsc = '_publicationScheduledAt_ASC',
  _PublicationScheduledAtDesc = '_publicationScheduledAt_DESC',
  _PublishedAtAsc = '_publishedAt_ASC',
  _PublishedAtDesc = '_publishedAt_DESC',
  _StatusAsc = '_status_ASC',
  _StatusDesc = '_status_DESC',
  _UnpublishingScheduledAtAsc = '_unpublishingScheduledAt_ASC',
  _UnpublishingScheduledAtDesc = '_unpublishingScheduledAt_DESC',
  _UpdatedAtAsc = '_updatedAt_ASC',
  _UpdatedAtDesc = '_updatedAt_DESC',
  CategoryIdAsc = 'categoryId_ASC',
  CategoryIdDesc = 'categoryId_DESC',
  CreatedAtAsc = 'createdAt_ASC',
  CreatedAtDesc = 'createdAt_DESC',
  IdAsc = 'id_ASC',
  IdDesc = 'id_DESC',
  TitleAsc = 'title_ASC',
  TitleDesc = 'title_DESC',
  UpdatedAtAsc = 'updatedAt_ASC',
  UpdatedAtDesc = 'updatedAt_DESC'
}
1 Like