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
133 stars 47 forks source link

Add terminateCircularRelationships option #31

Closed ford-outreach closed 3 years ago

ford-outreach commented 3 years ago

I ran into https://github.com/ardeois/graphql-codegen-typescript-mock-data/issues/13 when trying to bring in this plugin.

This introduces an option, terminateCircularRelationships, that when enabled, allows a particular type to only be resolved once by the mocks in a particular call stack. After the first time, subsequent resolutions will return an empty object.

This does not affect overrides, so if you need a circular structure, it can still be achieved using overrides.

Testing was a little tricky, because this issue doesn't present itself until you actually call one of the generated mock functions. What I've done is created a script (tests/circular-mocks/create-mocks.ts) that generates a set of mock functions for a schema with circular relationships. I hooked it up as a jest global set up script, so it will run before any test runs.

Then added a new test that imports the generated file and runs the mock functions (tests/circular-mocks/spec.ts)

I also pulled the code into our codebase, turned on the option, and it resolved the infinite recursion.

ford-outreach commented 3 years ago

Thank you for the quick review! That's awesome

ford-outreach commented 3 years ago

@ardeois Any chance this could get merged and released this week? I'd like to be able to pull it in directly rather than have to use a fork in the interim

ardeois commented 3 years ago

Yes thanks for the reminder, I'll check it today