Avoiding Duplicates & Ensuring Uniqueness with Mutations

I have a schema as follows:

type Person {
	id: ID!
	name: String!
	interests: [Interest!] @relation(name: "INTEREST", direction: "OUT")
}

type Interest {
	id: ID!
	interest: String!
	people_with_interest: [Person!] @relation(name: "INTEREST", direction: "IN")
}

When I call the mutation:

AddPersonInterests(from:$person, to:$interest) {
    to {
      id
    }
    from {
      id
    }
  }

multiple times with the same Person/Interest it creates duplicate edges.

How can I avoid duplicate edges being created? Additionally, is there a way to delete all interests for a given person in this example? Thank you!

Hello, do you think you could provide some code to show how you're attempting to add the interests? It's a little hard to get an idea of what error your getting without that. It sounds like you're getting multiple instances of a person having the same interests, which can easily happen because you're using the default created mutation, since you're unable to avoid duplicate relationships via any of the schema constraints that Neo4j allows you to place on your instance. Something you might look into is creating a custom mutation something like:

addInterest(Person: ID!, Interest: ID!): Person
  @cypher(
    statement: 
    "MATCH (p:Person {id: $Person})
     MATCH (i:Interest {id: $Interest})
     WHERE NOT (p)-[:INTEREST]->(i)
     SET (p)-[:INTEREST]->(i)
     RETURN p
  )

This would allow you to set the relationship only if it doesn't already exist. As for removing all interests from a person:

removeAllInterests(Person: ID!): Person
@cypher(
   statement:
   "MATCH (p:person {id: $Person})-[r:INTEREST]->()
    DELETE r
    RETURN p
)

Would get that done. Your other alternative using the custom generated mutations would be to use:

Promise.all(yourvalues.map

type of approach to remove all of your values with the provided custom generated mutations. I hope this helps and welcome to the community!