Duplicate identifier on Typescript type generation due to _ prefix
See original GitHub issueDescribe 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
- 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
- My
codegen.ymlconfig 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'
- 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
- OS: Darwin (MacOS)
- “@graphql-codegen/cli”: “2.2.0”,
- “@graphql-codegen/introspection”: “2.1.0”,
- “@graphql-codegen/typescript”: “2.2.2”,
- “@graphql-codegen/typescript-operations”: “2.1.4”,
- “@graphql-codegen/typescript-react-apollo”: “3.1.4”,
- “@graphql-codegen/typescript-resolvers”: “2.2.1”,
- NodeJS: v16.3.0
Additional context
Thanks ✌️
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:8 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I played around with the different naming conventions. From the ones I tried, this issue occurs with
pascalCaseandcamelCasebut not withtitleCase,upperCaseorupperCaseFirst. For this particular use case usingupperCaseFirstonly for enums gives a good enough workaround for me.Configuration:
Output:
Aside: I noticed some other unexpected behaviors. For example
transformUnderscoresdid not seem to do anything with some of the naming conventions. Another one was that with this config:…enum names remained untouched but underscores appeared to some type names:
In DatoCMS
createdAtis a legacy field that will be soon removed, while_createdAtis 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