# Create a graph with CALL apoc.do.when

**URL:** https://community.neo4j.com/t/create-a-graph-with-call-apoc-do-when/9641
**Category:** Procedures & APOC
**Tags:** apoc, cypher, operations
**Created:** [August 24, 2019, 10:01am UTC](https://community.neo4j.com/t/create-a-graph-with-call-apoc-do-when/9641 "2019-08-24T10:01:17Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![pavloN](https://avatars.discourse-cdn.com/v4/letter/p/a9adbd/32.png) [@pavloN](https://community.neo4j.com/u/pavloN)
#### Post date: [August 24, 2019, 10:01am UTC](https://community.neo4j.com/t/create-a-graph-with-call-apoc-do-when/9641/1 "2019-08-24T10:01:17Z")

</div>

Hi! I need to write a query, where will be created a graph by these rules:

- create three types of nodes:  
if goodParent = true, create the node with ChildGood;  
if goodParent = false, create the node with ChildBad;  
for every animal create a node.

- create relationships between these nodes:  
if animals: bigP - create the "BIG\_P" relationship  
if animals: smallA- create the "SMALL\_A" relationship

```auto
CALL apoc.load.json("fileName")  
YIELD value as v UNWIND v.animals.bigP as p 
 
MERGE (animalP:AnimalP {name:p}) WITH v, animalP

CALL apoc.do.when(v.goodParent=false,  
	'MERGE (childB:ChildBad{name:name}) MERGE (childB)-[:BIG_P]->(animalP)',   
	'MERGE (childG:ChildGood{name:name}) MERGE (childG)-[:BIG_P]->(animalP)',   
	{v: v}) YIELD value  
WITH v, animalP

UNWIND v.animals.smallA as a  
MERGE (animalA:AnimalA {name:a})

WITH v, animalA 

CALL apoc.do.when(v.goodParent=false,  
	'MATCH (childB:ChildBad) 
	MERGE (childB)-[:SMALL_A]->(animalA)',   
	'MATCH (childG:ChildGood) 
	MERGE (childG)-[:SMALL_A]->(animalA)',   
	{v: v}) YIELD value 
RETURN value

```

Why can't I use "apoc.do.when"? It doesn't work here

 ![example](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/7/7af75df954f2efbd33c2b35d3a41bbfca1d60930.png)

---

<div class="post-metadata">

### Author: ![stefan.armbruster](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/stefan.armbruster/32/74_2.png) [@stefan.armbruster](https://community.neo4j.com/u/stefan.armbruster)
#### Post date: [August 24, 2019, 10:30am UTC](https://community.neo4j.com/t/create-a-graph-with-call-apoc-do-when/9641/2 "2019-08-24T10:30:31Z")

</div>

In your first usage of `apoc.do.when` you need to use `v.name` instead of `name` when merging the ChildBad/Good nodes. The inner cypher statement does not have access to the outer namespace, you need to pass in everything you need via the params map.

The 2nd usage of `apoc.do.when` does not use the params ap all. I assum you need to do `MATCH (child:ChildBad[name:v.name})` instead.

---

<div class="post-metadata">

### Author: ![pavloN](https://avatars.discourse-cdn.com/v4/letter/p/a9adbd/32.png) [@pavloN](https://community.neo4j.com/u/pavloN)
#### Post date: [August 24, 2019, 10:57am UTC](https://community.neo4j.com/t/create-a-graph-with-call-apoc-do-when/9641/3 "2019-08-24T10:57:50Z")

</div>

Thank you Stefan!  
Is exist a way to call apoc.do.when second time?  
I'm not fully understand about  
MATCH (child:ChildBad[name:v.name})  
Where put it?

---

<div class="post-metadata">

### Author: ![stefan.armbruster](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/stefan.armbruster/32/74_2.png) [@stefan.armbruster](https://community.neo4j.com/u/stefan.armbruster)
#### Post date: [August 24, 2019, 8:28pm UTC](https://community.neo4j.com/t/create-a-graph-with-call-apoc-do-when/9641/4 "2019-08-24T20:28:54Z")

</div>

Hi Pavlo,

if you return in the first case.do.when the child in both cases under the very same name `child` you don't need the second `case.do.when`:

```auto
YIELD value as v UNWIND v.animals.bigP as p 
 
MERGE (animalP:AnimalP {name:p}) WITH v, animalP

CALL apoc.do.when(v.goodParent=false,  
	'MERGE (childB:ChildBad{name:v.child.name}) MERGE (childB)-[:BIG_P]->(animalP) return childB as child',   
	'MERGE (childG:ChildGood{name:v.child.name}) MERGE (childG)-[:BIG_P]->(animalP) return childG as child',   
	{v: v}) YIELD value  
WITH v, animalP, value.child as child

UNWIND v.animals.smallA as a  
MERGE (animalA:AnimalA {name:a})

WITH v, animalA 
MERGE (child)-[:SMALL_A]->(animalA)
RETURN child, animalA

```

Note: I didn't actually run and test that statement, but the idea should be clear.

---

<div class="post-metadata">

### Author: ![pavloN](https://avatars.discourse-cdn.com/v4/letter/p/a9adbd/32.png) [@pavloN](https://community.neo4j.com/u/pavloN)
#### Post date: [August 27, 2019, 8:40am UTC](https://community.neo4j.com/t/create-a-graph-with-call-apoc-do-when/9641/5 "2019-08-27T08:40:06Z")

</div>

Thank you!  
But It doesn't work.  
If I do it:

```auto
WITH v, animalP, value.child as child

UNWIND v.animals.smallA as a  
MERGE (animalA:AnimalA {name:a})

WITH v, animalA 
MERGE (child)-[:SMALL_A]->(animalA)
RETURN child, animalA

```

There aren't created the relationship :SMALL\_A and nodes AnimalA  
Could I fix it?

---

<div class="post-metadata">

### Author: ![michael.hunger](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/michael.hunger/32/27377_2.png) [@michael.hunger](https://community.neo4j.com/u/michael.hunger)
#### Post date: [August 28, 2019, 11:17pm UTC](https://community.neo4j.com/t/create-a-graph-with-call-apoc-do-when/9641/6 "2019-08-28T23:17:12Z")

</div>

You are not passing `child` through in your last `WITH`

---

<div class="post-metadata">

### Author: ![pavloN](https://avatars.discourse-cdn.com/v4/letter/p/a9adbd/32.png) [@pavloN](https://community.neo4j.com/u/pavloN)
#### Post date: [August 31, 2019, 5:30pm UTC](https://community.neo4j.com/t/create-a-graph-with-call-apoc-do-when/9641/7 "2019-08-31T17:30:33Z")

</div>

Stefan and Michael thank you very much!
