21.8. Nodes

Create node

Figure 21.12. Final Graph

Example request

  • POST http://localhost:7474/db/data/node
  • Accept: application/json; charset=UTF-8

Example response

  • 201: Created
  • Content-Type: application/json; charset=UTF-8
  • Location: http://localhost:7474/db/data/node/111
{
  "extensions" : { },
  "metadata" : {
    "id" : 111,
    "labels" : [ ]
  },
  "paged_traverse" : "http://localhost:7474/db/data/node/111/paged/traverse/{returnType}{?pageSize,leaseTime}",
  "outgoing_relationships" : "http://localhost:7474/db/data/node/111/relationships/out",
  "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/111/relationships/out/{-list|&|types}",
  "create_relationship" : "http://localhost:7474/db/data/node/111/relationships",
  "labels" : "http://localhost:7474/db/data/node/111/labels",
  "traverse" : "http://localhost:7474/db/data/node/111/traverse/{returnType}",
  "all_relationships" : "http://localhost:7474/db/data/node/111/relationships/all",
  "all_typed_relationships" : "http://localhost:7474/db/data/node/111/relationships/all/{-list|&|types}",
  "property" : "http://localhost:7474/db/data/node/111/properties/{key}",
  "self" : "http://localhost:7474/db/data/node/111",
  "incoming_relationships" : "http://localhost:7474/db/data/node/111/relationships/in",
  "properties" : "http://localhost:7474/db/data/node/111/properties",
  "incoming_typed_relationships" : "http://localhost:7474/db/data/node/111/relationships/in/{-list|&|types}",
  "data" : { }
}

Create node with properties

Figure 21.13. Final Graph

Example request

  • POST http://localhost:7474/db/data/node
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
{
  "foo" : "bar"
}

Example response

  • 201: Created
  • Content-Length: 1237
  • Content-Type: application/json; charset=UTF-8
  • Location: http://localhost:7474/db/data/node/107
{
  "extensions" : { },
  "metadata" : {
    "id" : 107,
    "labels" : [ ]
  },
  "paged_traverse" : "http://localhost:7474/db/data/node/107/paged/traverse/{returnType}{?pageSize,leaseTime}",
  "outgoing_relationships" : "http://localhost:7474/db/data/node/107/relationships/out",
  "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/107/relationships/out/{-list|&|types}",
  "create_relationship" : "http://localhost:7474/db/data/node/107/relationships",
  "labels" : "http://localhost:7474/db/data/node/107/labels",
  "traverse" : "http://localhost:7474/db/data/node/107/traverse/{returnType}",
  "all_relationships" : "http://localhost:7474/db/data/node/107/relationships/all",
  "all_typed_relationships" : "http://localhost:7474/db/data/node/107/relationships/all/{-list|&|types}",
  "property" : "http://localhost:7474/db/data/node/107/properties/{key}",
  "self" : "http://localhost:7474/db/data/node/107",
  "incoming_relationships" : "http://localhost:7474/db/data/node/107/relationships/in",
  "properties" : "http://localhost:7474/db/data/node/107/properties",
  "incoming_typed_relationships" : "http://localhost:7474/db/data/node/107/relationships/in/{-list|&|types}",
  "data" : {
    "foo" : "bar"
  }
}

Get node

Note that the response contains URI/templates for the available operations for getting properties and relationships.

Figure 21.14. Final Graph

Example request

  • GET http://localhost:7474/db/data/node/182
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
{
  "extensions" : { },
  "metadata" : {
    "id" : 182,
    "labels" : [ ]
  },
  "paged_traverse" : "http://localhost:7474/db/data/node/182/paged/traverse/{returnType}{?pageSize,leaseTime}",
  "outgoing_relationships" : "http://localhost:7474/db/data/node/182/relationships/out",
  "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/182/relationships/out/{-list|&|types}",
  "create_relationship" : "http://localhost:7474/db/data/node/182/relationships",
  "labels" : "http://localhost:7474/db/data/node/182/labels",
  "traverse" : "http://localhost:7474/db/data/node/182/traverse/{returnType}",
  "all_relationships" : "http://localhost:7474/db/data/node/182/relationships/all",
  "all_typed_relationships" : "http://localhost:7474/db/data/node/182/relationships/all/{-list|&|types}",
  "property" : "http://localhost:7474/db/data/node/182/properties/{key}",
  "self" : "http://localhost:7474/db/data/node/182",
  "incoming_relationships" : "http://localhost:7474/db/data/node/182/relationships/in",
  "properties" : "http://localhost:7474/db/data/node/182/properties",
  "incoming_typed_relationships" : "http://localhost:7474/db/data/node/182/relationships/in/{-list|&|types}",
  "data" : { }
}

Get non-existent node

Figure 21.15. Final Graph

Example request

  • GET http://localhost:7474/db/data/node/18600000
  • Accept: application/json; charset=UTF-8

