smallrye / smallrye-graphql

Implementation for MicroProfile GraphQL
Apache License 2.0
160 stars 91 forks source link

`@NonNull` at class/package level #923

Open ybroeker opened 3 years ago

ybroeker commented 3 years ago

It would be nice, if @NonNull (or an alternative annotation) could be used at class or package-level to mark all fields as non null.

Most of our classes look like this:

class API {
  @Query @NonNull String someField() {...}

  @Query Optional<String> someOptionalField() {...}

  @Query @NonNull List<@NonNull String> someFields() {...}

  @Mutation @NonNull SomePayload someFields(@NonNull SomeInput input) {...}
}

which could look like this if @NonNull on classes would be possible, where anything except someOptionalField would be non-null:

@NonNull
class API {
  @Query String someField() {...}

  @Query Optional<String> someOptionalField() {...}

  @Query List<String> someFields() {...}

  @Mutation SomePayload someFields(SomeInput input) {...}
}

I don't know if @NonNull is the best approach for this, maybe a new annotation to specify default nullability for fields, inputs, parameters would be better.

Maybe someone has an idea?

t1 commented 3 years ago

I like your suggestion, as we also prefer to make everything non-nullable. But when we configure it at a wider scope, we need to be able to define exceptions to the rule, i.e. a @Nullable annotation (or so). Maybe we could define the global setting in the config properties.