neo4j-graphql / neo4j-graphql-java

Neo4j Labs Project: Pure JVM translation for GraphQL queries and mutations to Neo4j's Cypher
Apache License 2.0
105 stars 49 forks source link

Invalid mutation query for one-to-many relations #292

Open congard opened 1 year ago

congard commented 1 year ago

Describe the bug As in the title - invalid mutation query for one-to-many relations: auto-generated add and delete mutations generate invalid Cypher query.

Test Case

package org.example.graphql

import org.neo4j.graphql.SchemaBuilder
import org.neo4j.graphql.Translator

fun main() {
    val sdl = /* GraphQL scheme */
    val schema = SchemaBuilder.buildSchema(sdl)
    val translator = Translator(schema)

    val mutation = /* GraphQL mutation */

    println(translator.translate(mutation, params = HashMap<String, Any>().also {
        it["id"] = 0
        it["belongsTo"] = listOf(1, 2, 3)
    }))
}

GraphQL schema

type Item {
    _id: ID!
    name: String!
    belongsTo: [Category!]! @relation(name: "BelongsTo")
}

type Category {
    _id: ID!
    name: String!
}

GraphQL request

mutation ItemMutateBelongsTo(
    $id: ID!,
    $belongsTo: [ID!]!
) {
    addItemBelongsTo(
        _id: $id,
        belongsTo: $belongsTo
    ) {
        _id
    }
}

Expected cypher query

MATCH (from:Item)
WHERE id(from) = toInteger($id)
MATCH (to:Category)
WHERE id(to) in $belongsTo // <---- here
MERGE (from)-[:BelongsTo]->(to)
WITH DISTINCT from AS addItemBelongsTo
RETURN addItemBelongsTo {
    _id: id(addItemBelongsTo)
} AS addItemBelongsTo

Generated cypher query

MATCH (from:Item)
WHERE id(from) = toInteger($id)
MATCH (to:Category)
WHERE id(to) = toInteger($belongsTo) // <---- invalid, $belongsTo is a list, not just a single integer
MERGE (from)-[:BelongsTo]->(to)
WITH DISTINCT from AS addItemBelongsTo
RETURN addItemBelongsTo {
    _id: id(addItemBelongsTo)
} AS addItemBelongsTo

Additional context neo4j-graphql-java version: 1.7.0 (also tested 1.6.0 and 1.5.0 - the same issue)