dotansimha / dotansimha/graphql-code-generator
Preserve Type Names
- Dominant language
- TypeScript
- Stars
- 11.3k
- Forks
- 1.4k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 23
Description
Given I have a type called `SICIndustry` I would like the corresponding generated typescript type to match that name.
**Input:**
```gql
type SICIndustry {
code: ID!
title: String!
}
```
**Current Output:**
```ts
export type SicIndustry = {
__typename?: 'SICIndustry';
code: Scalars['ID'];
title: Scalars['String'];
};
```
**Expected Output:**
```diff
- export type SicIndustry = {
+ export type SICIndustry =
__typename?: 'SICIndustry';
code: Scalars['ID'];
title: Scalars['String'];
};
```
## Reproduce
Take the `package.json` and `index.js` below, install the dependencies and then run `node index.js` - see the output with non preserved type name.
### Node Version
v16.0.0
### `package.json`
```json
{
"name": "beer",
"version": "1.0.0",
"main": "index.js",
"license": "ISC",
"dependencies": {
"@graphql-codegen/core": "^2.5.1",
"@graphql-codegen/typescript": "^2.4.8",
"@graphql-tools/schema": "^8.3.6",
"@graphql-tools/utils": "^8.6.5",
"graphql": "^15.8.0"
},
"devDependencies": {
"typescript": "^4.3.2"
},
"description": ""
}
```
### `index.js`
```js
const { codegen } = require("@graphql-codegen/core");
const { makeExecutableSchema } = require("@graphql-tools/schema");
const graphql = require("graphql");
const typescriptPlugin = require("@graphql-codegen/typescript");
const typeDefs = `
type SICIndustry {
code: ID!
title: String!
}
type Query {
SICIndustrys: [SICIndustry]
}
`;
const resolvers = {
Query: {
SICIndustrys: () => false,
},
};
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
async function main() {
const config = {
config: {},
plugins: [
{
typescript: {},
},
],
filename: "types.ts",
documents: [],
schemaAst: schema,
schema: graphql.parse(graphql.printSchema(schema)),
pluginMap: {
typescript: typescriptPlugin,
},
};
const output = await codegen(config);
console.log(output);
}
main();
```
### Output
```ts
export type Maybe = T | null;
export type InputMaybe = Maybe;
export type Exact = { [K in keyof T]: T[K] };
export type MakeOptional = Omit & { [SubKey in K]?: Maybe };
export type MakeMaybe = Omit & { [SubKey in K]: Maybe };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: string;
String: string;
Boolean: boolean;
Int: number;
Float: number;
};
export type SicIndustry = {
__typename?: 'SICIndustry';
code: Scalars['ID'];
title: Scalars['String'];
};
export type Query = {
__typename?: 'Query';
SICIndustrys?: Maybe>>;
};
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.