Search code examples
neo4jgraphqlcypherunique-constraintgrandstack

How to add a unique constraint on type field with Graphql and Neo4j?


I'm making a GRANDStack application so I'm using neo4j and graphql,

I have this type in my schema.graphql file :

type User {
  userId: ID!
  username: String! @unique
  mail: String! @unique
  password: String! @private
}

But I'm still able to create multiple accounts with the same

I know this is possible when I look at neo4j documentation here : https://neo4j.com/docs/graphql-manual/current/directives/#_unique

but the only I found for now is to manually do something like this in my database :

CREATE CONSTRAINT ON (u:User) ASSERT u.mail IS UNIQUE;

But it's not a good idea, I want this to be automatic

I think my package.json is also up to date :

{
  "name": "grand-stack-starter-api",
  "version": "0.0.1",
  "description": "API app for GRANDstack",
  "main": "src/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start:dev": "./node_modules/.bin/nodemon --watch src --ext js,graphql --exec babel-node  src/index.js",
    "build": "babel src --out-dir build && shx cp .env build 2>/dev/null || : && shx cp src/schema.graphql build",
    "now-build": "babel src --out-dir build && shx cp src/schema.graphql build",
    "start": "npm run build && node build/index.js",
    "seedDb": "./node_modules/.bin/babel-node src/seed/seed-db.js"
  },
  "author": "William Lyon",
  "license": "MIT",
  "dependencies": {
    "@apollo/client": "^3.2.5",
    "@neo4j/graphql": "^2.4.0",
    "apollo-server": "^3.5.0",
    "apollo-server-lambda": "^2.19.0",
    "csv-parse": "^4.10.1",
    "dotenv": "^7.0.0",
    "graphql": "^16.0.1",
    "neo4j-driver": "^4.4.0",
    "node-fetch": "^2.6.0",
    "react": "^16.13.1"
  },
  "devDependencies": {
    "@babel/cli": "^7.8.4",
    "@babel/core": "^7.9.0",
    "@babel/node": "^7.8.7",
    "@babel/plugin-proposal-class-properties": "^7.8.3",
    "@babel/plugin-transform-runtime": "^7.9.0",
    "@babel/preset-env": "^7.9.0",
    "@babel/preset-react": "^7.9.4",
    "@babel/preset-typescript": "^7.9.0",
    "@babel/runtime-corejs3": "^7.9.2",
    "babel-plugin-auto-import": "^1.0.5",
    "babel-plugin-module-resolver": "^4.0.0",
    "cross-env": "^7.0.2",
    "nodemon": "^1.19.1",
    "shx": "^0.3.2"
  }
}

Solution

  • So, turned out the documentation is not updated

    To fix this I first had to follow this : https://neo4j.com/docs/graphql-manual/current/type-definitions/constraints/#type-definitions-constraints-unique

    the solution would be to add this which is wrong

    await neoSchema.assertConstraints({ options: { create: true }});
    

    I talked with someone working at neo4j in their discord and the right function one :

    await neoSchema.assertIndexesAndConstraints({ options: { create: true }});
    

    If like me you can't do await since your code is not in a function you can do something like this with .then :

     const neoSchema = new Neo4jGraphQL({
       ...
        })
     neoSchema
          .assertIndexesAndConstraints({ options: { create: true } })
          .then(() => {
            const server = new ApolloServer({
             ...
    
        app.listen({ host, port, path }, () => {
          console.log(`GraphQL server ready at http://${host}:${port}${path}`)
        })
      })