Is your feature request related to a problem? Please describe.
The flexibility of GraphQL queries severely limits the use of exported types in the generated file of Flow types.
Consider the following schema:
/* @flow */
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {|
ID: string,
String: string,
Boolean: boolean,
Int: number,
Float: number,
ISO8601Date: any,
ISO8601DateTime: any,
|};
export const MyEnumValues = Object.freeze({
A: 'a',
B: 'b',
C: 'c'
});
export type MyEnum = $Values<typeof MyEnumValues>;
export type MyType = {|
id: $ElementType<Scalars, 'ID'>,
field: MyEnum,
|};
It would be fragile to type the return value of a GraphQL query with the type of MyType as Flow would not be able to assist us if we changed the query from:
query {
myType {
id,
field
}
}
To:
query {
myType {
field
}
}
Describe the solution you'd like
We would like a mechanism to just generate types for enums and skip over GraphQL types.
Describe alternatives you've considered
The alternative is manually defining and maintaining enums in Flow or limiting ourselves to just importing the enums. However, if the types can be imported it is not reasonable for us to be able to police every commit from using them.
@TomasBarry
The basic flow plugin only generates the base types, based on your GraphQL Schema.
If you wish to get type safety based on your queries, you should use flow-operations as well, and use documents: section to specify your GraphQL operations.
It will generate Flow types based on your selection set, and if you change your query if will generate new types according to the fields you are asking for.
Does that covers you use-case? I don't understand why generating only enums is related?
Closing. @TomasBarry let me know if it's still relevant.
@dotansimha the feature could make sense for another use case. Let's say that you want to use a .d.ts declaration file instead of a .ts one, but you also don't want to use enumsAsTypes: in that situation it could be useful to generate an additional .ts file with only the enums, so you can import them through your application while maintaining your declaration file.
@darkbasic I see. I'm not sure if it's related, but does const enum assertions works in d.ts files? (see https://github.com/dotansimha/graphql-code-generator/pull/3569) because this might help with that situations.
It shouldn't be too complicated to create a small plugin that only generates enums, and it can share the same logic as the existing plugins... PRs are welcome ;)
@dotansimha that's the problem: const enums don't work in .d.ts files either.
Thank you @dotansimha!
Fixed in v1.13.4 :)
You can now use:
config:
preResolveTypes: true
onlyOperationTypes: true
And you'll get minimal types generated.
Most helpful comment
Fixed in v1.13.4 :)
You can now use:
And you'll get minimal types generated.