title: Merge and Optional match - how to avoid trying to link into a null node
body: <p>I am using CSV load into my neo4j and I'm struggling constructing a query for one of my usecases.
  I have a file with contents like:</p>
  
  <pre><code>aNodeFoo,xNodeBar
  1,5
  2,10
  3,
  4,
  </code></pre>
  
  <p>There are other properties of start node in the file, but this is irrelevant to the question.
  What I'd like to achieve is:</p>
  
  <ul>
  <li>create/merge starting node A and set its property foo to whatever is the value of the column</li>
  <li>if xNodeBar is populated, create a relationship to (existing) node X with property bar equal to given value, but if the cell is empty - simply ignore it.</li>
  </ul>
  
  <p>In other words, for this example I expect to have nodes X with property bar set to 5 and 10 already, and I'd expect 4 nodes A with property foo set to 1,2,3 and 4 created, and node A foo=1 linked to node X bar=5 and node A foo=2 linked to node X bar=10.</p>
  
  <p>If all lines are guaranteed to be populated then query is fairly simple:</p>
  
  <pre><code>LOAD CSV WITH HEADERS FROM ... AS csvLine
  MATCH (nodeX:X {bar: csvLine.xNodeBar})
  MERGE (nodeA:A {foo: csvLine.aNodeFoo})
  MERGE (nodeA) -[:R]-&gt; (nodeX)
  </code></pre>
  
  <p>I tried using OPTIONAL MATCH instead (which finds nothing for nodeX if cell is empty) but then I get an error saying I'm trying to link into a null node.</p>
  
  <p>Hints anyone? Is such a query possible in cypher?</p>