I have a single TypeScript file (generated by a custom plugin) containing all enums for whole GraphQL schema.
I would like to have an option in graphql-codegen's config to specify that all enums should be imported from the file without the need manually mapping each enum in enumValues.
The config would allow a string, a file name, as an alternative to EnumValuesMap in enumValues option:
# config.yml
config:
enumValues: ./enums
If enumValues was EnumValuesMap, it would behave in the same way as now. If enumValues was string, the generator would not generate any enum at all. It would only add import {<EnumName>} from '<enumValues>' for each enum it encounters in the schema, assuming that enum with matching name exists in the file.
Would something like this be acceptable?
Hi @vhenzl
You can map you enums to an external file, and then it will be loaded from there. Is that what you need?
https://graphql-code-generator.com/docs/plugins/typescript#usage-example-with-external-enum
Nah, that's what I'm doing. The config looks like
config:
enumValues:
MyEnum1: ./enums#MyEnum1
MyEnum2: ./enums#MyEnum2
# … tens more
MyEnumN: ./enums#MyEnumN
Since the enum name on the left side and the right side is always the same and the file name is the same for all lines, I would like to simplify the config to
config:
enumValues: ./enums
@vhenzl I see. It's something that shouldn't be too complicated to support.
We can give it a try in the future (and we always welcome PRs ;)).
Added in: https://github.com/dotansimha/graphql-code-generator/pull/2121
Available as alpha 1.3.1-alpha-789526ab.83.
Awesome! Thanks a lot.
@dotansimha, I tried it in my project and it works fine 👍
One question, however. It also adds imports for __TypeKind and __DirectiveLocation. Is it intentional that these schema introspection enums are also included?
Available in 1.4.0 🎉
@vhenzl those are added because your queries are using the internal introspection types, so the codegen generates it for you.
@dotansimha I'm not sure I understand. The imports are added even if there aren't any queries and typescript-operations plugin isn't installed at all.
yarn add -D graphql @graphql-codegen/cli @graphql-codegen/typescript
# schema.graphql
schema {
query: Query
}
type Query {
me: User!
}
enum Role {
USER,
ADMIN,
}
type User {
id: ID!
username: String!
role: Role!
}
#codegen.yml
schema: schema.graphql
generates:
types.ts:
- typescript
config:
enumValues: ./enums
The result:
import { Role } from "./enums";
import { __TypeKind } from "./enums";
import { __DirectiveLocation } from "./enums";
export type Maybe<T> = T | null;
/** 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 Query = {
__typename?: "Query";
me: User;
};
export type User = {
__typename?: "User";
id: Scalars["ID"];
username: Scalars["String"];
role: Role;
};
The issue with the introspection types has been fixed in: https://github.com/dotansimha/graphql-code-generator/pull/2181