graphql-spring-boot-starter icon indicating copy to clipboard operation
graphql-spring-boot-starter copied to clipboard

Spring -> How to create 2 GraphQLObjectType-Objects referencing each other.

Open MartinX3 opened this issue 8 years ago • 0 comments

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");

MartinX3 avatar Sep 25 '17 08:09 MartinX3