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

Adds support for multiple generators per scalar/field, chosen randomly #142

Closed clang13 closed 3 months ago

clang13 commented 10 months ago

First off, this is a great package that I've found to be a real help in creating Storybook stories and test data. I hope this contribution seems useful!

I found it useful to have generators that provide more variety than is available from calling a single casual or faker generator (for instance, fields which are often null but occasionally have data). While this is possible via custom code, I prefer to make it more explicit through the configuration itself.

This PR adds support for passing an array of generator options to both the scalar and fieldGeneration options. If an array is provided, one option will be chosen at random each time a value is generated. By default each is equally likely, but a weight parameter can be specified to change the relative likelihood of each option.

Examples from the docs:

fieldName: # will be null 3/4 of the time, contain the output of faker.lorem.paragraphs() the other 1/4
  - generator: 'null'
    weight: 3
  - lorem.paragraphs

which could also be written as:

fieldName: # will be null 3/4 of the time, contain the output of faker.lorem.paragraphs() the other 1/4
  - 'null'
  - 'null'
  - 'null'
  - lorem.paragraphs
stale[bot] commented 9 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

ardeois commented 7 months ago

@clang13 could you fix the conflicts and warnings please? I'll check the code soon

stale[bot] commented 4 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.