Example response

  • 404: Not Found
  • Content-Type: application/json; charset=UTF-8
{
  "message": "Cannot find node with id [18600000] in database.",
  "exception": "NodeNotFoundException",
  "fullname": "org.neo4j.server.rest.web.NodeNotFoundException",
  "stackTrace": [
    "org.neo4j.server.rest.web.DatabaseActions.node(DatabaseActions.java:169)",
    "org.neo4j.server.rest.web.DatabaseActions.getNode(DatabaseActions.java:214)",
    "org.neo4j.server.rest.web.RestfulGraphDatabase.getNode(RestfulGraphDatabase.java:279)",
    "java.lang.reflect.Method.invoke(Method.java:497)",
    "org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:139)",
    "org.neo4j.server.rest.web.CollectUserAgentFilter.doFilter(CollectUserAgentFilter.java:69)",
    "java.lang.Thread.run(Thread.java:745)"
  ],
  "cause": {
    "exception": "NotFoundException",
    "cause": {
      "exception": "EntityNotFoundException",
      "fullname": "org.neo4j.kernel.api.exceptions.EntityNotFoundException",
      "stackTrace": [
        "org.neo4j.kernel.impl.factory.GraphDatabaseFacade.getNodeById(GraphDatabaseFacade.java:254)",
        "org.neo4j.server.rest.web.DatabaseActions.node(DatabaseActions.java:165)",
        "org.neo4j.server.rest.web.DatabaseActions.getNode(DatabaseActions.java:214)",
        "org.neo4j.server.rest.web.RestfulGraphDatabase.getNode(RestfulGraphDatabase.java:279)",
        "java.lang.reflect.Method.invoke(Method.java:497)",
        "org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:139)",
        "org.neo4j.server.rest.web.CollectUserAgentFilter.doFilter(CollectUserAgentFilter.java:69)",
        "java.lang.Thread.run(Thread.java:745)"
      ],
      "message": "Unable to load NODE with id 18600000.",
      "errors": [
        {
          "code": "Neo.ClientError.Statement.EntityNotFound",
          "message": "Unable to load NODE with id 18600000."
        }
      ]
    },
    "fullname": "org.neo4j.graphdb.NotFoundException",
    "stackTrace": [
      "org.neo4j.kernel.impl.factory.GraphDatabaseFacade.getNodeById(GraphDatabaseFacade.java:254)",
      "org.neo4j.server.rest.web.DatabaseActions.node(DatabaseActions.java:165)",
      "org.neo4j.server.rest.web.DatabaseActions.getNode(DatabaseActions.java:214)",
      "org.neo4j.server.rest.web.RestfulGraphDatabase.getNode(RestfulGraphDatabase.java:279)",
      "java.lang.reflect.Method.invoke(Method.java:497)",
      "org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:139)",
      "org.neo4j.server.rest.web.CollectUserAgentFilter.doFilter(CollectUserAgentFilter.java:69)",
      "java.lang.Thread.run(Thread.java:745)"
    ],
    "message": "Node 18600000 not found",
    "errors": [
      {
        "code": "Neo.ClientError.Statement.EntityNotFound",
        "message": "Node 18600000 not found"
      }
    ]
  },
  "errors": [
    {
      "code": "Neo.ClientError.Statement.EntityNotFound",
      "message": "Cannot find node with id [18600000] in database."
    }
  ]
}

Delete node

Figure 21.16. Starting Graph

Figure 21.17. Final Graph

Example request

  • DELETE http://localhost:7474/db/data/node/108
  • Accept: application/json; charset=UTF-8

Example response

  • 204: No Content

Nodes with relationships cannot be deleted

The relationships on a node has to be deleted before the node can be deleted.

[Tip]Tip

You can use DETACH DELETE in Cypher to delete nodes and their relationships in one go.

Figure 21.18. Starting Graph

Example request

  • DELETE http://localhost:7474/db/data/node/115
  • Accept: application/json; charset=UTF-8

Example response

  • 409: Conflict
  • Content-Type: application/json; charset=UTF-8
{
  "message": "The node with id 115 cannot be deleted. Check that the node is orphaned before deletion.",
  "exception": "ConstraintViolationException",
  "fullname": "org.neo4j.graphdb.ConstraintViolationException",
  "stackTrace": [
    "org.neo4j.server.rest.web.DatabaseActions.deleteNode(DatabaseActions.java:224)",
    "org.neo4j.server.rest.web.RestfulGraphDatabase.deleteNode(RestfulGraphDatabase.java:293)",
    "java.lang.reflect.Method.invoke(Method.java:497)",
    "org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:139)",
    "org.neo4j.server.rest.web.CollectUserAgentFilter.doFilter(CollectUserAgentFilter.java:69)",
    "java.lang.Thread.run(Thread.java:745)"
  ],
  "errors": [
    {
      "code": "Neo.ClientError.Schema.ConstraintViolation",
      "message": "The node with id 115 cannot be deleted. Check that the node is orphaned before deletion."
    }
  ]
}