cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Implementing Unique Constraint in GraphQL Schema

hey, i am trying to use neo4j along with graphql and using neo4j-graphql-js module wanted to know that how can i implement my schema to have some unique filed like username or email?
and avoid any duplicate entry through mutations

3 REPLIES 3

William_Lyon
Graph Fellow

If you add a field with the type ID! and don’t include it in the Create mutations then a unique UUID will be generated for that field.

We currently don’t expose database constraints via GraphQL, but you could also create a database constraint in Neo4j for email/username to enforce the uniqueness.

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

Adding this feature to neo4j-graphql.js is being tracked in this issue: https://github.com/neo4j-graphql/neo4j-graphql-js/issues/137

Please add any additional info to the issue so we can be sure to support your use case when we add it

Hi William,

So you're saying that if I want Neo4j to generate a GUID/UUID (i.e. uuid-4 compliant), I use the default ID type in my GraphQL Schema, because this is implemented as a UUID in Neo4j (or by the neo4j-graphql-js library)?

This is very convenient if so, I would just like clarification to be sure.

Yes. If you add an ID! field to your schema and don't specify a value for it in the Create mutation then a UUID will be generated and saved in the database. We use apoc.create.uuid in the generated Cypher to accomplish this. This is why the Create mutation will use an ID nullable field, even though you define it as a non-nullable ID! in the type definitions.

I'll be sure this is documented better in the docs, with an example.