Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differentiate between null and undefined

I'm working on a GraphQL API project where user is able to update an entire article or portions of it.

Usually, I just send JSON with columns I want to update and it works flawlessly for these columns, the json is as follows:

{
    title: "New title",
    categoryId: 3
}

But here's the issue: any other column that I didn't provide in this json becomes a null and simply adding:

if(value == null) 
    -- don't update that column

Wouldn't work with columns that can be nullable, because there's no difference between sending:

{
    title: "New title",
    categoryId: 3
}

and sending

{
    title: "New title",
    categoryId: 3,
    someCol: null
}

Even though in first JSON I want the value to remain unchanged and in second JSON I want it to be set to null.

So, my question is - how can I handle it in EFCore?

like image 362
JCode Avatar asked Sep 17 '26 01:09

JCode


1 Answers

We have the same situation and we've gone with: null = don't change, anything else = change to value.

var entity = ReadEntity();

if(jsonInput.Field1 != null)
{
    entity.Field1 = jsonInput.Field1;
}

This means that you can never 'unset' a field in the entity, but for us, that is acceptable. null = uninitialise, once it's initialised, it can't be 'uninitialised', only set to another value (including 'default').

like image 90
Neil Avatar answered Sep 18 '26 15:09

Neil