Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Connection return values for mutations #722

Closed
gnumbix opened this issue Jun 10, 2023 · 2 comments
Closed

Handle Connection return values for mutations #722

gnumbix opened this issue Jun 10, 2023 · 2 comments
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@gnumbix
Copy link

gnumbix commented Jun 10, 2023

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!
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 10, 2023
@rstoyanchev
Copy link
Contributor

Currently, we skip mutations and subscriptions when adapting Connection return values. I suppose no reason why it couldn't apply to mutations.

@rstoyanchev rstoyanchev self-assigned this Jun 12, 2023
@rstoyanchev rstoyanchev added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 12, 2023
@rstoyanchev rstoyanchev added this to the 1.2.1 milestone Jun 12, 2023
@rstoyanchev rstoyanchev changed the title @MutationMapping + Page<?> + Connection = GraphQL error Handle Connection return values for mutations Jun 12, 2023
@gnumbix
Copy link
Author

gnumbix commented Jun 12, 2023

I understand. Thank you.

rstoyanchev added a commit that referenced this issue Jun 16, 2023
rstoyanchev added a commit that referenced this issue Jun 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants