Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only export enum type #3475

Closed
TomasBarry opened this issue Feb 11, 2020 · 13 comments
Closed

Only export enum type #3475

TomasBarry opened this issue Feb 11, 2020 · 13 comments
Labels
plugins waiting-for-release Fixed/resolved, and waiting for the next stable release

Comments

@TomasBarry
Copy link

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.

@dotansimha
Copy link
Owner

@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?

@dotansimha dotansimha added the waiting-for-answer Waiting for answer from author label Feb 13, 2020
@dotansimha
Copy link
Owner

Closing. @TomasBarry let me know if it's still relevant.

@darkbasic
Copy link
Contributor

@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.

@dotansimha
Copy link
Owner

@darkbasic I see. I'm not sure if it's related, but does const enum assertions works in d.ts files? (see #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 ;)

@darkbasic
Copy link
Contributor

@dotansimha that's the problem: const enums don't work in .d.ts files either.

@dotansimha
Copy link
Owner

Done in: #3760

@dotansimha dotansimha added enhancement plugins waiting-for-release Fixed/resolved, and waiting for the next stable release and removed waiting-for-answer Waiting for answer from author labels Apr 28, 2020
@TomasBarry
Copy link
Author

Thank you @dotansimha!

@dotansimha
Copy link
Owner

Fixed in v1.13.4 :)

You can now use:

config:
  preResolveTypes: true
  onlyOperationTypes: true

And you'll get minimal types generated.

@antgonzales
Copy link

@dotansimha thanks for putting in the work to add those additional flags! One issue is I would like to only generate enums and not create types in the .ts file. Is that possible?

@dotansimha
Copy link
Owner

@dotansimha thanks for putting in the work to add those additional flags! One issue is I would like to only generate enums and not create types in the .ts file. Is that possible?

It's not available at the moment, you can create a PR if you need that ;)

@Borduhh
Copy link
Contributor

Borduhh commented Apr 1, 2022

@dotansimha thanks for putting in the work to add those additional flags! One issue is I would like to only generate enums and not create types in the .ts file. Is that possible?

We need this as well for the exact reason stated here:

@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.

@Borduhh
Copy link
Contributor

Borduhh commented Apr 1, 2022

Solved this in #7718 @dotansimha

@charlypoly
Copy link
Contributor

@Borduhh thank you for your contribution! ⚡

onlyEnums options released in @graphql-codegen/typescript@2.4.10 and related packages (see #7822)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugins waiting-for-release Fixed/resolved, and waiting for the next stable release
Projects
None yet
Development

No branches or pull requests

6 participants