2
votes

I am using GRAND stack - neo4j database and apollo server. Also using the augmentedSchema of neo4j-graphql-js. This adds more types and mutations to the GraphQL schema.

I have a type Option with the following typedef:

type Option {
  id: ID
  name: String
  position: Int
  values: [String]
}

Using the CreateOption mutation generated by neo4j-graphql-js, I am able to create an Option. The problem I am facing is that I can create multiple options with the same ID.

mutation {
  opt1: CreateOption(id: 1, name: "Test") {
    id
  }
  opt2: CreateOption(id: 1, name: "Test 2") {
    id
  }
}

The result of the above mutation is

{
  "data": {
    "opt1": {
      "id": "1"
    },
    "opt2": {
      "id": "1"
    }
  }
}

Why is neo4j allowing two nodes with the same ID? How can I ensure nodes with unique IDs in neo4j?

1

1 Answers

1
votes

In Neo4j concept of Id is a bit "different" from common understanding, and id is nothing more than an offset of object within the store file. Neo4j has all the control about id itself.

Even more, if you will remove object with Id 40, corresponding bytes in the store file will become vacant. And in the future you could get another object with id 40.

There is one more trick about id of objects: Neo4j id property called <id> instead of id. And property id from your example is the same as name. For example, I've created two objects on my local Neo4j:

╒═════════════════════════════════╕
│{"name":"test1","id":1,"<id>":47}│
├─────────────────────────────────┤
│{"name":"test2","id":1,"<id>":64}│
└─────────────────────────────────┘

To make id unique you have to create a constraint manually. Here is a CYPHER example:

CREATE CONSTRAINT ON (option:Option) ASSERT option.id IS UNIQUE