I reformulated the question and answered it here: My graphql resolvers have a circular dependency
I am trying to create circular dependency in ApolloGraphQL. The problem is that I can't figure out how to solve the circular references on the resolver side. The use case looks like this:
type Episode {
Id: String
ParentSeason: Season // I don't have this right now (circular dependency)
// etc... A pretty complex type
}
type Season {
Id: String
EpisodeList: [Episode]
// etc... A pretty complex type
}
The way I built this is by creating an Entity(simple class) from the database records and then I added an asGqType() function that adds the additional resolvers to the "raw" object. (I also have a REST API that's why)
The SeasonEntity looks like this:
class SeasonEntity {
constructor(seasonDBRecord) {
this.Id = seasonDBRecord.id;
etc...
}
asGqType = () => {
return {
...this,
EpisodeList: () => {
const episodeEntities = await episodeRepository.getEpisodes(); // returns EpisodeEntity[]
return episodeEntities.map(episode => episode.asGqType() );
}
etc etc...
}
}
It looks really nice, you just call asGqType() and you don't have to deal with the child complexities.
But the problem appears when I try to extend the EpisodeEntity.asGqType() with ParentSeason as it would rely on SeasonEntity which already relies on EpisodeEntity. So javascript will never resolve this: it's normal, I agree.
How could I change this so that my entities can reference any other entity and be referenced by any other entity? I am not looking for a "quick fix" as I have a lot of complex entities. I've seen some people talking about GraphQLObjectType but I am not sure how to actually use that.
I am grateful for any advice, even on how to rearchitect all this. Thank you for your time!