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")
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]);
14
22
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]);
18
-
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
+
xref:../variables-and-params/variables.adoc[Variables] can be added to the `CALL` scope by passing the to the second parameter of the constructor, as an array. 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
+
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]
@@ -74,12 +92,12 @@ CALL (*) {
74
92
75
93
[WARNING]
76
94
====
77
-
This method is deprecated in favor of <<_variable_scope>>
95
+
This method is deprecated in favor of <<_variable_scope>>.
78
96
====
79
97
80
98
[WARNING]
81
99
====
82
-
Import with cannot be used if scope variables are defined and will throw an error.
100
+
`ImportWith` cannot be used if scope variables are defined and will throw an error.
83
101
====
84
102
85
103
@@ -115,11 +133,11 @@ Note how the previous example uses `.concat` to concatenate the first `MATCH` st
115
133
116
134
== `.inTransactions`
117
135
118
-
A `CALL` subquery can be executed in separate transactions by using the `inTransaction` method:
136
+
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]:
119
137
120
138
[source, javascript]
121
139
----
122
-
new Cypher.Match(node).call(deleteSubquery).inTransactions();
140
+
new Call(subquery).inTransactions();
123
141
----
124
142
125
143
[source, cypher]
@@ -129,30 +147,98 @@ CALL {
129
147
} IN TRANSACTIONS
130
148
----
131
149
132
-
The method `inTransaction` accepts an object with the following options:
150
+
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`.
151
+
152
+
[source, javascript]
153
+
----
154
+
new Call(subquery).inTransactions({
155
+
ofRows: 10
156
+
});
157
+
----
158
+
159
+
`inTransactions` supports the following settings:
160
+
161
+
[cols="1,1,1",options="header"]
162
+
|===
163
+
| Setting | Description | Cypher
164
+
| `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`]
165
+
| `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\]`]
166
+
| `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`]
167
+
| `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\]`]
168
+
|===
169
+
170
+
171
+
**Example 1: Concurrent transaction of rows**
172
+
173
+
[source, javascript]
174
+
----
175
+
const clause = new Cypher.Call(subquery).inTransactions({
176
+
ofRows: 10,
177
+
concurrentTransactions: 5
178
+
});
179
+
----
180
+
181
+
182
+
[source, cypher]
183
+
----
184
+
CALL {
185
+
// subquery
186
+
} IN 5 CONCURRENT TRANSACTIONS OF 10 ROWS
187
+
----
188
+
189
+
**Example 2: Retry with maximum duration**
190
+
191
+
[source, javascript]
192
+
----
193
+
const clause = new Cypher.Call(subquery).inTransactions({
194
+
retry: 10
195
+
});
196
+
----
197
+
198
+
199
+
[source, cypher]
200
+
----
201
+
CALL {
202
+
// subquery
203
+
} TRANSACTIONS ON ERROR RETRY FOR 10 SECONDS
204
+
----
205
+
206
+
**Example 3: Retry error fallback**
207
+
208
+
[source, javascript]
209
+
----
210
+
const clause = new Cypher.Call(subquery).inTransactions({
211
+
retry: true,
212
+
onError: "continue"
213
+
});
214
+
----
215
+
216
+
217
+
[source, cypher]
218
+
----
219
+
CALL {
220
+
// subquery
221
+
} TRANSACTIONS ON ERROR RETRY THEN CONTINUE
222
+
----
133
223
134
-
* `ofRows`: A number to define the batch of rows. Translates to `IN TRANSACTIONS OF 10 ROWS`.
135
-
* `onError`: A behavior for error handling. This can be `continue`, `break` or `fail`. Translates to `ON ERROR CONTINUE`.
136
-
* `concurrentTransactions`: A number to execute concurrent transactions. Translates to `IN x CONCURRENT TRANSACTIONS`.
137
-
* `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]`.
138
224
139
225
== Optional Call
140
226
141
-
A `CALL` subquery can be converted to an `OPTIONAL CALL` by using the `.optional` method:
227
+
The method `.optional()` transforms a `CALL` subquery into link:https://neo4j.com/docs/cypher-manual/current/subqueries/call-subquery/#optional-call[`OPTIONAL CALL`] subquery.
142
228
143
229
[source, javascript]
144
230
----
145
-
new Cypher.Call(deleteSubquery).optional();
231
+
new Cypher.Call(subquery).optional();
146
232
----
147
233
148
-
Alternatively, the clause `OptionalCall` can be used to create an `OPTIONAL CALL` directly:
234
+
Alternatively, the clause `OptionalCall` creates an `OPTIONAL CALL` directly:
0 commit comments