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

Feature Request: a way to set dynamic values only for the ID field #89

Closed MH4GF closed 1 year ago

MH4GF commented 1 year ago

Thanks for creating this awesome library!

Description

I need a way to set dynamic values only for the ID field.

Problem Statement

When using the generated function multiple times to create an array of objects, Each object's ID field will all have the same value. In that case, problems will occur in tools that cache by ID. What I faced was passing an array of objects to storybook-addon-apollo-client seemed to copy the first object. Therefore, it is solved by overriding the ID in all places, but it is troublesome.

Proposed Solution or API

It would be nice to have an option to allow dynamic values ​​for the ID field only. Alternatively, I think it would be good to make it more general and have a setting for whether or not to use dynamic values ​​in ScalarDefinition.

Alternatives

Setting dynamic values ​​was not suitable for our use case as the VRT value would change every time.

Additional Information

I'll put up a PR if either way is better.

ardeois commented 1 year ago

Setting dynamic values ​​was not suitable for our use case as the VRT value would change every time.

Hi @MH4GF

What's VRT? Also the values should not change if you use the seedMocks method before your tests. Like in here Did you try seedMocks ?

MH4GF commented 1 year ago

@ardeois Thanks for quick response!

I misunderstood the behavior of casual.seed , It certainly looked possible to have the scope limited by seedMock. But I have a problem that does not work because I want to use it in a storybook that works in a browser: https://github.com/ardeois/graphql-codegen-typescript-mock-data/issues/90


That's why the proposed method doesn't solve the problem, if the scalarDefinition's generator can be used with the built-in scalar, my problem will be solved. I will try to create a PR to test it out, and would be happy to discuss it based on the code.

MH4GF commented 1 year ago

related: https://github.com/ardeois/graphql-codegen-typescript-mock-data/issues/91

ardeois commented 1 year ago

@MH4GF can you confirm this issue fixed now we can use faker in browser?

MH4GF commented 1 year ago

@ardeois Yes, the faker is now available in the browser, which solved the problem 👍🏻


It would be great to have a tool that generates mock data with less boilerplate for users to write! Thanks for the great job! 😄