# Having a label as a parameter in a cypher query (efficiently)

**URL:** <https://community.neo4j.com/t/having-a-label-as-a-parameter-in-a-cypher-query-efficiently/26555>\
**Category:** Neo4j Graph Platform\
**Created:** [October 3, 2020, 9:32pm UTC](https://community.neo4j.com/t/having-a-label-as-a-parameter-in-a-cypher-query-efficiently/26555 "2020-10-03T21:32:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![roymaor1](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/roymaor1/32/4107_2.png) [@roymaor1](https://community.neo4j.com/u/roymaor1)\
**Post date:** [October 3, 2020, 9:32pm UTC](https://community.neo4j.com/t/having-a-label-as-a-parameter-in-a-cypher-query-efficiently/26555/1 "2020-10-03T21:32:29Z")

</div>

Hello all,

I am looking for a way to get a label as a string from my user **AND** use the label optimization in my query (rather than have the engine go for a full DB scan).

2 ways I have found so far of doing so (I'll use the WITH keyword to simulate user input):  
1.

```auto
WITH "facebook" as company
MATCH p = (a:company)-->(b:company)
RETURN p

```

This one just won't work, as cypher apparently does not know how to handle form of (node\_name:label\_name) when label\_name is a parameter.

1. 

```auto
WITH "facebook" as company
MATCH p = (a)-->(b)
WHERE ALL (x IN nodes(p) WHERE company IN labels(x))
RETURN p

```

This one works, but performs a full DB scan, not using the label optimization benefit for improved performance.

Any ideas?  
Thanks!

---

<div class="post-metadata">

**Author:** ![ameyasoft](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ameyasoft/32/32639_2.png) [@ameyasoft](https://community.neo4j.com/u/ameyasoft)\
**Post date:** [October 4, 2020, 4:36am UTC](https://community.neo4j.com/t/having-a-label-as-a-parameter-in-a-cypher-query-efficiently/26555/2 "2020-10-04T04:36:59Z")

</div>

It's not clear. Do you have a node with label "facebook"?

---

<div class="post-metadata">

**Author:** ![roymaor1](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/roymaor1/32/4107_2.png) [@roymaor1](https://community.neo4j.com/u/roymaor1)\
**Post date:** [October 4, 2020, 8:09am UTC](https://community.neo4j.com/t/having-a-label-as-a-parameter-in-a-cypher-query-efficiently/26555/3 "2020-10-04T08:09:49Z")

</div>

Yep (just an example for an input string representing a label).  
I'm allowing myself to assume that the given string would always represent a valid label in my graph. (side note: I don't think that really matters - if label doesn't exist in graph and query is valid, it should still run successfully and just return 0 results).

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [October 4, 2020, 10:00am UTC](https://community.neo4j.com/t/having-a-label-as-a-parameter-in-a-cypher-query-efficiently/26555/4 "2020-10-04T10:00:19Z")

</div>

Hello @roymaor1 🙂

I had the same question earlier this week, [in the doc](https://neo4j.com/docs/cypher-manual/current/syntax/parameters/#cypher-parameters-introduction), it says it is not possible to use parameters for labels.

Your second solution is right:

```auto
WITH "facebook" as company
MATCH p = (a)-->(b)
WHERE ALL (x IN nodes(p) WHERE company IN labels(x))
RETURN p

```

But @andrew_bowman advised me to use the other option:

- write the label in the language you use to encapsulate your request, for example Python
- to sanitize what you are appending since this is a cypher injection risk (check that the string you will use is only a label and not something else)

Example:

```auto
label = "facebook"
query = "MATCH p = (a:"+label +")-->(b:"+label +") RETURN p"

```

Regards,  
Cobra

---

<div class="post-metadata">

**Author:** ![roymaor1](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/roymaor1/32/4107_2.png) [@roymaor1](https://community.neo4j.com/u/roymaor1)\
**Post date:** [October 4, 2020, 11:56am UTC](https://community.neo4j.com/t/having-a-label-as-a-parameter-in-a-cypher-query-efficiently/26555/5 "2020-10-04T11:56:55Z")

</div>

That looks good @Cobra. Thanks alot

---

<div class="post-metadata">

**Author:** ![adel1](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/adel1/32/4595_2.png) [@adel1](https://community.neo4j.com/u/adel1)\
**Post date:** [January 3, 2022, 12:58pm UTC](https://community.neo4j.com/t/having-a-label-as-a-parameter-in-a-cypher-query-efficiently/26555/6 "2022-01-03T12:58:30Z")

</div>

I would add the following.

```auto
label = "facebook".Replace('`', '\'').Replace(';', '_')
query = "MATCH p = (a:`"+label +"`)-->(b:"+label +") RETURN p"

```

What we are doing here is we added backticks so even if the label contains illegal characters, they will be executed as string.

```auto
`label`

```

Then we disallow the use of the backtick and semicolon in the label. This should at least allow the script to fail if someone tries to perform injection as they cannot exit the backtick escape and cannot terminate properly resulting in a query the DB will reject.

It is not fail proof though. There will be someone who is smart that can go around this.

---

<div class="post-metadata">

**Author:** ![Raahs](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/raahs/32/21473_2.png) [@Raahs](https://community.neo4j.com/u/Raahs)\
**Post date:** [February 3, 2022, 9:27am UTC](https://community.neo4j.com/t/having-a-label-as-a-parameter-in-a-cypher-query-efficiently/26555/7 "2022-02-03T09:27:35Z")

</div>

I know this is really late but it might help someone who lands where the same way as I did.  
There is an apoc function that does this quite well:

```auto
WITH "facebook" as company
MATCH p = (a)->(b)
WHERE apoc.label.exists(a, company) AND apoc.label.exists(b, company)

```

  
This allows you to provide the label as a parameter since the apoc function accepts a string.

---

<div class="post-metadata">

**Author:** ![dana\_canzano](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/dana_canzano/32/27392_2.png) [@dana\_canzano](https://community.neo4j.com/u/dana_canzano)\
**Post date:** [February 3, 2022, 5:13pm UTC](https://community.neo4j.com/t/having-a-label-as-a-parameter-in-a-cypher-query-efficiently/26555/8 "2022-02-03T17:13:13Z")

</div>

@Raahs

Your last cypher may work but its still going to do a AllNodesScan. If your graph has 100million nodes but only 30k nodes with label `facebook` your query will still examine all 100million nodes so as to find the 30k `facebook` nodes.  
As @Cobra indicated

> > I had the same question earlier this week, [in the doc](https://neo4j.com/docs/cypher-manual/current/syntax/parameters/#cypher-parameters-introduction), it says it is not possible to use parameters for labels.

and this is so since the cypher planner would not now how to necessarily plan the query if a label was defined via a parameter

---

<div class="post-metadata">

**Author:** ![antori](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/antori/32/30043_2.png) [@antori](https://community.neo4j.com/u/antori)\
**Post date:** [November 12, 2023, 4:50pm UTC](https://community.neo4j.com/t/having-a-label-as-a-parameter-in-a-cypher-query-efficiently/26555/9 "2023-11-12T16:50:48Z")

</div>

Very late but maybe apoc.cypher.run may help with this?

```auto
WITH "facebook" as company
CALL apoc.cypher.run("MATCH p = (a:" + company + ")-->(b:" + company + ") RETURN p", {}) YIELD value
RETURN value

```

---

<div class="post-metadata">

**Author:** ![ameyasoft](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ameyasoft/32/32639_2.png) [@ameyasoft](https://community.neo4j.com/u/ameyasoft)\
**Post date:** [November 14, 2023, 6:59am UTC](https://community.neo4j.com/t/having-a-label-as-a-parameter-in-a-cypher-query-efficiently/26555/10 "2023-11-14T06:59:25Z")

</div>

```auto
Try this:

with ["facebook"] as lbl 
MATCH p = (a)-[]-(b)
where labels(a) = labels(b) = lbl
with nodes(p)as n1, relationships(p) as rels
return n1, rels limit 22

```

---

<div class="post-metadata">

**Author:** ![antori](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/antori/32/30043_2.png) [@antori](https://community.neo4j.com/u/antori)\
**Post date:** [November 14, 2023, 7:19am UTC](https://community.neo4j.com/t/having-a-label-as-a-parameter-in-a-cypher-query-efficiently/26555/11 "2023-11-14T07:19:39Z")

</div>

Would not this still do an AllNodesScan?
