Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connecting to multiple nodes produces too many results #247

Closed
darrellwarde opened this issue Jun 11, 2021 · 2 comments · Fixed by #341
Closed

Connecting to multiple nodes produces too many results #247

darrellwarde opened this issue Jun 11, 2021 · 2 comments · Fixed by #341
Labels
bug Something isn't working confirmed Confirmed bug

Comments

@darrellwarde
Copy link
Contributor

darrellwarde commented Jun 11, 2021

Describe the bug
When connecting using a where clause which produces more than one node to connect to, the update Mutation returns an array entry for each node connected to, when it should only return the root node.

The data itself is in the correct shape, just the return value is off.

Type definitions

type Movie {
  title: String!
  owners: [User!]! @relationship(type: "OWNS", direction: IN)
}

type User {
  name: String!
  movies: [Movie!]! @relationship(type: "OWNS", direction: OUT)
}

To Reproduce
Steps to reproduce the behavior:

  1. With an empty database, create some test data:
mutation {
  createUsers(input: [{ name: "darrell" }]) {
    users {
      name
    }
  }
}

mutation {
  createMovies(
    input: [
      { title: "Batman Begins" }
      { title: "The Dark Knight" }
      { title: "The Dark Knight Rises" }
    ]
  ) {
    movies {
      title
    }
  }
}
  1. Execute the following Mutation:
mutation {
  updateUsers(
    where: { name: "darrell" }
    connect: { movies: [{ where: { title_CONTAINS: "The Dark Knight" } }] }
  ) {
    users {
      name
      movies {
        title
      }
    }
  }
}
  1. You will see the following incorrect result:
{
  "data": {
    "updateUsers": {
      "users": [
        {
          "name": "darrell",
          "movies": [
            {
              "title": "The Dark Knight"
            }
          ]
        },
        {
          "name": "darrell",
          "movies": [
            {
              "title": "The Dark Knight"
            },
            {
              "title": "The Dark Knight Rises"
            }
          ]
        }
      ]
    }
  }
}

Expected behavior
The result to be:

{
  "data": {
    "updateUsers": {
      "users": [
        {
          "name": "darrell",
          "movies": [
            {
              "title": "The Dark Knight"
            },
            {
              "title": "The Dark Knight Rises"
            }
          ]
        }
      ]
    }
  }
}

System (please complete the following information):

@darrellwarde darrellwarde added inbox bug Something isn't working labels Jun 11, 2021
@darrellwarde
Copy link
Contributor Author

Initial investigation finds that the current Cypher is:

MATCH (this:User)
WHERE this.name = $this_name
WITH this
OPTIONAL MATCH (this_connect_movies0:Movie)
WHERE this_connect_movies0.title CONTAINS $this_connect_movies0_title_CONTAINS
FOREACH(_ IN CASE this_connect_movies0 WHEN NULL THEN [] ELSE [1] END | 
MERGE (this)-[:OWNS]->(this_connect_movies0)
)
RETURN this { .name, movies: [ (this)-[:OWNS]->(this_movies:Movie)   | this_movies { .title } ] } AS this

@danstarns has suggested that refactoring to the following could fix this issue:

MATCH (this:User)
WHERE this.name = $this_name
CALL {
    WITH this
    OPTIONAL MATCH (this_connect_movies0:Movie)
    WHERE this_connect_movies0.title CONTAINS $this_connect_movies0_title_CONTAINS
    FOREACH(_ IN CASE this_connect_movies0 WHEN NULL THEN [] ELSE [1] END | 
        MERGE (this)-[:OWNS]->(this_connect_movies0)
    )
    RETURN count(*)
}
RETURN this { .name, movies: [ (this)-[:OWNS]->(this_movies:Movie)   | this_movies { .title } ] } AS this

@jadafi
Copy link

jadafi commented Jun 23, 2021

Similar as #235 ?

@darrellwarde darrellwarde added the confirmed Confirmed bug label Jul 1, 2021
@darrellwarde darrellwarde linked a pull request Jul 21, 2021 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working confirmed Confirmed bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants