Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/unique directive #547

Merged
merged 28 commits into from
Oct 28, 2021

Conversation

darrellwarde
Copy link
Contributor

@darrellwarde darrellwarde commented Oct 22, 2021

Description

Adds an assertConstrains function to the Neo4jGraphQL class which can be used to assert unique node property constraints indicated by a new @unique directive exist.

Overview of features:

  • By default, @id and @unique directives indicate that a unique node property constraint should exist for the property mapped to the GraphQL field.
  • @id has a new boolean option, unique, which is true by default, which let's users disable this new behaviour.
  • Compatible with both the label argument of the @node directive, and the @alias directive.
  • Option to create missing constraints on running of assertConstraints.

UPDATE:

Instead of using the desired SHOW CONSTRAINTS (https://neo4j.com/docs/cypher-manual/4.3/constraints/examples/#administration-constraints-list-constraint), this has to use CALL db.constraints (https://neo4j.com/docs/cypher-manual/4.1/administration/constraints/#administration-constraints-list-constraint) to maintain compatibility with 4.1.

Introduced a parsing function which outputs in the same object format as SHOW CONSTRAINTS, and left the code for SHOW CONSTRAINTS present under TODO blocks so that swapping out is simple when 4.1 reaches end-of-life.

Issue

#206

Checklist

The following requirements should have been met (depending on the changes in the branch):

  • Documentation has been updated
  • TCK tests have been updated
  • Integration tests have been updated
  • Example applications have been updated
  • New files have copyright header
  • CLA (https://neo4j.com/developer/cla/) has been signed

@darrellwarde darrellwarde linked an issue Oct 22, 2021 that may be closed by this pull request
@github-actions github-actions bot added documentation Improvements or additions to documentation feature graphql labels Oct 22, 2021
@neo4j-team-graphql
Copy link
Collaborator

neo4j-team-graphql commented Oct 22, 2021

Thanks for the documentation updates.

The preview documentation has now been torn down - reopening this PR will republish it.

Copy link
Member

@angrykoala angrykoala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. A few minor comments

@darrellwarde
Copy link
Contributor Author

Thanks for the comments @angrykoala - looks like I need to rewrite the whole thing anyway because my current solution isn't 4.1 compatible!!! 👿

Copy link
Contributor

@danstarns danstarns left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that when I create a node through the GraphQL API the Neo4j Error is propagated to the users. What do you think and would there be a possibility to throw something more generic, just to ensure that we don't leak any info about the backend?

Example Error:

{
  "errors": [
    {
      "message": "Node(7108) already exists with label `Movie` and property `title` = 'Some Title'",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "createMovies"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "code": "Neo.ClientError.Schema.ConstraintValidationFailed",
          "name": "Neo4jError",
          "stacktrace": [
            "Neo4jError: Node(7108) already exists with label `Movie` and property `title` = 'Some Title'",
            ": ",
            "    at captureStacktrace (C:\\code\\neo4j\\graphql\\node_modules\\neo4j-driver-core\\lib\\result.js:239:17)",
            "    at new Result (C:\\code\\neo4j\\graphql\\node_modules\\neo4j-driver-core\\lib\\result.js:59:23)",
            "    at newCompletedResult (C:\\code\\neo4j\\graphql\\node_modules\\neo4j-driver-core\\lib\\transaction.js:372:12)",
            "    at Object.run (C:\\code\\neo4j\\graphql\\node_modules\\neo4j-driver-core\\lib\\transaction.js:226:20)",
            "    at Transaction.run (C:\\code\\neo4j\\graphql\\node_modules\\neo4j-driver-core\\lib\\transaction.js:98:34)",
            "    at C:\\code\\neo4j\\graphql\\packages\\graphql\\src\\utils\\execute.ts:94:35",
            "    at TransactionExecutor._safeExecuteTransactionWork (C:\\code\\neo4j\\graphql\\node_modules\\neo4j-driver-core\\lib\\internal\\transaction-executor.js:92:26)",
            "    at TransactionExecutor._executeTransactionInsidePromise (C:\\code\\neo4j\\graphql\\node_modules\\neo4j-driver-core\\lib\\internal\\transaction-executor.js:83:34)",
            "    at C:\\code\\neo4j\\graphql\\node_modules\\neo4j-driver-core\\lib\\internal\\transaction-executor.js:40:19",
            "    at new Promise (<anonymous>)"
          ]
        }
      }
    }
  ],
  "data": null
}

Copy link
Member

@angrykoala angrykoala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking nice, just minor style/legibility proposals

packages/graphql/src/schema/get-obj-field-meta.ts Outdated Show resolved Hide resolved
packages/graphql/src/utils/execute.ts Show resolved Hide resolved
@darrellwarde darrellwarde merged commit f341645 into neo4j:master Oct 28, 2021
@darrellwarde darrellwarde deleted the feature/unique-directive branch May 9, 2022 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add @Unique Directive Back
4 participants