Graql Insert Queries

The page documents use of the Graql insert query, which will insert a specified variable pattern describing data. To follow along, or experiment further, with the examples given below, please load the basic-genealogy.gql file, which can be found in the examples directory of the Grakn installation zip, or on Github.


If a match is provided, the query will insert the given variable patterns for every result of the query. The pattern describes properties to set on a particular concept and can optionally be bound to a variable or an ID.

In the example below, we insert additional (fictional) information for a person entity who we have matched through identifier Mary Guthrie.

match $p has identifier "Mary Guthrie"; insert $p has middlename "Mathilda"; $p has birth-date 1902-01-01; $p has death-date 1952-01-01; $p has age 50;

qb.match(var("p").has("identifier", "Mary Guthrie"))
    .insert(var("p").has("middlename", "Mathilda"),
        var("p").has("birth-date", LocalDateTime.of(1902, 1, 1, 0, 0, 0)),
        var("p").has("death-date", LocalDateTime.of(1952, 1, 1, 0, 0, 0)),
        var("p").has("age", 50)



Set the type of the inserted concept.

insert has identifier "Titus Groan" isa person;

qb.insert(var().has("identifier", "Titus Groan").isa("person")).execute();


It is not possible to insert a concept with the given id, as this is the job of the system. However, if you attempt to insert by id, you will retrieve a concept if one with that id already exists. The created or retrieved concept can then be modified with further properties.


insert id "1376496" isa person;




Set the value of the concept.

insert $x == "Ash" isa surname;
insert $x isa surname "Ash";



Add an attribute of the given type to the concept.

insert isa person, has identifier "Fuchsia Groan" has gender "female";

qb.insert(var().isa("person").has("identifier", "Fuchsia Groan").has("gender", "female")).execute();

You can also specify a variable to represent the relationship connecting the thing and the attribute:

insert isa person has identifier "Fuchsia Groan" via $r;

qb.insert(var().isa("person").has(Label.of("identifier"), var().val("Fuchsia Groan"), var("r"))).execute();


Make the concept a relationship that relates the given role players, playing the given roles. (With apologies to ‘Gormenghast’ fans, who will be aware that Titus and Fuchsia are siblings and thus cannot marry).

match $p1 has identifier "Titus Groan"; $p2 has identifier "Fuchsia Groan"; insert (spouse: $p1, spouse: $p2) isa marriage;

  var("p1").has("name", "Titus Groan"),
  var("p2").has("name", "Fuchsia Groan")
    .rel("spouse", "p1")
    .rel("spouse", "p2")

Tags: graql