I am trying to merge a user node and return true if the user node was created and false if the user node already existed. And if optional properties come in with the query, add them to the db.
Here is my query:
CALL apoc.merge.node($labels, $identityProps, {created: true}, {created: false})
YIELD node AS u
WITH u
CALL apoc.do.when(
$optionalProps.advertisingId IS NOT NULL,
"SET u.limitAdTracking = $optionalProps.limitAdTracking
MERGE (a:AdvertisingId {advertisingId: $optionalProps.advertisingId})
MERGE (u)-[:HAS_ADVERTISING_ID]->(a)",
"",
{u:u, optionalProps: $optionalProps}
)
YIELD value
WITH u.created AS created, u
REMOVE u.created
RETURN created
As you can see, I set a temporary property on the user node in the first line, then assign that value to a return value called created
, and then delete the temporary property before returning.
When I don't send $optionalProps
, it works as expected: I get a return value of {created: boolean}
and the created
property on the user node is deleted at the end of the query.
When I do send $optionalProps
(and $optionalProps.advertisingId
is not null), It creates the user node, adds the created
property, runs the ifQuery, all as expected , but then it returns nothing and doesn't delete u.created
.
It seems like if apoc.do.when runs the ifQuery, I can't get it to do anything after the apoc.do.when. If I change the last few lines to
YIELD value RETURN value
I get nothing back.
same with
YIELD value RETURN u
I'm pretty stumped.
I'm using the javascript driver 5.1.0 like this (but I get the same behavior in the neo4j browser):
return await session.executeWrite((tx) => tx.run(query, {labels, identityProps, optionalProps));