Skip to content
This repository was archived by the owner on Sep 3, 2021. It is now read-only.

Commit 4b4737b

Browse files
authoredDec 4, 2018
Merge pull request #152 from michaeldgraham/master
Temporal property relation removal test
2 parents a42573f + e759128 commit 4b4737b

File tree

3 files changed

+386
-194
lines changed

3 files changed

+386
-194
lines changed
 

‎src/translate.js

+52-26
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,9 @@ const relationTypeMutationPayloadField = ({
177177
tailParams,
178178
rootVariableNames
179179
}) => {
180+
const safeVariableName = safeVar(variableName);
180181
return {
181-
initial: `${initial}${fieldName}: ${variableName} {${
182+
initial: `${initial}${fieldName}: ${safeVariableName} {${
182183
subSelection[0]
183184
}}${skipLimit} ${commaIfTail}`,
184185
...tailParams,
@@ -802,7 +803,10 @@ const relationshipCreate = ({
802803
let query = `
803804
MATCH (${fromVariable}:${fromLabel} ${
804805
fromTemporalClauses && fromTemporalClauses.length > 0
806+
// uses either a WHERE clause for managed type primary keys (temporal, etc.)
805807
? `) WHERE ${fromTemporalClauses.join(' AND ')} `
808+
// or a an internal matching clause for normal, scalar property primary keys
809+
// NOTE this will need to change if we at some point allow for multi field node selection
806810
: `{${fromParam}: $from.${fromParam}})`
807811
}
808812
MATCH (${toVariable}:${toLabel} ${
@@ -838,15 +842,14 @@ const relationshipDelete = ({
838842
'Missing required MutationMeta directive on add relationship directive'
839843
);
840844
}
845+
841846
try {
842847
relationshipNameArg = mutationMeta.arguments.find(x => {
843848
return x.name.value === 'relationship';
844849
});
845-
846850
fromTypeArg = mutationMeta.arguments.find(x => {
847851
return x.name.value === 'from';
848852
});
849-
850853
toTypeArg = mutationMeta.arguments.find(x => {
851854
return x.name.value === 'to';
852855
});
@@ -855,29 +858,51 @@ const relationshipDelete = ({
855858
'Missing required argument in MutationMeta directive (relationship, from, or to)'
856859
);
857860
}
858-
//TODO: need to handle one-to-one and one-to-many
859-
const args = resolveInfo.schema.getMutationType().getFields()[
860-
resolveInfo.fieldName
861-
].astNode.arguments;
862861

862+
//TODO: need to handle one-to-one and one-to-many
863+
const args = getMutationArguments(resolveInfo);
863864
const typeMap = resolveInfo.schema.getTypeMap();
864865

865866
const fromType = fromTypeArg.value.value;
866867
const fromVar = `${lowFirstLetter(fromType)}_from`;
867868
const fromInputArg = args.find(e => e.name.value === 'from').type;
868-
const fromInputAst =
869-
typeMap[getNamedType(fromInputArg).type.name.value].astNode;
870-
const fromParam = fromInputAst.fields[0].name.value;
869+
const fromInputAst = typeMap[getNamedType(fromInputArg).type.name.value].astNode;
870+
const fromFields = fromInputAst.fields;
871+
const fromParam = fromFields[0].name.value;
872+
const fromTemporalArgs = getTemporalArguments(fromFields);
871873

872874
const toType = toTypeArg.value.value;
873875
const toVar = `${lowFirstLetter(toType)}_to`;
874876
const toInputArg = args.find(e => e.name.value === 'to').type;
875-
const toInputAst =
876-
typeMap[getNamedType(toInputArg).type.name.value].astNode;
877-
const toParam = toInputAst.fields[0].name.value;
877+
const toInputAst = typeMap[getNamedType(toInputArg).type.name.value].astNode;
878+
const toFields = toInputAst.fields;
879+
const toParam = toFields[0].name.value;
880+
const toTemporalArgs = getTemporalArguments(toFields);
878881

879882
const relationshipName = relationshipNameArg.value.value;
880883

884+
const schemaTypeName = safeVar(schemaType);
885+
const fromVariable = safeVar(fromVar);
886+
const fromLabel = safeLabel(fromType);
887+
const toVariable = safeVar(toVar);
888+
const toLabel = safeLabel(toType);
889+
const relationshipVariable = safeVar(fromVar + toVar);
890+
const relationshipLabel = safeLabel(relationshipName);
891+
const fromRootVariable = safeVar('_' + fromVar);
892+
const toRootVariable = safeVar('_' + toVar);
893+
const fromTemporalClauses = temporalPredicateClauses(
894+
params.from,
895+
fromVariable,
896+
fromTemporalArgs,
897+
"from"
898+
);
899+
const toTemporalClauses = temporalPredicateClauses(
900+
params.to,
901+
toVariable,
902+
toTemporalArgs,
903+
"to"
904+
);
905+
// TODO remove use of _ prefixes in root variableNames and variableName
881906
const [subQuery, subParams] = buildCypherSelection({
882907
initial: '',
883908
selections,
@@ -892,19 +917,20 @@ const relationshipDelete = ({
892917
variableName: schemaType.name === fromType ? `_${toVar}` : `_${fromVar}`
893918
});
894919
params = { ...params, ...subParams };
895-
896-
const schemaTypeName = safeVar(schemaType);
897-
const fromVariable = safeVar(fromVar);
898-
const fromLabel = safeLabel(fromType);
899-
const toVariable = safeVar(toVar);
900-
const toLabel = safeLabel(toType);
901-
const relationshipVariable = safeVar(fromVar + toVar);
902-
const relationshipLabel = safeLabel(relationshipName);
903-
const fromRootVariable = safeVar('_' + fromVar);
904-
const toRootVariable = safeVar('_' + toVar);
905-
const query = `
906-
MATCH (${fromVariable}:${fromLabel} {${fromParam}: $from.${fromParam}})
907-
MATCH (${toVariable}:${toLabel} {${toParam}: $to.${toParam}})
920+
// TODO create builder functions for selection clauses below for both relation mutations
921+
let query = `
922+
MATCH (${fromVariable}:${fromLabel} ${
923+
fromTemporalClauses && fromTemporalClauses.length > 0
924+
// uses either a WHERE clause for managed type primary keys (temporal, etc.)
925+
? `) WHERE ${fromTemporalClauses.join(' AND ')} `
926+
// or a an internal matching clause for normal, scalar property primary keys
927+
: `{${fromParam}: $from.${fromParam}})`
928+
}
929+
MATCH (${toVariable}:${toLabel} ${
930+
toTemporalClauses && toTemporalClauses.length > 0
931+
? `) WHERE ${toTemporalClauses.join(' AND ')} `
932+
: `{${toParam}: $to.${toParam}})`
933+
}
908934
OPTIONAL MATCH (${fromVariable})-[${relationshipVariable}:${relationshipLabel}]->(${toVariable})
909935
DELETE ${relationshipVariable}
910936
WITH COUNT(*) AS scope, ${fromVariable} AS ${fromRootVariable}, ${toVariable} AS ${toRootVariable}

0 commit comments

Comments
 (0)