@@ -60,10 +60,32 @@ class Translator(val schema: GraphQLSchema) {
60
60
return cypherQueryOrMutation(variable, fieldDefinition, field, cypherDirective, mapProjection, ordering, skipLimit, isQuery)
61
61
62
62
} else {
63
- val where = if (ctx.topLevelWhere) where(variable, fieldDefinition, type, propertyArguments(field)) else Cypher .EMPTY
64
- val properties = if (ctx.topLevelWhere) Cypher .EMPTY else properties(variable, fieldDefinition, propertyArguments(field))
65
- return Cypher (" MATCH ($variable :$label${properties.query} )${where.query} RETURN ${mapProjection.query} AS $variable$ordering$skipLimit " ,
66
- (mapProjection.params + properties.params + where.params))
63
+ if (isQuery) {
64
+ val where = if (ctx.topLevelWhere) where(variable, fieldDefinition, type, propertyArguments(field)) else Cypher .EMPTY
65
+ val properties = if (ctx.topLevelWhere) Cypher .EMPTY else properties(variable, fieldDefinition, propertyArguments(field))
66
+ return Cypher (" MATCH ($variable :$label${properties.query} )${where.query} RETURN ${mapProjection.query} AS $variable$ordering$skipLimit " ,
67
+ (mapProjection.params + properties.params + where.params))
68
+ } else {
69
+ // todo extract method or better object
70
+ val properties = properties(variable, fieldDefinition, propertyArguments(field))
71
+ val idProperty = fieldDefinition.arguments.find { it.type.inner() == Scalars .GraphQLID }
72
+ val returnStatement = " WITH $variable RETURN ${mapProjection.query} AS $variable$ordering$skipLimit " ;
73
+ return when (name) {
74
+ " create" + type.name -> Cypher (" CREATE ($variable :$label${properties.query} )" + returnStatement, (mapProjection.params + properties.params))
75
+ " update" + type.name -> {
76
+ val setProperties = setProperties(variable, fieldDefinition, propertyArguments(field))
77
+ Cypher (" MATCH ($variable :$label {${idProperty!! .name.quote()} :\$ ${paramName(variable, idProperty.name, properties.params[idProperty.name])} }) " + setProperties.query + returnStatement,
78
+ (mapProjection.params + setProperties.params))
79
+ }
80
+ " delete" + type.name -> {
81
+ val paramName = paramName(variable, idProperty!! .name, properties.params[idProperty.name]) // todo currently wrong, needs to be paramName
82
+ Cypher (" MATCH ($variable :$label {${idProperty.name.quote()} :\$ $paramName }) " +
83
+ " WITH $variable as toDelete, ${mapProjection.query} AS $variable $ordering$skipLimit DETACH DELETE toDelete RETURN $variable " ,
84
+ (mapProjection.params + mapOf (paramName to properties.params[paramName])))
85
+ }
86
+ else -> throw IllegalArgumentException (" Unknown Mutation " + name)
87
+ }
88
+ }
67
89
}
68
90
}
69
91
@@ -118,7 +140,12 @@ class Translator(val schema: GraphQLSchema) {
118
140
private fun properties (variable : String , field : GraphQLFieldDefinition , arguments : List <Argument >) : Cypher {
119
141
val all = preparePredicateArguments(field, arguments)
120
142
return Cypher (all.map { (k,p, v) -> " ${p.quote()} :\$ ${paramName(variable, k, v)} " }.joinToString(" , " ," {" ," }" ) ,
121
- all.map { (k,p,v) -> paramName(variable, k, v) to v }.toMap())
143
+ all.map { (k,_,v) -> paramName(variable, k, v) to v }.toMap())
144
+ }
145
+ private fun setProperties (variable : String , field : GraphQLFieldDefinition , arguments : List <Argument >) : Cypher {
146
+ val all = preparePredicateArguments(field, arguments)
147
+ return Cypher (all.map { (k,p, v) -> " ${variable.quote()} .${p.quote()} =\$ ${paramName(variable, k, v)} " }.joinToString(" ," ," SET " ," " ) ,
148
+ all.map { (k,_,v) -> paramName(variable, k, v) to v }.toMap())
122
149
}
123
150
124
151
data class CypherArgument (val name : String , val propertyName : String , val value : Any? )
0 commit comments