Enigmatis / graphql-java-annotations

GraphQL Annotations for Java
Other
387 stars 97 forks source link

How should nulls and omitted values be handled? #267

Closed mridang closed 1 year ago

mridang commented 3 years ago

How can I distinguish in java graphQL if a parameter was explicitly set to null, or if it was not provided at all?

mutation {
  updateProducts(products: [
    {
      id: "ABC"
      name: null
      availability: "InStock"
    }
  ]) {
    result {
      errors {
        field
      }
    }
  }
}

How can I differentiate between name being null vs. name being omitted? Here's my bean:

import graphql.annotations.annotationTypes.GraphQLField;
import graphql.annotations.annotationTypes.GraphQLName;
import graphql.annotations.annotationTypes.GraphQLNonNull;

public class MyEntity {

    @GraphQLField
    @GraphQLName("id")
    @GraphQLNonNull
    public final String id;

    @GraphQLField
    @GraphQLName("name")
    private final String name;

    @GraphQLField
    @GraphQLName("availability")
    private final String availability;

    public MyEntity(
            @GraphQLNonNull
            @GraphQLName("id") String id,
            @GraphQLName("name") String name,
            @GraphQLName("availability") String availability) {
        this.id = id;
        this.name = name;
        this.availability = availability;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getAvailability() {
        return availability;
    }
}
yarinvak commented 3 years ago

You can receive a HashMap in the constructor which contains your arguments. Then if the argument name exists as a key in the hashmap, you know it's defined (and null in that matter)

Fgerthoffert commented 1 year ago

Hi @mridang ,

We're helping with project maintenance and reviewing the list of opened PRs and Issues.

This issue was created quite a while ago, we were wondering if you were still interested in the outcome, please let us know if this is the case.

Without an answer by July 1st, 2023, this issue will be closed as "inactive" (and can always be re-opened later on if needed).

Thanks,