Skip to content

Commit ba2517a

Browse files
authored
Fix graph Direction serialization
1 parent f852c67 commit ba2517a

File tree

6 files changed

+88
-5
lines changed

6 files changed

+88
-5
lines changed

lib/datastax/graph/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,11 @@ export namespace graph {
8282
const label: EnumValue;
8383
const value: EnumValue;
8484
}
85+
86+
namespace direction {
87+
// `in` is a reserved word
88+
const in_: EnumValue;
89+
const out: EnumValue;
90+
const both: EnumValue;
91+
}
8592
}

lib/datastax/graph/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ const t = {
4646
value: new EnumValue('T', 'value'),
4747
};
4848

49+
/**
50+
* Represents the edge direction.
51+
*/
52+
const direction = {
53+
'both': new EnumValue('Direction', 'BOTH'),
54+
'in': new EnumValue('Direction', 'IN'),
55+
'out': new EnumValue('Direction', 'OUT')
56+
};
57+
58+
// `in` is a reserved keyword depending on the context
59+
// TinkerPop JavaScript GLV only exposes `in` but it can lead to issues for TypeScript users and others.
60+
// Expose an extra property to represent `Direction.IN`.
61+
direction.in_ = direction.in;
62+
4963
module.exports = {
5064
Edge,
5165
Element,
@@ -59,6 +73,7 @@ module.exports = {
5973
asFloat,
6074
asTimestamp,
6175
asUdt,
76+
direction,
6277
getCustomTypeSerializers,
6378
GraphResultSet,
6479
GraphTypeWrapper,

lib/datastax/graph/type-serializers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@
3030
// Replace dependencies to minimize code changes from Apache TinkerPop
3131
const t = {
3232
P: UnsupportedType, TextP: UnsupportedType, Traversal: UnsupportedType, Traverser: UnsupportedType,
33-
EnumValue: UnsupportedType, direction: {}
33+
EnumValue: UnsupportedType
3434
};
3535
const ts = { TraversalStrategy: UnsupportedType };
3636
const Bytecode = UnsupportedType;
3737
const g = require('./index');
3838
const utils = { Long: UnsupportedType };
3939
t.t = g.t;
40+
t.direction = g.direction;
4041

4142
function UnsupportedType() { }
4243

test/integration/short/graph/graph-tests.js

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const { InetAddress, Uuid, Tuple } = types;
2828
const ExecutionProfile = require('../../../../lib/execution-profile').ExecutionProfile;
2929
const utils = require('../../../../lib/utils');
3030
const graphModule = require('../../../../lib/datastax/graph');
31-
const { asInt, asFloat, asUdt, t } = graphModule;
31+
const { asInt, asFloat, asUdt, t, Edge, direction } = graphModule;
3232
const { graphProtocol } = require('../../../../lib/datastax/graph/options');
3333
const graphTestHelper = require('./graph-test-helper');
3434

@@ -857,16 +857,25 @@ vdescribe('dse-5.0', 'Client @SERVER_API', function () {
857857
return client.executeGraph(query, null, { graphName });
858858
});
859859

860-
before(() => {
861-
const createLabelQuery = `schema.vertexLabel('label_test').ifNotExists().partitionBy('id', Int)
860+
before(async () => {
861+
const createVertexLabel1Query = `schema.vertexLabel('label_test').ifNotExists().partitionBy('id', Int)
862862
.property('prop_double', Double)
863863
.property('prop_text', Text)
864864
.property('prop_uuid', Uuid)
865865
.property('prop_long', Bigint)
866866
.property('prop_blob', Blob)
867867
.property('prop_duration', Duration)
868868
.create()`;
869-
return client.executeGraph(createLabelQuery, null, { graphName });
869+
const createVertexLabel2Query = `schema.vertexLabel('label_test2').ifNotExists().partitionBy('id', Int)
870+
.property('prop_text', Text)
871+
.create()`;
872+
const createEdgeLabelQuery = `schema.edgeLabel('created')
873+
.from('label_test').to('label_test2')
874+
.property('tag', Text)
875+
.create()`;
876+
await client.executeGraph(createVertexLabel1Query, null, { graphName });
877+
await client.executeGraph(createVertexLabel2Query, null, { graphName });
878+
await client.executeGraph(createEdgeLabelQuery, null, { graphName });
870879
});
871880

872881
after(() => client.shutdown());
@@ -911,6 +920,43 @@ vdescribe('dse-5.0', 'Client @SERVER_API', function () {
911920
assert.deepEqual(rs.toArray(), ['value1']);
912921
});
913922

923+
it('should be able to add a edge and retrieve it', async () => {
924+
const id1 = schemaCounter++;
925+
const id2 = schemaCounter++;
926+
const addTraversal = `g.addV('label_test')
927+
.property('id', ${id1})
928+
.property('prop_text', 'value1')
929+
.as('first')
930+
.addV('label_test2')
931+
.property('id', ${id2})
932+
.property('prop_text', 'value2')
933+
.as('second')
934+
.addE('created').from('first').to('second').property('tag', 'sample')`;
935+
await client.executeGraph(addTraversal, null, { graphName });
936+
937+
const retrieveMapTraversal = `g.V().hasLabel('label_test').has('id', ${id1}).outE('created').elementMap()`;
938+
let rs = await client.executeGraph(retrieveMapTraversal, null, { executionProfile: 'profile1' });
939+
const map = rs.first();
940+
assert.instanceOf(map, Map);
941+
assert.equal(map.get('tag'), 'sample');
942+
assert.equal(map.get(t.label), 'created');
943+
const outMap = map.get(direction.out);
944+
const inMap = map.get(direction.in);
945+
assert.instanceOf(outMap, Map);
946+
assert.equal(outMap.get(t.label), 'label_test');
947+
assert.instanceOf(inMap, Map);
948+
assert.equal(inMap.get(t.label), 'label_test2');
949+
950+
const retrieveEdgeTraversal = `g.V().hasLabel('label_test').has('id', ${id1}).outE('created')`;
951+
rs = await client.executeGraph(retrieveEdgeTraversal, null, { executionProfile: 'profile1' });
952+
const edge = rs.first();
953+
assert.instanceOf(edge, Edge);
954+
assert.equal(edge.label, 'created');
955+
assert.equal(edge.outVLabel, 'label_test');
956+
assert.equal(edge.inVLabel, 'label_test2');
957+
assert.typeOf(edge.outV, 'string');
958+
});
959+
914960
it('should support named parameters', async () => {
915961
const id = schemaCounter++;
916962
const value = 'value2';

test/unit/api-tests.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ describe('API', function () {
8585
assert.equal(api.datastax.graph.t[name].toString(), name);
8686
});
8787

88+
[
89+
'in',
90+
'out',
91+
'both'
92+
].forEach(name => {
93+
assert.isObject(api.datastax.graph.direction[name]);
94+
assert.equal(api.datastax.graph.direction[name].toString().toLowerCase(), name);
95+
});
96+
97+
assert.equal(api.datastax.graph.direction['in_'].toString(), 'IN');
98+
8899
checkConstructor(api.datastax.graph, 'UdtGraphWrapper');
89100
checkConstructor(api.datastax.graph, 'GraphTypeWrapper');
90101
});

test/unit/typescript/graph-tests.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@ async function myTest(client: Client): Promise<any> {
3939
let tokenString: string;
4040
tokenString = datastax.graph.t.id.toString();
4141
tokenString = datastax.graph.t.label.toString();
42+
tokenString = datastax.graph.direction.in_.toString();
43+
tokenString = datastax.graph.direction.out.toString();
44+
tokenString = datastax.graph.direction.both.toString();
4245
}

0 commit comments

Comments
 (0)