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

Dynamic values for enums #110

Closed arbejd closed 1 year ago

arbejd commented 1 year ago

Thank you folks for an excellent plugin!

Im having some difficulty in dynamically handling enums. I got an enum

export const UserStatus = {
  ACTIVE: 'active',
  DISABLED: 'disabled'
} as const;

export type UserStatus = typeof UserStatus[keyof typeof UserStatus];

that i want to mock dynamically so its just picked at random, but this does nothing for me:

fieldGeneration: {
          User: {
            userStatus: {
              generator: 'helpers.arrayElement',
              arguments: ['active', 'disabled'],
            },
            ...
 }

No matter what generator i use it seems i cant override the definition that it just takes the first of the enums always.

Any hints?

ardeois commented 1 year ago

It's indeed possible that fieldGeneration has not been implemented for enums. I'll tag this as a bug

ardeois commented 1 year ago

I have no time to fix this right now, but feel free to open a pull request. Or maybe @emab if you want to check it out?

emab commented 1 year ago

I'll look into this when I have minute.

I would have thought this should work but potentially the enum auto generation is taking precedence.

emab commented 1 year ago

I've just opened a PR which should allow this to work @arbejd but worth noting that you'll need to pass in your arguments like this:

fieldGeneration: {
          User: {
            userStatus: {
              generator: 'helpers.arrayElement',
              arguments: [['active', 'disabled']],
            },
            ...
}

Note the double arrays - this ensures the array is passed in as the argument to helpers.arrayElement.

ardeois commented 1 year ago

Fixed on version 3.2.2