Null input for graphql mutation

Hello I was wondering if it was possible to set fields to null with a graphql mutation?

I thought if the field is not marked as required in the realm schema it would be possible but I keep getting this error.

{
“data”: null,
“errors”: [
{
“message”: “Syntax Error GraphQL request (35:24) Unexpected Name “null”\n\n34: _id: “111118e14e40ae75111b0005”,\n35: classifications: null,\n ^\n36: createDateTimeStamp: “2022-02-01T20:32:33.255Z”,\n”,
“locations”: [
{
“line”: 35,
“column”: 24
}
]
}
]
}

Hey there, can you provide the mutation that you’re running and the associated schema? I think what you’re looking for is https://www.mongodb.com/docs/realm/schemas/enforce-a-schema/#validate-null-types which will allow you to explicitly pass in null for optional types but please let me know if your question is different

Hi Sumedha,

My graphql query is:

mutation {
updateOneIncidentType(query:
{
SQLServerId: “AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA”},
set:{name: null}
) {
SQLServerId
name
}
}

And the schema is:

{
“properties”: {
“SQLServerId”: {
“bsonType”: “string”
},
“_id”: {
“bsonType”: “objectId”
},
“createDateTimeStamp”: {
“bsonType”: “date”
},
“dataHubVersion”: {
“bsonType”: “long”
},
“isActive”: {
“bsonType”: “bool”
},
“isDeleted”: {
“bsonType”: “bool”
},
“name”: {
“bsonType”: “string”
},
“partition”: {
“bsonType”: “string”
}
},
“required”: [
“createDateTimeStamp”,
“isActive”,
“isDeleted”,
“partition”
],
“title”: “IncidentType”
}

I set Null Type Schema Validation to ON but I still get this error:

{
“data”: null,
“errors”: [
{
“message”: “Syntax Error GraphQL request (36:17) Unexpected Name “null”\n\n35: SQLServerId: “AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA”},\n36: set:{name: null} \n ^\n37: ) {\n”,
“locations”: [
{
“line”: 36,
“column”: 17
}
]
}
]
}

I believe you actually have to use set:{name_unset:true} rather than set explicitly to null

Oh I see name_unset works. That makes it undefined though, although I think that might work for me.

Just double checking there’s no way to make it actually null through graphql? I can do it with a realm function although I prefer graphql.

Thanks

I encountered another problem using _unset

If I do

            mutation {
            
            aliasOrgIncident: updateOneOrgIncident(query: { SQLServerId: "AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA" },
                set: { location: 
                  {longitude_unset: true 
                    latitude_unset: true 
                    description: "Test"}  
                  
                }
            )
            {
                _id
            }}

I get this error

{
“data”: {
“aliasOrgIncident”: null
},
“errors”: [
{
“message”: “(ConflictingUpdateOperators) Updating the path ‘location’ would create a conflict at ‘location’”,
“locations”: [
{
“line”: 4,
“column”: 17
}
],
“path”: [
“aliasOrgIncident”
]
}
]
}

But if I update the description without the longitude_unset/latitude_unset it works. It also works if I update
longitude_unset/latitude_unset without the description. It’s just when it’s together that it gives me the error. Do you know why?

This post has the same question but there’s no replies: