How to use apoc procedure in Merge clause?

Hello,
I am trying to use merge on create clause combine with an apoc procedure as I can't use parameters on on node labels. My query is shown bellow:

req = """
           MERGE (a {key:$sHash}) 
            ON CREATE
               SET a.uri = $s
               WITH a as subject
               call apoc.create.addLabels(subject, [$typeSub]) YIELD node as sub
            ON MATCH
              SET a.uri = $s
              WITH a as subject
              call apoc.create.addLabels(subject, [$typeSub]) YIELD node as sub
           MERGE (ob {key:$oHash})
            ON CREATE
              SET ob.uri = $obj
              WITH collect(ob) as object
              call apoc.create.addLabels(object, [$typeObj]) YIELD node as ob
           MERGE (sub)-[r:IRI {key:$sHash+$oHash+$pHash}]->(ob)
            ON CREATE
              SET r.uri = $pred
              WITH r as rel
              call apoc.refactor.setType(rel, $pred) YIELD output
           RETURN output
           """
    res = neo4j.run(req, parameters={"s":s, 'typeSub':typeSub, 'typeObj':typeObj, "sHash":sHash, "obj":obj,  "oHash":oHash, 'pred':pred, 'pHash':pHash}).data()

But i got an error:

ClientError: [Statement.SyntaxError] Invalid input 'N': expected 'p/P' (line 7, column 14 (offset: 215))
"            ON MATCH"
              ^

I am asking my self if can include apoc procedure in merge on create clause. If yes how I can correct my query, If not what's the alternative that I can use.

Thanks

You should pass only the SET statements in ON CREATE and ON MATCH and wrap the CALLs with WITH statements.

So, something like this:

MERGE (a {key:$sHash})
    ON CREATE
        SET a.uri = $s
    ON MATCH
        SET a.uri = $s
WITH a as subject
    CALL apoc.create.addLabels(subject, [$typeSub]) YIELD node as sub
WITH sub
MERGE (ob {key:$oHash})
ON CREATE
    SET ob.uri = $obj
WITH collect(ob) as object, sub
    CALL apoc.create.addLabels(object, [$typeObj]) YIELD node as ob
WITH ob, sub
    MERGE (sub)-[r:IRI {key:$sHash+$oHash+$pHash}]->(ob)
ON CREATE
    SET r.uri = $pred
WITH r as rel
    CALL apoc.refactor.setType(rel, $pred) YIELD output
RETURN output
1 Like

Hi @giuseppe.villani Thanks for your reply. It's works !