smallrye-graphql icon indicating copy to clipboard operation
smallrye-graphql copied to clipboard

Make optional parameters nullable

Open dicolasi opened this issue 4 years ago • 3 comments
trafficstars

It would be great to have an annotation for nullable/optional parameters. Here an example:

@Query("getYieldByPostcode") public Uni<YieldStats> getYield(@Name("fullPostcode") @NonNull String postcode, @Name("bedrooms") int bedrooms, @Name("houseType") String houseType){ return yieldService.getByFullPostCode(postcode,bedrooms,houseType); }

bedrooms and houseType are optional, as per today, I got to default them to a dummy value, check inside the method and pass null when required.

dicolasi avatar Apr 15 '21 11:04 dicolasi

Thanks @dicolasi .

This case is when you have an Integer (Object) rather than a primitive, and it can be null. So it would be good to allow @Nullable on Object primitives.

phillip-kruger avatar Apr 15 '21 11:04 phillip-kruger

Maybe I misunderstand this, but what would be passed as null for a primitive type? 0? -1? And it's irritating to have an Optional to be null, isn't it?

OTOH maybe it makes sense add a config option to invert the default, i.e. everything is NonNull by default. Then we'd need a Nullable for those fields that actually should be nullable. Only Optionals should remain always nullable.

t1 avatar Apr 16 '21 03:04 t1

Yes Optional<Integer> or OptionalInt should also work. @dicolasi that might be a clean solution than checking the -1 on the int value.

phillip-kruger avatar Apr 16 '21 06:04 phillip-kruger