Spring -> How to create 2 GraphQLObjectType-Objects referencing each other.
Dear developer,
The Question is: How to solve that problem?
In Spring (@Autowired / @ Lazy) I want to create an GraphQL-Java-Server, which acts as a Fruit-Shop-Database. With
query
{
products
{
id
name
vendor
{
id
name
}
}
}
I get the products and the vendor of each product.
But I also want bidirectional connections, like it is possible with the GraphQL-Database "dgraph". So I want to do, as example, this one, too.
query
{
products
{
id
name
vendor
{
id
name
products
{
id
name
}
}
}
}
I implement the Fields like this:
vendorType = newObject().name("category").description("A category")
.field(newFieldDefinition().name("id").description("The id").type(GraphQLLong).build())
.field(newFieldDefinition().name("name").description("The name").type(GraphQLString).build())
.field(newFieldDefinition().name("products").description("The products").type(new GraphQLList(productFields.getProductType()))
.dataFetcher(environment -> productDataFetcher.getByCategory(environment.getSource()))
.build())
.build();
That's working nice until there is a kind of reference cycle, where two objects referencing each other directly or over a chain of references.
With the Constructor-Injection, I get the "The dependencies of some of the beans in the application context form a cycle"-Exception.
If I use the Setter-Injection or @Lazy, then I get "graphql.AssertException: type can't be null" in GraphQLFieldDefinition.java in the Internal Constructor, at assertNotNull(type, "type can't be null");