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

@graphql-tools/mock - controllable randomness when mocking out union types #5614

Open
waynezhang1995 opened this issue Sep 28, 2023 · 0 comments

Comments

@waynezhang1995
Copy link

Is your feature request related to a problem? Please describe.

Hello, we are using @graphql-tools/mock in our unit and functional testing. It works well for most parts; however, we recently discovered that the default mocks for union types are generated randomly, which means the mocked value could change from one test run to another. If an engineer isn't aware that a field is a dependency or even a sub-dependency of their module, it's possible to have a flaky test.

For example, given a union type like

union A = B | C | D;

The resolved type of A is completely unpredictable(can be B, C, or D) between different test runs. We believe this behavior is due to this takeRandom call

Describe the solution you'd like
It would be nice if we can add an extra control to the addMocksToSchema function. Something like fixedUnionType: boolean. If the flag is true, we also return the first subtype from a union and generate mocks for it. In the above example, if fixedUnionType is true, A would always resolve to B.

Describe alternatives you've considered
We could ask our engineers to always provide a mock override when dealing with union types. However, this solution might be unrealistic, even at a small scale, since a component might consume many fields, and providing mocks for everything would be a heavyweight task.

Additional context

@waynezhang1995 waynezhang1995 changed the title @graphql-tools/mock - controllable randomness of mocking out union types @graphql-tools/mock - controllable randomness when mocking out union types Sep 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant