Skip to content

Commit 1ad3f20

Browse files
epii-1keski
andauthored
Minor edge validation optimization (#98)
* Implemented a small optimization to edge validation * Updated changelog to reflect the minor optimization Co-authored-by: Robin Keskisärkkä <[email protected]>
1 parent a040baa commit 1ad3f20

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
**v0.1.2, to be released on ???**
22
* Bug fix: Checks for existence of the database before attempting to drop it. See: https://github.com/LiUGraphQL/woo.sh/issues/89
3+
* New feature: Changed Edge validation behaviour to not check for existance of documents created in the same mutation. See: https://github.com/LiUGraphQL/woo.sh/pull/98
34
* New feature: Individual edges can now by queried by ID
45
* New feature: Edge (annotations) can now be updated. See: https://github.com/LiUGraphQL/woo.sh/issues/35
56
* New feature: Edges can now be deleted. See: https://github.com/LiUGraphQL/woo.sh/issues/36

graphql-server/drivers/arangodb/driver.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,12 @@ function getObjectOrInterfaceFields(type) {
309309
* @param targetType
310310
* @param annotations
311311
* @param info
312-
* @param resVar
312+
* @param resVar = null (optional)
313+
* @param validateSource = true (optional)
314+
* @param validateTarget = true (optional)
313315
* @returns {null|Promise<any>}
314316
*/
315-
function createEdge(isRoot, ctxt, varOrSourceID, sourceType, sourceField, varOrTargetID, targetType, annotations, info, resVar = null) {
317+
function createEdge(isRoot, ctxt, varOrSourceID, sourceType, sourceField, varOrTargetID, targetType, annotations, info, resVar = null, validateSource = true, validateTarget = true) {
316318
// init transaction (if not already defined)
317319
initTransaction(ctxt);
318320

@@ -342,7 +344,7 @@ function createEdge(isRoot, ctxt, varOrSourceID, sourceType, sourceField, varOrT
342344
doc['_creationDate'] = new Date().valueOf();
343345

344346
// validate edge
345-
validateEdge(ctxt, sourceVar, sourceType, sourceField, targetVar, targetType, info);
347+
validateEdge(ctxt, sourceVar, sourceType, sourceField, targetVar, targetType, info, validateSource, validateTarget);
346348

347349
let docVar = addParameterVar(ctxt, createParamVar(ctxt), doc);
348350
ctxt.trans.code.push(`let ${resVar} = db._query(aql\`INSERT MERGE(${asAQLVar(docVar)}, {'_from': ${asAQLVar(sourceVar)}._id, '_to': ${asAQLVar(targetVar)}._id}) IN ${asAQLVar(collectionVar)} RETURN NEW\`).next();`);
@@ -406,7 +408,7 @@ function create(isRoot, ctxt, data, returnType, info, resVar = null) {
406408
throw new ApolloError(`${value['connect']} is not an instance of a type implementing the interface ${targetType}`);
407409
}
408410
}
409-
createEdge(false, ctxt, resVar, returnType, fieldName, value['connect'], typeToConnect, annotations, info);
411+
createEdge(false, ctxt, resVar, returnType, fieldName, value['connect'], typeToConnect, annotations, info, null, false, true);
410412
} else {
411413
// reference to target
412414
let targetVar = createVar(ctxt);
@@ -420,12 +422,12 @@ function create(isRoot, ctxt, data, returnType, info, resVar = null) {
420422
if (value[possibleField]) {
421423
typeToCreate = possibleType;
422424
create(false, ctxt, value[possibleField], typeToCreate, info, targetVar);
423-
createEdge(false, ctxt, resVar, returnType, fieldName, targetVar, typeToCreate, annotations, info);
425+
createEdge(false, ctxt, resVar, returnType, fieldName, targetVar, typeToCreate, annotations, info, null, false, false);
424426
}
425427
}
426428
} else {
427429
create(false, ctxt, value['create'], targetType, info, targetVar);
428-
createEdge(false, ctxt, resVar, returnType, fieldName, targetVar, targetType, annotations, info);
430+
createEdge(false, ctxt, resVar, returnType, fieldName, targetVar, targetType, annotations, info, null, false, false);
429431
}
430432
}
431433
}
@@ -577,7 +579,7 @@ function update(isRoot, ctxt, id, data, returnType, info, resVar = null) {
577579
throw new ApolloError(`${value['connect']} is not an instance of a type implementing the interface ${targetType}`);
578580
}
579581
}
580-
createEdge(false, ctxt, resVar, returnType, fieldName, value['connect'], typeToConnect, annotations, info);
582+
createEdge(false, ctxt, resVar, returnType, fieldName, value['connect'], typeToConnect, annotations, info, null, false, true);
581583
} else {
582584
// reference to target
583585
let targetVar = createVar(ctxt);
@@ -591,12 +593,12 @@ function update(isRoot, ctxt, id, data, returnType, info, resVar = null) {
591593
if (value[possibleField]) {
592594
typeToCreate = possibleType;
593595
create(false, ctxt, value[possibleField], typeToCreate, info, targetVar);
594-
createEdge(false, ctxt, resVar, returnType, fieldName, targetVar, typeToCreate, annotations, info);
596+
createEdge(false, ctxt, resVar, returnType, fieldName, targetVar, typeToCreate, annotations, info, null, false, false);
595597
}
596598
}
597599
} else {
598600
create(false, ctxt, value['create'], targetType, info, targetVar);
599-
createEdge(false, ctxt, resVar, returnType, fieldName, targetVar, targetType, annotations, info);
601+
createEdge(false, ctxt, resVar, returnType, fieldName, targetVar, targetType, annotations, info, null, false, false);
600602
}
601603
}
602604
}
@@ -1160,16 +1162,22 @@ function getResultPromise(ctxt, key) {
11601162
* @param varOrTargetID
11611163
* @param targetType
11621164
* @param info
1165+
* @param validateSource = true (optional)
1166+
* @param validateTarget = true (optional)
11631167
*/
1164-
function validateEdge(ctxt, sourceVar, sourceType, sourceField, targetVar, targetType, info) {
1168+
function validateEdge(ctxt, sourceVar, sourceType, sourceField, targetVar, targetType, info, validateSource = true, validateTarget = true) {
11651169
if (disableEdgeValidation) {
11661170
console.log('Edge validation disabled');
11671171
return;
11681172
}
1169-
ctxt.trans.code.push('/* source exists? */');
1170-
exists(ctxt, sourceVar, sourceType, info.schema);
1171-
ctxt.trans.code.push('/* target exists? */');
1172-
exists(ctxt, targetVar, targetType, info.schema);
1173+
if (validateSource) {
1174+
ctxt.trans.code.push('/* source exists? */');
1175+
exists(ctxt, sourceVar, sourceType, info.schema);
1176+
}
1177+
if (validateTarget) {
1178+
ctxt.trans.code.push('/* target exists? */');
1179+
exists(ctxt, targetVar, targetType, info.schema);
1180+
}
11731181

11741182
// if field is not list type, verify that it is not already populated
11751183
let fieldType = info.schema.getType(sourceType).getFields()[sourceField].type;

0 commit comments

Comments
 (0)