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

How to deal with the cyclic reference? #162

Closed pavelmasekcmgx closed 1 month ago

pavelmasekcmgx commented 1 month ago

Is there some kind of common way how to deal with the cyclic reference of the graph?

Let's imagine graph like this one.

type Query {
  user(id: ID): User
  location(id: ID): Location
}

type User {
  id: ID!
  ...
  location?: Location
}

type Location {
  id: ID!
  ...
  user?: User
}

that will generate mock functions

function anUser(...) {...}
function aLocation(...) {...}

calling those functions will result in Maximum call stack size exceeded

I can imagine something like altering of the fieldGeneration in the plugin config.

fieldGeneration:
  User:
    location: aLocation({user: null})
  Location:
    user: anUser({location: null})

but this becomes quite complicated in case of more complex graph where the cyclic reference appears on some nested levels.

ardeois commented 1 month ago

Have you tried this option? https://github.com/ardeois/graphql-codegen-typescript-mock-data?tab=readme-ov-file#terminatecircularrelationships-boolean-defaultvalue-false

pavelmasekcmgx commented 1 month ago

That works like a charm I completely missed that in the readme. Thanks a lot 🙏