gql-dart / gql

Libraries supporting GraphQL in Dart
MIT License
267 stars 121 forks source link

#feat: Add value or absent option #449

Closed LiLatee closed 6 months ago

LiLatee commented 6 months ago

Hey! 👋 I've been lacking the option to send a value if it exists and not send any value (neither null) if not present.

EDIT: Converted to DRAFT, because it's not working yet.

knaeckeKami commented 6 months ago

Why do you need a separate class for that? Adding a new subtype to a sealed class is a breaking change.

And there are no new states - a value is either present or absent. we don't need a separate type for this, this could be a static method or factory, couldn't it?


factory Value.ofNullabe(T? t) {
   if(t == null} { 
      return Value.absent();
   }
   return Value.present(t);
}
LiLatee commented 6 months ago

Adding a new subtype to a sealed class is a breaking change.

Yes, I've noticed that 😅 So you are right, it's better to just add a factory. Seems to work fine in my case 👌 Idk why I complicated it so much 😞 Thanks!

knaeckeKami commented 6 months ago

final nit:

Thinking about it, I think .ofNullable is not a great name for the behavior "present if not null, absent otherwise".

What about Value.absentWhenNull() ? of can you think of something more descriptive?

LiLatee commented 6 months ago

Regarding the name I was thinking about Value.valueOrAbsent, but I have to admit that your absentWhenNull looks better 👌 Changed

knaeckeKami commented 6 months ago

thanks! lgtm