Apoc.do.when not creating nodes when is NULL check is true

I am trying to use the apoc.do.when function to check if a node and its relationship existing

if parent doesn't then I want to create a new node and link it

If parent exists then I want to update/modify the child

This is not working for me to create, but works for modify. The following snippet would replicate this issue

//Create test data
CREATE (parent1:PARENT{parent_no:1,name:"dad1"})
CREATE (parent2:PARENT{parent_no:2,name:"dad2"})
CREATE (child1:CHILD{name:"child1"})
MERGE (parent1)-[r:PARENT_OF]->(child1)
RETURN parent1, parent2, child1

1. THE APOC Query is as follows: This correctly modifies the child node because

MATCH (parent:PARENT{name:"dad1"})
WITH elementId(parent) as parent_id
MATCH(parent) WHERE elementId(parent)= parent_id
MATCH (parent)-[relation:PARENT_OF]->(child:CHILD)
CALL apoc.do.when(
child is NULL,
'//when child is null
MATCH(parent) WHERE elementId(parent)= parent_id
CREATE (new_child:CHILD{name:"child1"})
set new_child.created=true
MERGE (parent) -[r:PARENT_OF]-> (new_child)
RETURN new_child as child, parent as parent, "created" as action',

'//when child is null
SET child.modified =true
RETURN child as child, parent as parent, "modified" as action',
// variables in the block
{parent_id:parent_id, parent:parent, child:child}
)
YIELD value
RETURN value.child, value.parent, value.action

I correctly get the result of child1, parent1, "modified" and child 1 has modified attribute

2. THE APOC Query when modified for dad2 should create another child1 node but I get no result , no error , no changes or records

MATCH (parent:PARENT{name:"dad2"})
WITH elementId(parent) as parent_id
MATCH(parent) WHERE elementId(parent)= parent_id
MATCH (parent)-[relation:PARENT_OF]->(child:CHILD)
CALL apoc.do.when(
child is NULL,
'//when child is null
MATCH(parent) WHERE elementId(parent)= parent_id
CREATE (new_child:CHILD{name:"child1"})
set new_child.created=true
MERGE (parent) -[r:PARENT_OF]-> (new_child)
RETURN new_child as child, parent as parent, "created" as action',

'//when child is null
SET child.modified =true
RETURN child as child, parent as parent, "modified" as action',
// variables in the block
{parent_id:parent_id, parent:parent, child:child}
)
YIELD value
RETURN value.child, value.parent, value.action

Thanks for any help / guidance

Because no parent is returned in the query

MATCH (parent)-[relation:PARENT_OF]->(child:CHILD)

So if parent is null it cannot create anything. You need to first fetch parent, then with OPTIONAL keyword fetch child. This way parent will be returned even if it has no child. Below query works.

  MATCH (parent:PARENT{name:"dad2"}) OPTIONAL MATCH(parent)-[relation:PARENT_OF]->(child:CHILD)
 CALL apoc.do.when(
    child is NULL,
    
    '//when child is null
    MERGE (parent)-[r:PARENT_OF]->(new_child:CHILD{name:"child1",created:true})
    RETURN new_child as child, parent as parent, "created" as action',

   '//when child is not null
    SET child.modified =true
    RETURN child as child, parent as parent, "modified" as action',
   // variables in the block
   {parent:parent, child:child}
 )
 YIELD value
RETURN value.child, value.parent, value.action