Skip to content

Commit 2556f52

Browse files
committed
Simplified how to convert annotation map to string for input usage, and renamed the used function to better reprensent what it actually does: convertToInputAppendString
1 parent 1fab48b commit 2556f52

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

graphql-server/drivers/arangodb/driver.js

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -241,23 +241,17 @@ function getObjectOrInterfaceFields(type) {
241241
}
242242

243243
/**
244-
* Get the annotations of an edge and convert them to string to be used in insert operations
245-
* Would have prefered using createEdge, but we seem unable to pass a properly accesible "context variable"._id as parameter
246-
* @param annotations
244+
* Transforms the doc (field_name, field_value) collection to JSON stringify, strip leading and ending '{' and '}' and insert leading ', '
245+
* This allows docs to be appanded to already existing input parameters.
246+
* @param doc (should already have passed through getScalarsAndEnums)
247247
* @returns String
248248
*/
249-
function getAnnotations(annotations) {
250-
// Remeber that annotations should already have passed through getScalarsAndEnums
251-
// So injections on field should not be possible
252-
// The values need to be checked separatly.
253-
let ret = '';
254-
for (let field in annotations) {
255-
if (typeof annotations[field] === 'string') {
256-
// Treat strings as strings
257-
ret += `, ${field}: "${annotations[field]}"`;
258-
}
259-
else
260-
ret += `, ${field}: ${annotations[field]}`;
249+
function convertToInputAppendString(doc) {
250+
ret = ''
251+
if (doc != null && doc.size > 0) {
252+
ret = JSON.stringify(doc);
253+
ret[0] = ',';
254+
ret = ret - slice(0, -1);
261255
}
262256
return ret;
263257
}
@@ -384,7 +378,7 @@ async function create(isRoot, ctxt, data, returnType, info) {
384378
let typeToConnect = value['connect'].split('/')[0];
385379
// add edge
386380
ctxt.trans.code.push(`if(db._collection('${typeToConnect}').exists('${value['connect']}')){`);
387-
ctxt.trans.code.push(` db._query(aql\`INSERT {_from: ${from}._id, _to: "${value['connect']}" } IN ${edgeCollection} RETURN NEW\`);`);
381+
ctxt.trans.code.push(` db._query(aql\`INSERT {_from: ${from}._id, _to: "${value['connect']}" ${convertToInputAppendString(annotations)}} IN ${edgeCollection} RETURN NEW\`);`);
388382
ctxt.trans.code.push(`} else { `);
389383
ctxt.trans.code.push(` throw "${value['connect']} does not exist in ${typeToConnect}";`);
390384
ctxt.trans.code.push(`}`);
@@ -398,22 +392,22 @@ async function create(isRoot, ctxt, data, returnType, info) {
398392
let typeToCreate = key.replace(/^create(.+)$/, '$1');
399393
let to = asAQLVar(getVar(ctxt)); // reference to the object to be added
400394
await create(false, ctxt, value[key], info.schema.getType(typeToCreate), info);
401-
ctxt.trans.code.push(`db._query(aql\`INSERT {_from: ${from}._id, _to: ${to}._id ${getAnnotations(annotations)}} IN ${edgeCollection} RETURN NEW\`);`);
395+
ctxt.trans.code.push(`db._query(aql\`INSERT {_from: ${from}._id, _to: ${to}._id ${convertToInputAppendString(annotations)}} IN ${edgeCollection} RETURN NEW\`);`);
402396
}
403397
} else { // type
404398
if (value['connect']) {
405399
validateType(ctxt, value['connect'], innerFieldType, info.schema);
406400
let typeToConnect = value['connect'].split('/')[0];
407401
// add edge
408402
ctxt.trans.code.push(`if(db._collection('${typeToConnect}').exists('${value['connect']}')){`);
409-
ctxt.trans.code.push(` db._query(aql\`INSERT {_from: ${from}._id, _to: "${value['connect']}" } IN ${edgeCollection} RETURN NEW\`);`);
403+
ctxt.trans.code.push(` db._query(aql\`INSERT {_from: ${from}._id, _to: "${value['connect']}" ${convertToInputAppendString(annotations)}} IN ${edgeCollection} RETURN NEW\`);`);
410404
ctxt.trans.code.push(`} else { `);
411405
ctxt.trans.code.push(` throw "${value['connect']} does not exist in ${typeToConnect}";`);
412406
ctxt.trans.code.push(`}`);
413407
} else { // create
414408
let to = asAQLVar(getVar(ctxt)); // reference to the object to be added
415409
await create(false, ctxt, value['create'], innerFieldType, info);
416-
ctxt.trans.code.push(`db._query(aql\`INSERT {_from: ${from}._id, _to: ${to}._id ${getAnnotations(annotations)}} IN ${edgeCollection} RETURN NEW\`);`);
410+
ctxt.trans.code.push(`db._query(aql\`INSERT {_from: ${from}._id, _to: ${to}._id ${convertToInputAppendString(annotations)}} IN ${edgeCollection} RETURN NEW\`);`);
417411
}
418412
}
419413
}
@@ -671,11 +665,11 @@ async function update(isRoot, ctxt, id, data, returnType, info) {
671665
// add edge
672666
if (!disableEdgeValidation) { // check the database
673667
ctxt.trans.code.push(`if(db._collection('${typeToConnect}').exists('${value['connect']}')){`);
674-
ctxt.trans.code.push(`db._query(aql\`INSERT {_from: "${id}", _to: "${value['connect']}" ${getAnnotations(annotations)}} IN ${edgeCollection} RETURN NEW\`);`);
668+
ctxt.trans.code.push(`db._query(aql\`INSERT {_from: "${id}", _to: "${value['connect']}" ${convertToInputAppendString(annotations)}} IN ${edgeCollection} RETURN NEW\`);`);
675669
ctxt.trans.code.push(`} else { throw "${value['connect']} does not exist in ${typeToConnect}"; }`);
676670
} else {
677671
console.warn(`Adding connection to ${value['connect']} in ${edge} without validating ID`);
678-
ctxt.trans.code.push(`db._query(aql\`INSERT {_from: "${id}", _to: "${value['connect']}" ${getAnnotations(annotations)}} IN ${edgeCollection} RETURN NEW\`);`);
672+
ctxt.trans.code.push(`db._query(aql\`INSERT {_from: "${id}", _to: "${value['connect']}" ${convertToInputAppendString(annotations)}} IN ${edgeCollection} RETURN NEW\`);`);
679673
}
680674
} else {
681675
// create
@@ -686,7 +680,7 @@ async function update(isRoot, ctxt, id, data, returnType, info) {
686680
}
687681
let to = asAQLVar(getVar(ctxt)); // reference to the object to be added
688682
await create(false, ctxt, value[key], info.schema.getType(typeToCreate), info);
689-
ctxt.trans.code.push(`db._query(aql\`INSERT {_from: "${id}", _to: ${to}._id } IN ${edgeCollection} RETURN NEW\`);`);
683+
ctxt.trans.code.push(`db._query(aql\`INSERT {_from: "${id}", _to: ${to}._id ${convertToInputAppendString(annotations)}} IN ${edgeCollection} RETURN NEW\`);`);
690684
}
691685
} else {
692686
// type field
@@ -699,17 +693,17 @@ async function update(isRoot, ctxt, id, data, returnType, info) {
699693
// add edge
700694
if (!disableEdgeValidation) { // check the database
701695
ctxt.trans.code.push(`if(db._collection('${typeToConnect}').exists('${value['connect']}')){`);
702-
ctxt.trans.code.push(`db._query(aql\`INSERT {_from: "${id}", _to: "${value['connect']}" ${getAnnotations(annotations)}} IN ${edgeCollection} RETURN NEW\`);`);
696+
ctxt.trans.code.push(`db._query(aql\`INSERT {_from: "${id}", _to: "${value['connect']}" ${convertToInputAppendString(annotations)}} IN ${edgeCollection} RETURN NEW\`);`);
703697
ctxt.trans.code.push(`} else { throw "${value['connect']} does not exist in ${typeToConnect}"; }`);
704698
} else {
705699
console.warn(`Adding connection to ${value['connect']} in ${edge} without validating ID`);
706-
ctxt.trans.code.push(`db._query(aql\`INSERT {_from: "${id}", _to: "${value['connect']}" ${getAnnotations(annotations)}} IN ${edgeCollection} RETURN NEW\`);`);
700+
ctxt.trans.code.push(`db._query(aql\`INSERT {_from: "${id}", _to: "${value['connect']}" ${convertToInputAppendString(annotations)}} IN ${edgeCollection} RETURN NEW\`);`);
707701
}
708702
} else {
709703
// create
710704
let to = asAQLVar(getVar(ctxt)); // reference to the object to be added
711705
await create(false, ctxt, value['create'], nestedReturnType, info);
712-
ctxt.trans.code.push(`db._query(aql\`INSERT {_from: "${id}", _to: ${to}._id ${getAnnotations(annotations)}} IN ${edgeCollection} RETURN NEW\`);`);
706+
ctxt.trans.code.push(`db._query(aql\`INSERT {_from: "${id}", _to: ${to}._id ${convertToInputAppendString(annotations)}} IN ${edgeCollection} RETURN NEW\`);`);
713707
}
714708
}
715709
}
@@ -777,11 +771,11 @@ function formatFixVariable(_type, v) {
777771
if (Array.isArray(v)) {
778772
let newV = []
779773
for (date of v)
780-
newV.push(aql`DATE_TIMESTAMP(${date})`);
774+
newV.push(aql`DATE_TIMESTAMP("${date}")`);
781775
return newV;
782776
}
783777
else
784-
return aql`DATE_TIMESTAMP(${v})`;
778+
return aql`DATE_TIMESTAMP("${v}")`;
785779
else
786780
return v;
787781
}

0 commit comments

Comments
 (0)