Skip to content

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

Open
@congard

Description

@congard

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions