Skip to content

Commit 0ee7bfd

Browse files
Integrate JavaScript examples into CI build
- Add examples execution to gremlin-go-integration-tests container - Make server URLs and vertex labels configurable via environment variables - Build fails if any example fails to execute - Improve consistency between root-level and glv-level examples
1 parent 2cd2748 commit 0ee7bfd

File tree

7 files changed

+56
-33
lines changed

7 files changed

+56
-33
lines changed

gremlin-examples/gremlin-javascript/basic-gremlin.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,29 @@ const gremlin = require('gremlin');
2121
const traversal = gremlin.process.AnonymousTraversalSource.traversal;
2222
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
2323

24+
const serverUrl = 'ws://localhost:8182/gremlin';
25+
const vertexLabel = 'person';
26+
2427
async function main() {
25-
const dc = new DriverRemoteConnection('ws://localhost:8182/gremlin');
28+
const dc = new DriverRemoteConnection(serverUrl);
2629
const g = traversal().withRemote(dc);
2730

2831
// Basic Gremlin: adding and retrieving data
29-
const v1 = await g.addV('person').property('name','marko').next();
30-
const v2 = await g.addV('person').property('name','stephen').next();
31-
const v3 = await g.addV('person').property('name','vadas').next();
32+
const v1 = await g.addV(vertexLabel).property('name','marko').next();
33+
const v2 = await g.addV(vertexLabel).property('name','stephen').next();
34+
const v3 = await g.addV(vertexLabel).property('name','vadas').next();
3235

3336
// Be sure to use a terminating step like next() or iterate() so that the traversal "executes"
3437
// Iterate() does not return any data and is used to just generate side-effects (i.e. write data to the database)
3538
await g.V(v1.value).addE('knows').to(v2.value).property('weight',0.75).iterate();
3639
await g.V(v1.value).addE('knows').to(v3.value).property('weight',0.75).iterate();
3740

3841
// Retrieve the data from the "marko" vertex
39-
const marko = await g.V().has('person','name','marko').values('name').toList();
42+
const marko = await g.V().has(vertexLabel,'name','marko').values('name').toList();
4043
console.log("name: " + marko[0]);
4144

4245
// Find the "marko" vertex and then traverse to the people he "knows" and return their data
43-
const peopleMarkoKnows = await g.V().has('person','name','marko').out('knows').values('name').toList();
46+
const peopleMarkoKnows = await g.V().has(vertexLabel,'name','marko').out('knows').values('name').toList();
4447
peopleMarkoKnows.forEach((person) => {
4548
console.log("marko knows " + person);
4649
});

gremlin-examples/gremlin-javascript/connections.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@ const traversal = gremlin.process.AnonymousTraversalSource.traversal;
2222
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
2323
const serializer = gremlin.structure.io.graphserializer;
2424

25+
const serverUrl = 'ws://localhost:8182/gremlin';
26+
const vertexLabel = 'connection';
27+
2528
async function main() {
2629
await withRemote();
2730
await withConfigs();
2831
}
2932

3033
async function withRemote() {
3134
// Connecting to the server
32-
const dc = new DriverRemoteConnection('ws://localhost:8182/gremlin');
35+
const dc = new DriverRemoteConnection(serverUrl);
3336
const g = traversal().withRemote(dc);
3437

35-
// Drop existing vertices
36-
await g.V().drop().iterate();
37-
3838
// Simple query to verify connection
39-
const v = await g.addV().iterate();
40-
const count = await g.V().count().next();
39+
const v = await g.addV(vertexLabel).iterate();
40+
const count = await g.V().hasLabel(vertexLabel).count().next();
4141
console.log("Vertex count: " + count.value);
4242

4343
// Cleanup
@@ -46,7 +46,7 @@ async function withRemote() {
4646

4747
async function withConfigs() {
4848
// Connecting and customizing configurations
49-
const dc = new DriverRemoteConnection('ws://localhost:8182/gremlin', {
49+
const dc = new DriverRemoteConnection(serverUrl, {
5050
mimeType: 'application/vnd.gremlin-v3.0+json',
5151
reader: serializer,
5252
writer: serializer,
@@ -55,8 +55,8 @@ async function withConfigs() {
5555
});
5656
const g = traversal().withRemote(dc);
5757

58-
const v = await g.addV().iterate();
59-
const count = await g.V().count().next();
58+
const v = await g.addV(vertexLabel).iterate();
59+
const count = await g.V().hasLabel(vertexLabel).count().next();
6060
console.log("Vertex count: " + count.value);
6161

6262
await dc.close();

gremlin-examples/gremlin-javascript/modern-traversals.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
2424
const p = gremlin.process.P;
2525
const t = gremlin.process.t;
2626

27+
const serverUrl = 'ws://localhost:8182/gremlin';
28+
2729
async function main() {
2830
/*
2931
This example requires the Modern toy graph to be preloaded upon launching the Gremlin server.
3032
For details, see https://tinkerpop.apache.org/docs/current/reference/#gremlin-server-docker-image and use
3133
conf/gremlin-server-modern.yaml.
3234
*/
33-
const dc = new DriverRemoteConnection('ws://localhost:8182/gremlin');
35+
const dc = new DriverRemoteConnection(serverUrl);
3436
const g = traversal().withRemote(dc);
3537

3638
const e1 = await g.V(1).bothE().toList(); // (1)

gremlin-javascript/examples/node/basic-gremlin.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,29 @@ const gremlin = require('gremlin');
2121
const traversal = gremlin.process.AnonymousTraversalSource.traversal;
2222
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
2323

24+
const serverUrl = process.env.GREMLIN_SERVER_URL || 'ws://localhost:8182/gremlin';
25+
const vertexLabel = process.env.VERTEX_LABEL || 'person';
26+
2427
async function main() {
25-
const dc = new DriverRemoteConnection('ws://localhost:8182/gremlin');
28+
const dc = new DriverRemoteConnection(serverUrl);
2629
const g = traversal().withRemote(dc);
2730

2831
// Basic Gremlin: adding and retrieving data
29-
const v1 = await g.addV('person').property('name','marko').next();
30-
const v2 = await g.addV('person').property('name','stephen').next();
31-
const v3 = await g.addV('person').property('name','vadas').next();
32+
const v1 = await g.addV(vertexLabel).property('name','marko').next();
33+
const v2 = await g.addV(vertexLabel).property('name','stephen').next();
34+
const v3 = await g.addV(vertexLabel).property('name','vadas').next();
3235

3336
// Be sure to use a terminating step like next() or iterate() so that the traversal "executes"
3437
// Iterate() does not return any data and is used to just generate side-effects (i.e. write data to the database)
3538
await g.V(v1.value).addE('knows').to(v2.value).property('weight',0.75).iterate();
3639
await g.V(v1.value).addE('knows').to(v3.value).property('weight',0.75).iterate();
3740

3841
// Retrieve the data from the "marko" vertex
39-
const marko = await g.V().has('person','name','marko').values('name').toList();
42+
const marko = await g.V().has(vertexLabel,'name','marko').values('name').toList();
4043
console.log("name: " + marko[0]);
4144

4245
// Find the "marko" vertex and then traverse to the people he "knows" and return their data
43-
const peopleMarkoKnows = await g.V().has('person','name','marko').out('knows').values('name').toList();
46+
const peopleMarkoKnows = await g.V().has(vertexLabel,'name','marko').out('knows').values('name').toList();
4447
peopleMarkoKnows.forEach((person) => {
4548
console.log("marko knows " + person);
4649
});

gremlin-javascript/examples/node/connections.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@ const traversal = gremlin.process.AnonymousTraversalSource.traversal;
2222
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
2323
const serializer = gremlin.structure.io.graphserializer;
2424

25+
const serverUrl = process.env.GREMLIN_SERVER_URL || 'ws://localhost:8182/gremlin';
26+
const vertexLabel = process.env.VERTEX_LABEL || 'connection';
27+
2528
async function main() {
2629
await withRemote();
2730
await withConfigs();
2831
}
2932

3033
async function withRemote() {
3134
// Connecting to the server
32-
const dc = new DriverRemoteConnection('ws://localhost:8182/gremlin');
35+
const dc = new DriverRemoteConnection(serverUrl);
3336
const g = traversal().withRemote(dc);
3437

35-
// Drop existing vertices
36-
await g.V().drop().iterate();
37-
3838
// Simple query to verify connection
39-
const v = await g.addV().iterate();
40-
const count = await g.V().count().next();
39+
const v = await g.addV(vertexLabel).iterate();
40+
const count = await g.V().hasLabel(vertexLabel).count().next();
4141
console.log("Vertex count: " + count.value);
4242

4343
// Cleanup
@@ -46,7 +46,7 @@ async function withRemote() {
4646

4747
async function withConfigs() {
4848
// Connecting and customizing configurations
49-
const dc = new DriverRemoteConnection('ws://localhost:8182/gremlin', {
49+
const dc = new DriverRemoteConnection(serverUrl, {
5050
mimeType: 'application/vnd.gremlin-v3.0+json',
5151
reader: serializer,
5252
writer: serializer,
@@ -55,8 +55,8 @@ async function withConfigs() {
5555
});
5656
const g = traversal().withRemote(dc);
5757

58-
const v = await g.addV().iterate();
59-
const count = await g.V().count().next();
58+
const v = await g.addV(vertexLabel).iterate();
59+
const count = await g.V().hasLabel(vertexLabel).count().next();
6060
console.log("Vertex count: " + count.value);
6161

6262
await dc.close();

gremlin-javascript/examples/node/modern-traversals.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@ const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
2424
const p = gremlin.process.P;
2525
const t = gremlin.process.t;
2626

27+
const serverUrl = process.env.GREMLIN_SERVER_URL || 'ws://localhost:8182/gremlin';
28+
2729
async function main() {
2830
/*
2931
This example requires the Modern toy graph to be preloaded upon launching the Gremlin server.
3032
For details, see https://tinkerpop.apache.org/docs/current/reference/#gremlin-server-docker-image and use
3133
conf/gremlin-server-modern.yaml.
3234
*/
33-
const dc = new DriverRemoteConnection('ws://localhost:8182/gremlin');
35+
36+
// Use gmodern in CI environment, default connection locally
37+
const options = process.env.DOCKER_ENVIRONMENT ? { traversalSource: 'gmodern' } : {};
38+
const dc = new DriverRemoteConnection(serverUrl, options);
3439
const g = traversal().withRemote(dc);
3540

3641
const e1 = await g.V(1).bothE().toList(); // (1)

gremlin-javascript/src/main/javascript/gremlin-javascript/docker-compose.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,22 @@ services:
4949
- ../../../../../gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/test/features:/gremlin-test
5050
- ../../../../../docker/gremlin-test-server:/js_app/gremlin-test-server
5151
- ../../../../../gremlin-tools/gremlin-socket-server/conf:/js_app/gremlin-socket-server/conf/
52+
- ../../../..:/gremlin-javascript
5253
environment:
5354
- DOCKER_ENVIRONMENT=true
55+
- GREMLIN_SERVER_URL=ws://gremlin-server-test-js:45940/gremlin
56+
- VERTEX_LABEL=javascript-example
5457
working_dir: /js_app
5558
command: >
5659
bash -c "npm config set cache /tmp --global
57-
&& npm ci && npm run test && npm run features-docker"
60+
&& npm ci && npm run test && npm run features-docker
61+
&& echo 'Running examples'
62+
&& cd /gremlin-javascript/src/main/javascript/gremlin-javascript && npm ci
63+
&& cd /gremlin-javascript/examples/node && npm ci
64+
&& node basic-gremlin.js
65+
&& node connections.js
66+
&& node modern-traversals.js
67+
&& echo 'All examples completed successfully'"
5868
depends_on:
5969
gremlin-server-test-js:
6070
condition: service_healthy

0 commit comments

Comments
 (0)