question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Duplicate identifier on Typescript type generation due to _ prefix

See original GitHub issue

Describe the bug On generating GraphQL schema (using Typescript + Typescript-Operations plugins), duplicate fields are generated: TS2300: Duplicate identifier error

To Reproduce Having a graphql endpoint and then run the command yarn graphql-codegen --config codegen.yml -r dotenv/config (or using npm)

The bug happen when a GraphQL schema has key like _createdAt_ASC and createdAt_ASC, the _ is ignore and the generated type definition create two createdAt_ASC

  1. My GraphQL schema:
_createdAt_ASC
_createdAt_DESC
createdAt_ASC
createdAt_DESC
id_ASC
id_DESC
_firstPublishedAt_ASC
_firstPublishedAt_DESC
_publicationScheduledAt_ASC
_publicationScheduledAt_DESC
_unpublishingScheduledAt_ASC
_unpublishingScheduledAt_DESC
_publishedAt_ASC
_publishedAt_DESC
_status_ASC
_status_DESC
_updatedAt_ASC
_updatedAt_DESC
updatedAt_ASC
updatedAt_DESC
_isValid_ASC
_isValid_DESC
title_ASC
title_DESC
  1. My codegen.yml config file:
overwrite: true

schema:
  - '${GRAPHQL_API_ENDPOINT}':
      headers:
        Authorization: 'Bearer ${GRAPHQL_API_TOKEN}'

documents: core/queries/**/*.ts
generates:
  core/queries/schema.ts:
    plugins:
      - 'typescript'
      - 'typescript-operations'
  1. My schema output:
export enum ArticleModelOrderBy {
  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',
  CreatedAtAsc = 'createdAt_ASC', // duplicate
  CreatedAtDesc = 'createdAt_DESC',  // duplicate
  IdAsc = 'id_ASC',
  IdDesc = 'id_DESC',
  TitleAsc = 'title_ASC',
  TitleDesc = 'title_DESC',
  UpdatedAtAsc = 'updatedAt_ASC',  // duplicate
  UpdatedAtDesc = 'updatedAt_DESC'  // duplicate
}

Expected behavior

Environment: Darwin Majdis-MacBook-Pro.local 20.6.0 Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:21 PDT 2021; root:xnu-7195.141.6~3/RELEASE_X86_64 x86_64

Additional context

Thanks ✌️

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
ristomatticommented, Dec 1, 2021

I played around with the different naming conventions. From the ones I tried, this issue occurs with pascalCase and camelCase but not with titleCase, upperCase or upperCaseFirst. For this particular use case using upperCaseFirst only for enums gives a good enough workaround for me.

Configuration:

generates:
  ./models/datocms/index.ts:
    plugins:
      - typescript
    config:
      namingConvention:
        enumValues: change-case-all#upperCaseFirst

Output:

export enum PageModelOrderBy {
  _createdAt_ASC = '_createdAt_ASC',
  _createdAt_DESC = '_createdAt_DESC',
  _firstPublishedAt_ASC = '_firstPublishedAt_ASC',
  _firstPublishedAt_DESC = '_firstPublishedAt_DESC',
  _isValid_ASC = '_isValid_ASC',
  _isValid_DESC = '_isValid_DESC',
  _publicationScheduledAt_ASC = '_publicationScheduledAt_ASC',
  _publicationScheduledAt_DESC = '_publicationScheduledAt_DESC',
  _publishedAt_ASC = '_publishedAt_ASC',
  _publishedAt_DESC = '_publishedAt_DESC',
  _status_ASC = '_status_ASC',
  _status_DESC = '_status_DESC',
  _unpublishingScheduledAt_ASC = '_unpublishingScheduledAt_ASC',
  _unpublishingScheduledAt_DESC = '_unpublishingScheduledAt_DESC',
  _updatedAt_ASC = '_updatedAt_ASC',
  _updatedAt_DESC = '_updatedAt_DESC',
  CreatedAt_ASC = 'createdAt_ASC',
  CreatedAt_DESC = 'createdAt_DESC',
  Id_ASC = 'id_ASC',
  Id_DESC = 'id_DESC',
  PageTitle_ASC = 'pageTitle_ASC',
  PageTitle_DESC = 'pageTitle_DESC',
  Title_ASC = 'title_ASC',
  Title_DESC = 'title_DESC',
  UpdatedAt_ASC = 'updatedAt_ASC',
  UpdatedAt_DESC = 'updatedAt_DESC'
}

Aside: I noticed some other unexpected behaviors. For example transformUnderscores did not seem to do anything with some of the naming conventions. Another one was that with this config:

    config:
      namingConvention:
        enumValues: change-case-all#titleCase

…enum names remained untouched but underscores appeared to some type names:

/** The query root for this schema */
export type Query_SiteArgs = {
  locale?: InputMaybe<SiteLocale>;
};


/** The query root for this schema */
export type QueryAllPagesArgs = {
  filter?: InputMaybe<PageModelFilter>;
  first?: InputMaybe<Scalars['IntType']>;
  locale?: InputMaybe<SiteLocale>;
  orderBy?: InputMaybe<Array<InputMaybe<PageModelOrderBy>>>;
  skip?: InputMaybe<Scalars['IntType']>;
};
0reactions
stefanovernacommented, Nov 10, 2022

In DatoCMS createdAt is a legacy field that will be soon removed, while _createdAt is the new name. They have exactly the same meaning.

Thanks @cjpete, I’ll add a complete example here that solves the issue:

https://github.com/Tonel/typescript-type-generation-graphql-example/pull/1/files

Read more comments on GitHub >

github_iconTop Results From Across the Web

Confusing "duplicate identifier" Typescript error message
When I ran npm start , I got a bunch of duplicate identifier errors. ... node_modules/@types/es6-promise/index.d.ts(11,15): error TS2300: Duplicate ...
Read more >
Typescript codegen from graphql schema has types issues
Following the steps outlined here: How To Generate TypeScript Types ... function - Duplicate identifier on Typescript type generation due to ...
Read more >
Documentation - Everyday Types - TypeScript
In this chapter, we'll cover some of the most common types of values you'll find in JavaScript code, and explain the corresponding ways...
Read more >
typescript-resolvers - GraphQL Code Generator
You can use this plugin to generate simple resolvers signature based on your GraphQL types, or you can change its behavior be providing ......
Read more >
typescript-cheatsheet - GitHub Pages
A set of TypeScript related notes used for quick reference. The cheatsheet contains references to types, classes, decorators, and many other TypeScript ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found