graphql-java-annotations icon indicating copy to clipboard operation
graphql-java-annotations copied to clipboard

How should nulls and omitted values be handled?

Open mridang opened this issue 3 years ago • 1 comments

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;
    }
}

mridang avatar Nov 26 '20 08:11 mridang

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)

yarinvak avatar Dec 07 '20 10:12 yarinvak

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,

Fgerthoffert avatar Jun 02 '23 08:06 Fgerthoffert