The closed graph is constrained by its graph type, which imposes a strict framework that governs data insertion: each node or edge belongs to exactly one pre-defined node type or edge type.
In a closed graph,
While the graph type can be altered after a graph is created, its defined data model ensures consistent structure, guaranteeing high data integrity and consistency.
Syntax<node type> ::= "NODE" [ "TYPE" ] <node type name> "(" [ <additional labels> ] [ <property types> ] ")" <edge type> ::= "EDGE" [ "TYPE" ] <edge type name> <source node type reference> "-[" [ <additional labels> ] [ <property types> ] "]->" <destination node type reference> <additional labels> ::= < ":" | "IS" > <label name> [ { "&", <label name> } ... ] <property types> ::= "{" <property type> [ { "," <property type> } ... ] "}" <property type> ::= <property name> <property value type>
Each node or edge type has a unique type name, and is associated with a set of labels and property types. Each property type is defined with a property name and a property value type.
The node or edge type name automatically becomes a label. The label set of a node or edge type is the union of its type name and additional labels (optional).
When no additional labels are specified, the type name is the only label:
GQL-- label set: [User] NODE User ({name STRING})
Add additional labels:
GQL-- label set: [User, Employee] NODE User (:Employee {name STRING})
GQL-- label set: [User, Employee, Manager] NODE User (:Employee&Manager {name STRING})
Edge types can specify source and destination node types, where <source/destination node type reference> is () or (<labels>). The <labels> is matched against the label sets of node types.
GQL-- The FOLLOWS edge connects any two nodes EDGE FOLLOWS ()-[{createdOn LOCAL DATETIME}]->() -- The JOINS edge connects source nodes whose label set is [User,Employee] to destination nodes whose label set is [Company] EDGE JOINS (:User&Employee)-[{title STRING}]->(:Company)
Create a closed graph g2 with inline graph type specification, which defines:
User with properties name (STRING type) and age (UINT32 type)Club with a property name (STRING type)FOLLOWS with a property createdOn (LOCAL DATETIME type)JOINS with no propertiesGQLCREATE GRAPH g2 { NODE User ({name STRING, age UINT32}), NODE Club ({name STRING}), EDGE FOLLOWS ()-[{createdOn TIMESTAMP}]->(), EDGE JOINS ()-[]->() }
Create a closed graph g3 based on the defined graph type named gType (How to manage graph types), you have three equivalent GQL options to specify the graph type to be used:
GQLCREATE GRAPH g3 gType
or
GQLCREATE GRAPH g3 :: gType
or
GQLCREATE GRAPH g3 TYPED gType
The graph g3 is created with all the types and properties defined within the graph type gType.
The graph type defines structural rules for graphs by outlining the allowed node and edge types. You can define and store graph types in the database, making them reusable when creating new graphs.
Show graph types defined in the database:
GQLSHOW GRAPH TYPES
Each graph type provides the following essential metadata:
| Field | Description |
|---|---|
name | The unique name assigned to the graph type. |
node_type_count | Number of node types. |
edge_type_count | Number of edge types. |
node_types | Comma-separated list of node type names. |
edge_types | Comma-separated list of edge type names. |
definition | The type definitions. |
bound_graphs | Graphs that use this graph type. |
comment | The comment of the graph type. |
created_at | Creation time. |
updated_at | Last update time. |
Create a graph type gType:
GQLCREATE GRAPH TYPE gType { NODE User ({name STRING, age UINT32}), NODE Club ({name STRING}), EDGE FOLLOWS ()-[{createdOn TIMESTAMP}]->(), EDGE JOINS ()-[]->() }
Drop the graph type gType:
GQLDROP GRAPH TYPE gType
The IF EXISTS clause is used to prevent errors when attempting to delete a graph type that does not exist. It allows the statement to be safely executed.
GQLDROP GRAPH TYPE IF EXISTS gType
This deletes the graph type gType only if a graph type with that name does exist. If gType does not exist, the statement is ignored without throwing an error.
Show node types defined in the current graph:
GQLSHOW NODE TYPES
Show edge types defined in the current graph:
GQLSHOW EDGE TYPES
Each type provides the following metadata:
Field | Description |
|---|---|
type | NODE or EDGE. |
name | The name of the type. |
properties | The associated property definitions. |
Show labels in the current graph:
GQLSHOW LABELS
Show node labels in the current graph:
GQLSHOW NODE LABELS
Show edge labels in the current graph:
GQLSHOW EDGE LABELS
Each label provides the following essential metadata:
Field | Description |
|---|---|
label | The name of the label. |
type | The type of the label, NODE or EDGE. |
You can alter the graph type of an existing closed graph.
Add a node type Book in the graph g2:
GQLALTER GRAPH g2 ADD NODE Book ({name STRING, author STRING})
Add an edge type PURCHASED in the graph g2:
GQLALTER GRAPH g2 ADD EDGE PURCHASED (:User)-[{createdOn TIMESTAMP}]->(:Book)
Add a property gender to the node type User in the current graph:
GQLALTER NODE User ADD PROPERTY gender STRING
Add a property memberNo to the edge type JOINS within the current graph:
GQLALTER EDGE JOINS ADD PROPERTY memberNo INT32
To rename the node type User to People in the current graph:
GQLALTER NODE User RENAME TO People
To rename the edge type FOLLOWS to LINKS in the current graph:
GQLALTER EDGE FOLLOWS RENAME TO LINKS
To rename the property name to title for the node type Book in the current graph:
GQLALTER NODE Book PROPERTY name RENAME TO title
To rename the property memberNo to memberNumber for the edge type JOINS in the current graph:
GQLALTER EDGE JOINS PROPERTY memberNo RENAME TO memberNumber
A node or edge type can only be dropped when no nodes or edges of that type exist.
Drop the node type User from the graph g2:
GQLALTER GRAPH g2 DROP NODE User
Drop the edge type FOLLOWS from the graph g2:
GQLALTER GRAPH g2 DROP EDGE FOLLOWS
When a property is dropped, all related data - including the property values, associated indexes, and cached values - is permanently removed.
Drop the property name from the node type User in the current graph:
GQLALTER NODE User DROP PROPERTY name
Drop the property createdOn from the edge type FOLLOWS in the current graph:
GQLALTER EDGE FOLLOWS DROP PROPERTY createdOn