graphql icon indicating copy to clipboard operation
graphql copied to clipboard

Reconnect in case of a To-1 relationship

Open AccsoSG opened this issue 1 year ago • 6 comments

Describe the bug I have a simple To-1 relationship where I want to change the relationship to one node to another node. Unfortunately an update does not work. I get the error message "... required exactly once"

Type definitions

type Movie {
  name: String!
  genre: Genre! @relationship(type: "HAS_GENRE", direction: OUT)
}

type Genre {
  name: String! @unique
  movies: [Movie!]! @relationship(type: "HAS_GENRE", direction: IN)
}

To Reproduce Steps to reproduce the behavior:

  1. Run a server with the following code...
  2. Execute the following Mutation... 2.1. Init Genres
mutation CreateGenres {
  createGenres(input: [
    {
      name: "Action"
    },
    {
      name: "Thriller"
    }
  ]) {
    genres {
      name
    }
  }
}

2.2. Init Movie with connection to the Genre "Action"

mutation CreateMovie {
  createMovies(input: {
    name: "TestMovie1",
    genre: {
      connect: {
        where: {
          node: {
            name: "Action"
          }
        }
      }
    }
  }) {
    movies {
      name
      genre {
        name
      }
    }
  }
}
  1. Then run the following Mutation to update the relationship...
mutation UpdateMovieWithUpdateConnect {
  updateMovies(
    where: {
      name: "TestMovie1",
    },
    update: {
      genre: {
	connect: {
          where: {
            node: {
              name: "Thriller"
            }
          }
        }
      }
    }
  ) {
    movies {
      name
      genre {
        name
      }
    }
  }
}
  1. See error
{
  "errors": [
    {
      "message": "Movie.genre required exactly once",
      "locations": [
        {
          "line": 39,
          "column": 3
        }
      ],
      "path": [
        "updateMovies"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR"
      }
    }
  ],
  "data": null
}

Expected behavior As a client, I would like to be able to reconnect an existing relationship to a new node.

Current workaround Perform an explicit disconnect in addition to the connect. As a client, however, this behavior is not intuitive

mutation UpdateMovieWithUpdateConnectAndDisconnect {
  updateMovies(
    where: {
      name: "TestMovie1",
    },
    update: {
      genre: {
	connect: {
          where: {
            node: {
              name: "Thriller"
            }
          }
        },
        disconnect: {
          where: {
            node: {
              NOT: {
                name: "Thriller"
              }
            }
          }
        }
      }
    }
  ) {
    movies {
      name
      genre {
        name
      }
    }
  }
}

System (please complete the following information):

  • Version: 3.18.2

Additional context

  1. I noticed that there is a new parameter "overwrite" in connect. I'm not sure if that helps or has anything to do with it at all. I can't find any documentation on this.
  2. When you want to perform a reconnect to a node that does not exist/ could not be found, there is no error message. The response is successful, but logically no reconnect has taken place. Unfortunately, the client does not notice this.

AccsoSG avatar Apr 27 '23 12:04 AccsoSG