You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
:description: This page describes how to create CALL subqueries with the Cypher Builder.
3
3
= `Call`
4
4
5
-
Cypher link:https://neo4j.com/docs/cypher-manual/current/subqueries/call-subquery/[`CALL` subqueries] can be created with `new Cypher.Call()` in Cypher Builder.
6
-
To do this, a valid query needs to be passed to `Call`, for example:
5
+
`Cypher.Call` generates Cypher link:https://neo4j.com/docs/cypher-manual/current/subqueries/call-subquery/[`CALL` subqueries]. To do this, instantiate `Cypher.Call` with a valid subquery as parameter in the constructor.
6
+
7
+
[source, javascript]
8
+
----
9
+
new Cypher.Call(subquery);
10
+
----
11
+
12
+
In the following example, `Cypher.Call` receives a `Cypher.Match` clause.
7
13
8
14
[source, javascript]
9
15
----
10
16
const dog = new Cypher.Node({ labels: ["Dog"] });
11
17
const person = new Cypher.Node({ labels: ["Person"] });
18
+
const dogName = new Cypher.NamedVariable("dogName");
12
19
13
-
const dogName = new Cypher.NamedVariable("dogName")
14
-
15
-
const subquery = new Cypher.Match(
16
-
new Cypher.Pattern(person).related(new Cypher.Relationship({ type: "HAS_DOG" })).to(dog)
17
-
).return([dog.property("name"), dogName]);
20
+
const pattern = new Cypher.Pattern(person).related(new Cypher.Relationship({ type: "HAS_DOG" })).to(dog)
21
+
const subquery = new Cypher.Match(pattern).return([dog.property("name"), dogName]);
18
22
19
-
const classClause = new Cypher.Call(subquery).return(dogName);
23
+
const callClause = new Cypher.Call(subquery).return(dogName);
24
+
const { cypher, params } = callClause.build();
20
25
----
21
26
27
+
This example builds a `CALL` clause with the `MATCH` subquery inside.
28
+
22
29
[source, cypher]
23
30
----
24
31
CALL {
@@ -30,20 +37,29 @@ RETURN dogName
30
37
31
38
== Variable scope
32
39
33
-
The variable scope can be set with the second parameter of `new Cypher.Call()`, by passing an array of variables:
40
+
`Call` clauses accept an array of xref:../variables-and-params/variables.adoc[Cypher Variables] as the second parameter of its constructor. These variables will be available within the scope of the subquery in the generated Cypher.
41
+
42
+
[source, javascript]
43
+
----
44
+
new Cypher.Call(subquery, [var1, var2]);
45
+
----
46
+
47
+
For example, the following code will make two node variables available in the `CREATE` statement inside `CALL`.
34
48
35
49
[source, javascript]
36
50
----
37
51
const movieNode = new Cypher.Node();
38
52
const actorNode = new Cypher.Node();
39
53
40
-
const clause = new Cypher.Call(new Cypher.Create(new Cypher.Pattern(movieNode).related().to(actorNode)), [
54
+
const createSubquery = new Cypher.Create(new Cypher.Pattern(movieNode).related().to(actorNode));
55
+
56
+
const clause = new Cypher.Call(createSubquery, [
41
57
movieNode,
42
58
actorNode,
43
59
]);
44
60
----
45
61
46
-
This will add the two variables `movieNode` and `actorNode` to the `CALL` scope:
62
+
The resulting Cypher adds the two variables `movieNode` (`this0`) and `actorNode` (`this1`) to the `CALL` scope:
47
63
48
64
[source, cypher]
49
65
----
@@ -52,14 +68,16 @@ CALL (this0, this1) {
52
68
}
53
69
----
54
70
55
-
To import all variables from the outer scope, the second parameter can be set to the string `"*"`:
71
+
To import all variables from the outer scope, set the second parameter to the string `"*"`:
56
72
57
73
[source, javascript]
58
74
----
59
75
const movieNode = new Cypher.Node();
60
76
const actorNode = new Cypher.Node();
61
77
62
-
const clause = new Cypher.Call(new Cypher.Create(new Cypher.Pattern(movieNode).related().to(actorNode)), "*");
78
+
const createSubquery = new Cypher.Create(new Cypher.Pattern(movieNode).related().to(actorNode));
79
+
80
+
const clause = new Cypher.Call(createSubquery, "*");
63
81
----
64
82
65
83
[source, cypher]
@@ -73,7 +91,12 @@ CALL (*) {
73
91
74
92
[WARNING]
75
93
====
76
-
Import with cannot be used if scope variables are defined and will throw an error
94
+
This method is deprecated in favor of <<_variable_scope>>.
95
+
====
96
+
97
+
[WARNING]
98
+
====
99
+
ImportWith cannot be used if scope variables are defined and will throw an error.
77
100
====
78
101
79
102
@@ -109,11 +132,11 @@ Note how the previous example uses `.concat` to concatenate the first `MATCH` st
109
132
110
133
== `.inTransactions`
111
134
112
-
A `CALL` subquery can be executed in separate transactions by using the `inTransaction` method:
135
+
The method `.inTransactions` appends the modifier `IN TRANSACTIONS` at the end of the `CALL` subquery, this way subqueries will link:https://neo4j.com/docs/cypher-manual/current/subqueries/subqueries-in-transactions/[execute in separate transactions]:
113
136
114
137
[source, javascript]
115
138
----
116
-
new Cypher.Match(node).call(deleteSubquery).inTransactions();
139
+
new Call(subquery).inTransactions();
117
140
----
118
141
119
142
[source, cypher]
@@ -123,30 +146,98 @@ CALL {
123
146
} IN TRANSACTIONS
124
147
----
125
148
126
-
The method `inTransaction` accepts an object with the following options:
149
+
The method `inTransactions` accepts multiple settings to change the behaviour of the transactions, such as, error handling, rows per transaction, or concurrency. You can pass one or more settings, as an object, to `.inTransaction`.
150
+
151
+
[source, javascript]
152
+
----
153
+
new Call(subquery).inTransactions({
154
+
ofRows: 10
155
+
});
156
+
----
157
+
158
+
`inTransactions` supports the following settings:
159
+
160
+
[cols="1,1,1",options="header"]
161
+
|===
162
+
| Setting | Description | Cypher
163
+
| `ofRows` | Define the number of rows per transaction | link:https://neo4j.com/docs/cypher-manual/current/subqueries/subqueries-in-transactions/#batching[`IN TRANSACTIONS OF [n\] ROWS`]
164
+
| `onError` | Error handling behaviour, can be `continue`, `break` or `fail` | link:https://neo4j.com/docs/cypher-manual/current/subqueries/subqueries-in-transactions/#error-behavior[`ON ERROR [CONTINUE \| BREAK \| FAIL\]`]
165
+
| `concurrentTransactions` | The maximum number of transactions to execute concurrently | link:https://neo4j.com/docs/cypher-manual/current/subqueries/subqueries-in-transactions/#concurrent-transactions[`IN [n\] CONCURRENT TRANSACTIONS`]
166
+
| `retry` | If set to `true`, retries failing transactions. Can be a number to define the maximum duration, in seconds. | link:https://neo4j.com/docs/cypher-manual/current/subqueries/subqueries-in-transactions/#on-error-retry[`ON ERROR RETRY [FOR x SECONDS\]`]
167
+
|===
168
+
169
+
170
+
**Example 1: Concurrent transaction of rows**
171
+
172
+
[source, javascript]
173
+
----
174
+
const clause = new Cypher.Call(subquery).inTransactions({
175
+
ofRows: 10,
176
+
concurrentTransactions: 5
177
+
});
178
+
----
179
+
180
+
181
+
[source, cypher]
182
+
----
183
+
CALL {
184
+
// subquery
185
+
} IN 5 CONCURRENT TRANSACTIONS OF 10 ROWS
186
+
----
187
+
188
+
**Example 2: Retry with maximum duration**
189
+
190
+
[source, javascript]
191
+
----
192
+
const clause = new Cypher.Call(subquery).inTransactions({
193
+
retry: 10
194
+
});
195
+
----
196
+
197
+
198
+
[source, cypher]
199
+
----
200
+
CALL {
201
+
// subquery
202
+
} TRANSACTIONS ON ERROR RETRY FOR 10 SECONDS
203
+
----
204
+
205
+
**Example 3: Retry error fallback**
206
+
207
+
[source, javascript]
208
+
----
209
+
const clause = new Cypher.Call(subquery).inTransactions({
210
+
retry: true,
211
+
onError: "continue"
212
+
});
213
+
----
214
+
215
+
216
+
[source, cypher]
217
+
----
218
+
CALL {
219
+
// subquery
220
+
} TRANSACTIONS ON ERROR RETRY THEN CONTINUE
221
+
----
127
222
128
-
* `ofRows`: A number to define the batch of rows. Translates to `IN TRANSACTIONS OF 10 ROWS`.
129
-
* `onError`: A behavior for error handling. This can be `continue`, `break` or `fail`. Translates to `ON ERROR CONTINUE`.
130
-
* `concurrentTransactions`: A number to execute concurrent transactions. Translates to `IN x CONCURRENT TRANSACTIONS`.
131
-
* `retry`: Either a boolean or a number. Translates to `ON ERROR RETRY [FOR x SECONDS]`. If `onError` is also defined, it will add `THEN [error]`.
132
223
133
224
== Optional Call
134
225
135
-
A `CALL` subquery can be converted to an `OPTIONAL CALL` by using the `.optional` method:
226
+
The method `.optional()` transforms a `CALL` subquery into link:https://neo4j.com/docs/cypher-manual/current/subqueries/call-subquery/#optional-call[`OPTIONAL CALL`] subquery.
136
227
137
228
[source, javascript]
138
229
----
139
-
new Cypher.Call(deleteSubquery).optional();
230
+
new Cypher.Call(subquery).optional();
140
231
----
141
232
142
-
Alternatively, the clause `OptionalCall` can be used to create an `OPTIONAL CALL` directly:
233
+
Alternatively, the clause `OptionalCall` creates an `OPTIONAL CALL` directly:
0 commit comments