Skip to content

Handle Connection return values for mutations #722

Closed
@gnumbix

Description

@gnumbix

If you return Page<?>(spring data) from @MutationMapping and use Connection autogeneration. Then a GraphQL error occurs.

The field at path '/createProduct/edges' was declared as a non null type, but the code involved in retrieving
data has wrongly returned a null value. The graphql specification requires that the parent field be set to null,
or if that is non nullable that it bubble up null to its parent and so on.
The non-nullable type is '[ProductEdge]' within parent type 'ProductConnection'

But if you use @QueryMapping instead of @MutationMapping, then there is no such error.

spring boot 3.1.0
spring-graphql 1.2.0
graphql-java 20.2
OpenJDK 17

To demonstrate, I made a simple example: Demo project

Example of a request with an error:

mutation {
  createProduct {
    edges {
      node {
        title
      }
    } 
  }
}

Query without error:

{
  getProducts {
    edges {
      node {
        title
      } 
    } 
  } 
}

Both requests return the same data.

Controller:

@Controller
public class GraphQlController {

    private Page<Product> getResult() {
        List<Product> products = List.of(new Product("Demo"));
        return new PageImpl<>(
                products,
                PageRequest.of(0, products.size()),
                products.size()
        );
    }

    @MutationMapping("createProduct")
    public Page<Product> createProduct() {
        return getResult();
    }

    @QueryMapping("getProducts")
    public Page<Product> getProducts() {
        return getResult();
    }

}

Schema:

type Product {
    title: String
}

type Mutation {
    createProduct : ProductConnection!
}

type Query {
    getProducts : ProductConnection!
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions