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

Differentate between undefined and null on input parameters.

Open MarcusDunn opened this issue 4 years ago • 1 comments

I'm not sure if this is a matter of missing documentation or if I'm making a feature request (although I'm pretty sure its the latter). According to the graphql spec for a given resolver

type Query {
  hero(episode: Episode): Character
}

I should be able to tell serverside weather the request was

hero {
     ...
}

or

hero(episode: null) {
    ...
}

and change the behaviour accordingly.

I personally have a use case (which after looking at the spec is also their motivating example) for this where when updating nullable fields on an entity, I would like to be able to allow the consumer to leave the field undefined (not pass null) and not update the field, or explicitly set to null to set the field to null.

setHero(id: 1, firstName: "Luke") {
    ...
}

would leave the middleName alone whereas

setHero(id: 1, firstName: "Luke", middleName: null) {
    ...
}

would remove it.

GraphQLKotlin solves this nicely by adding a sum type (or Kotlins best attempt at it) over the three possibilities (undefined, null, T). It also allows to simply set the feild as nullable (a Haskell Maybe) if one does not wish to differentiate between the undefined and null. I'm a fairly new Haskeller but would be happy to take a crack at implementing this if I could have a pointer in the correct direction.

MarcusDunn avatar Aug 29 '21 04:08 MarcusDunn

thanks @MarcusDunn. actually, I'm not in favour of such a distinction. the value is either there or it isn't.

however if you wish you can try. this would also require that we rewrite the validation a bit. and you would then define type like and use it.

data Option a = Null | Undefined | Some a

nalchevanidze avatar Sep 30 '22 21:09 nalchevanidze

as I said I am not in favour of this difference, accordingly I close it. but if the demand for it increases, you could open it again.

nalchevanidze avatar Oct 15 '22 10:10 nalchevanidze