Skip to content

Commit f1d1498

Browse files
authored
Merge pull request #57 from Bit-Quill/nodeEdgeLabelCollision
Alias edges with same label as a node
2 parents 50a22e4 + 0b901f2 commit f1d1498

4 files changed

+104
-2
lines changed

src/graphdb.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
99
express or implied. See the License for the specific language governing
1010
permissions and limitations under the License.
1111
*/
12-
import { loggerInfo } from './logger.js';
12+
import { loggerDebug, loggerInfo } from './logger.js';
1313

1414
let changeCase = true;
1515

@@ -19,6 +19,7 @@ function checkForDuplicateNames(schema) {
1919
let pascalCase = toPascalCase(node.label);
2020
if (names.includes(pascalCase)) {
2121
changeCase = false;
22+
loggerDebug(`Node label '${node.label}' was detected as a duplicate. It is recommended to resolve duplicate labels.`, {toConsole: true});
2223
} else {
2324
names.push(pascalCase);
2425
}
@@ -28,6 +29,7 @@ function checkForDuplicateNames(schema) {
2829
let pascalCase = toPascalCase(edge.label);
2930
if (names.includes(pascalCase)) {
3031
changeCase = false;
32+
loggerDebug(`Edge label '${edge.label}' was detected as a duplicate. It is recommended to resolve duplicate labels.`, {toConsole: true});
3133
} else {
3234
names.push(pascalCase);
3335
}
@@ -192,10 +194,17 @@ function graphDBInferenceSchema (graphbSchema, addMutations) {
192194
r += '}\n\n';
193195
})
194196

197+
const nodeLabels = new Set(gdbs.nodeStructures.map((n) => n.label));
198+
195199
// edge types
196200
gdbs.edgeStructures.forEach(edge => {
197201
// edge type
198-
let edgeCase = cleanseLabel(edge.label);
202+
203+
// resolve potential conflict between the edge label and node label by prefixing with an underscore
204+
let edgeCase = nodeLabels.has(edge.label)
205+
? `_${cleanseLabel(edge.label)}`
206+
: cleanseLabel(edge.label);
207+
199208
if (changeCase) {
200209
edgeCase = toPascalCase(edgeCase);
201210
}

src/test/graphdb.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { graphDBInferenceSchema } from '../graphdb.js';
22
import fs from "fs";
3+
import { loggerInit } from "../logger.js";
34

45
test('node with same property and edge label should add underscore prefix', () => {
56
expect(graphDBInferenceSchema(readFile('./src/test/node-edge-same-property-neptune-schema.json'), false)).toContain('_commonName:Commonname');
@@ -47,6 +48,13 @@ test('should output security graph schema', () => {
4748
expect(actual).toBe(expected);
4849
});
4950

51+
test('should alias edge with same label as node', () => {
52+
loggerInit('./src/test/output', false, 'info');
53+
const actual = inferGraphQLSchema('./src/test/node-edge-same-label-neptune-schema.json');
54+
const expected = loadGraphQLSchema('./src/test/node-edge-same-label.graphql')
55+
expect(actual).toBe(expected);
56+
});
57+
5058
function inferGraphQLSchema(neptuneSchemaFilePath) {
5159
let neptuneSchema = readFile(neptuneSchemaFilePath);
5260
let inferredSchema = graphDBInferenceSchema(neptuneSchema);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"nodeStructures": [
3+
{
4+
"label": "post",
5+
"properties": [
6+
{
7+
"name": "text",
8+
"type": "String"
9+
}
10+
]
11+
},
12+
{
13+
"label": "author",
14+
"properties": [
15+
{
16+
"name": "username",
17+
"type": "String"
18+
}
19+
]
20+
}
21+
],
22+
"edgeStructures": [
23+
{
24+
"label": "author",
25+
"properties": [
26+
{
27+
"name": "date",
28+
"type": "Date"
29+
}
30+
],
31+
"directions": [
32+
{
33+
"from": "post",
34+
"to": "author",
35+
"relationship": "ONE-ONE"
36+
}
37+
]
38+
}
39+
]
40+
}

src/test/node-edge-same-label.graphql

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
type post {
2+
_id: ID! @id
3+
text: String
4+
author:author
5+
}
6+
7+
input postInput {
8+
_id: ID @id
9+
text: String
10+
}
11+
12+
type author {
13+
_id: ID! @id
14+
username: String
15+
author:author
16+
}
17+
18+
input authorInput {
19+
_id: ID @id
20+
username: String
21+
}
22+
23+
type _author @alias(property:"author") {
24+
_id: ID! @id
25+
date: Date
26+
}
27+
28+
input _authorInput {
29+
date: Date
30+
}
31+
32+
input Options {
33+
limit:Int
34+
}
35+
36+
type Query {
37+
getNodepost(filter: postInput): post
38+
getNodeposts(filter: postInput, options: Options): [post]
39+
getNodeauthor(filter: authorInput): author
40+
getNodeauthors(filter: authorInput, options: Options): [author]
41+
}
42+
43+
schema {
44+
query: Query
45+
}

0 commit comments

Comments
 (0)