ardeois / graphql-codegen-typescript-mock-data

[GraphQL Codegen Plugin](https://github.com/dotansimha/graphql-code-generator) for building mock data based on the schema.
MIT License
132 stars 47 forks source link

support for enumsAsTypes #117

Closed bryantserres closed 1 year ago

bryantserres commented 1 year ago

may have missed something but is enumsAsTypes (https://the-guild.dev/graphql/codegen/plugins/typescript/typescript#enumsastypes) currently supported?

ardeois commented 1 year ago

No this is not currently supported. What would be the use case of having this supported in functions generating mock data?

bryantserres commented 1 year ago

it could be useful for defaults with mock data generating functions

example:

enum Status {
  ONLINE
  OFFLINE
}

using graphql-codegen/cli with enumsAsTypes: true would output:

export type Status = 'ONLINE' | 'OFFLINE';

a mock data generator would be output as something like the following using the schema's enum vs. the generated enum as a string union type which results is TS errors related to a type that is being used as a value:

export const aStatus = (overrides?: Partial < ... >) => {
  return {
    status: overrides && overrides.hasOwnProperty('status') ? overrides.status : Status.ONLINE,
  };
};

desired output would be:

export const aStatus = (overrides?: Partial < ... >) => {
  return {
    status: overrides && overrides.hasOwnProperty('status') ? overrides.status : 'ONLINE',
  };
};
bryantserres commented 1 year ago

also, willing to put out a PR to add this functionality 🙂

ardeois commented 1 year ago

Ah interesting, that makes sense indeed. Feel free to open a PR to add this functionnality, but make sure to add unit tests, the code is not as clean as I would like, but we try to cover all use cases with unit tests

ardeois commented 1 year ago

Fixed in #118