Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit

* bump to 2.5

* try 2.2

* change ubuntu to 20.04

* revert to MG 2.3.0

* bump to mg 2.5.0 and update qm signatures

Git stats


Failed to load latest commit information.
Latest commit message
Commit time


Code style: black

GQLAlchemy is a fully open-source Python library and Object Graph Mapper (OGM) - a link between graph database objects and Python objects.

An Object Graph Mapper or OGM provides a developer-friendly workflow that allows for writing object-oriented notation to communicate with graph databases. Instead of writing Cypher queries, you will be able to write object-oriented code, which the OGM will automatically translate into Cypher queries.

GQLAlchemy is built on top of Memgraph's low-level Python client pymgclient (PyPI / Documentation / GitHub).


Before you install gqlalchemy, make sure that you have cmake installed by running:

cmake --version

You can install cmake by following the official instructions.

To install gqlalchemy, simply run the following command:

pip install gqlalchemy

If you are using Conda for Python environment management, you can install GQLAlchemy through pip.

Build & Test

The project uses Poetry to build the GQLAlchemy Python library. To build and run tests, execute the following command: poetry install

Before starting the tests, make sure you have an active Memgraph instance running. Execute the following command: poetry run pytest .

GQLAlchemy capabilities

🗺️ Object graph mapper

Below you can see an example of how to create User and Language node classes, and a relationship class of type SPEAKS. Along with that, you can see how to create a new node and relationship and how to save them in the database. After that, you can load those nodes and relationship from the database.

from gqlalchemy import Memgraph, Node, Relationship, Field
from typing import Optional

db = Memgraph()

class User(Node, index=True, db=db):
    id: str = Field(index=True, exist=True, unique=True, db=db)

class Language(Node):
    name: str = Field(unique=True, db=db)

class Speaks(Relationship, type="SPEAKS"):

user = User(id="3", username="John").save(db)
language = Language(name="en").save(db)
speaks_rel = Speaks(
    _start_node_id = user._id,
    _end_node_id = language._id

loaded_user = User(id="3").load(db=db)
loaded_speaks = Speaks(
🔨 Query builder
When building a Cypher query, you can use a set of methods that are wrappers around Cypher clauses.

from gqlalchemy import create, match
from gqlalchemy.query_builder import Operator

query_create = create()
        .node(labels="Person", name="Leslie")
        .node(labels="Person", name="Ron")

query_match = match()
        .node(labels="Person", variable="p1")
        .node(labels="Person", variable="p2")
        .where(item="", operator=Operator.EQUAL, literal="Leslie")
        .return_(results=["p1", ("p2", "second")])
🚰 Manage streams

You can create and start Kafka or Pulsar stream using GQLAlchemy.

Kafka stream

from gqlalchemy import MemgraphKafkaStream

stream = MemgraphKafkaStream(name="ratings_stream", topics=["ratings"], transform="movielens.rating", bootstrap_servers="localhost:9093")

Pulsar stream

from gqlalchemy import MemgraphPulsarStream

stream = MemgraphPulsarStream(name="ratings_stream", topics=["ratings"], transform="movielens.rating", service_url="localhost:6650")
🗄️ Import table data from different sources

Import table data to a graph database

You can translate table data from a file to graph data and import it to Memgraph. Currently, we support reading of CSV, Parquet, ORC and IPC/Feather/Arrow file formats via the PyArrow package.

Read all about it in table to graph importer how-to guide.

Make a custom file system importer

If you want to read from a file system not currently supported by GQLAlchemy, or use a file type currently not readable, you can implement your own by extending abstract classes FileSystemHandler and DataLoader, respectively.

Read all about it in custom file system importer how-to guide.

⚙️ Manage Memgraph instances

You can start, stop, connect to and monitor Memgraph instances with GQLAlchemy.

Manage Memgraph Docker instance

from gqlalchemy.instance_runner import (

memgraph_instance = MemgraphInstanceDocker(
    docker_image=DockerImage.MEMGRAPH, docker_image_tag="latest", host="", port=7687
memgraph = memgraph_instance.start_and_connect(restart=False)

memgraph.execute_and_fetch("RETURN 'Memgraph is running' AS result"))[0]["result"]

Manage Memgraph binary instance

from gqlalchemy.instance_runner import MemgraphInstanceBinary

memgraph_instance = MemgraphInstanceBinary(
    host="", port=7698, binary_path="/usr/lib/memgraph/memgraph", user="memgraph"
memgraph = memgraph_instance.start_and_connect(restart=False)

memgraph.execute_and_fetch("RETURN 'Memgraph is running' AS result"))[0]["result"]
🔫 Manage database triggers

Because Memgraph supports database triggers on CREATE, UPDATE and DELETE operations, GQLAlchemy also implements a simple interface for maintaining these triggers.

from gqlalchemy import Memgraph, MemgraphTrigger
from gqlalchemy.models import (

db = Memgraph()

trigger = MemgraphTrigger(
    statement="UNWIND createdVertices AS node SET node.created_at = LocalDateTime()",

triggers = db.get_triggers()
💽 On-disk storage

Since Memgraph is an in-memory graph database, the GQLAlchemy library provides an on-disk storage solution for large properties not used in graph algorithms. This is useful when nodes or relationships have metadata that doesn’t need to be used in any of the graph algorithms that need to be carried out in Memgraph, but can be fetched after. Learn all about it in the on-disk storage how-to guide.

If you want to learn more about OGM, query builder, managing streams, importing data from different source, managing Memgraph instances, managing database triggers and using on-disk storage, check out the GQLAlchemy how-to guides.

Development (how to build)

poetry run flake8 .
poetry run black .
poetry run pytest . -k "not slow"


The GQLAlchemy documentation is available on

The documentation can be generated by executing:

pip3 install python-markdown


Copyright (c) 2016-2022 Memgraph Ltd.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.