@@ -309,10 +309,12 @@ function getObjectOrInterfaceFields(type) {
309
309
* @param targetType
310
310
* @param annotations
311
311
* @param info
312
- * @param resVar
312
+ * @param resVar = null (optional)
313
+ * @param validateSource = true (optional)
314
+ * @param validateTarget = true (optional)
313
315
* @returns {null|Promise<any> }
314
316
*/
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 ) {
316
318
// init transaction (if not already defined)
317
319
initTransaction ( ctxt ) ;
318
320
@@ -342,7 +344,7 @@ function createEdge(isRoot, ctxt, varOrSourceID, sourceType, sourceField, varOrT
342
344
doc [ '_creationDate' ] = new Date ( ) . valueOf ( ) ;
343
345
344
346
// validate edge
345
- validateEdge ( ctxt , sourceVar , sourceType , sourceField , targetVar , targetType , info ) ;
347
+ validateEdge ( ctxt , sourceVar , sourceType , sourceField , targetVar , targetType , info , validateSource , validateTarget ) ;
346
348
347
349
let docVar = addParameterVar ( ctxt , createParamVar ( ctxt ) , doc ) ;
348
350
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) {
406
408
throw new ApolloError ( `${ value [ 'connect' ] } is not an instance of a type implementing the interface ${ targetType } ` ) ;
407
409
}
408
410
}
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 ) ;
410
412
} else {
411
413
// reference to target
412
414
let targetVar = createVar ( ctxt ) ;
@@ -420,12 +422,12 @@ function create(isRoot, ctxt, data, returnType, info, resVar = null) {
420
422
if ( value [ possibleField ] ) {
421
423
typeToCreate = possibleType ;
422
424
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 ) ;
424
426
}
425
427
}
426
428
} else {
427
429
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 ) ;
429
431
}
430
432
}
431
433
}
@@ -577,7 +579,7 @@ function update(isRoot, ctxt, id, data, returnType, info, resVar = null) {
577
579
throw new ApolloError ( `${ value [ 'connect' ] } is not an instance of a type implementing the interface ${ targetType } ` ) ;
578
580
}
579
581
}
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 ) ;
581
583
} else {
582
584
// reference to target
583
585
let targetVar = createVar ( ctxt ) ;
@@ -591,12 +593,12 @@ function update(isRoot, ctxt, id, data, returnType, info, resVar = null) {
591
593
if ( value [ possibleField ] ) {
592
594
typeToCreate = possibleType ;
593
595
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 ) ;
595
597
}
596
598
}
597
599
} else {
598
600
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 ) ;
600
602
}
601
603
}
602
604
}
@@ -1160,16 +1162,22 @@ function getResultPromise(ctxt, key) {
1160
1162
* @param varOrTargetID
1161
1163
* @param targetType
1162
1164
* @param info
1165
+ * @param validateSource = true (optional)
1166
+ * @param validateTarget = true (optional)
1163
1167
*/
1164
- function validateEdge ( ctxt , sourceVar , sourceType , sourceField , targetVar , targetType , info ) {
1168
+ function validateEdge ( ctxt , sourceVar , sourceType , sourceField , targetVar , targetType , info , validateSource = true , validateTarget = true ) {
1165
1169
if ( disableEdgeValidation ) {
1166
1170
console . log ( 'Edge validation disabled' ) ;
1167
1171
return ;
1168
1172
}
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
+ }
1173
1181
1174
1182
// if field is not list type, verify that it is not already populated
1175
1183
let fieldType = info . schema . getType ( sourceType ) . getFields ( ) [ sourceField ] . type ;
0 commit comments