GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • GitHubGitHub
  • DiscordDiscord
  • Setup and sample prompts
  • Question? Give us feedback →
  • Edit this page

This view

  • Original page
  • New address

GeezerVison is showing memgraph.com — Mock Python query module API

For AI agents: an index of this documentation is at https://memgraph.com/docs/llms.txt. Every page is also available as Markdown by adding .md to its URL. To search the docs, use the Memgraph docs MCP server at https://mcp-docs.memgraph.com (no API key).

  • Home
  • Getting started
    • AI coding assistants
      • Memgraph plugins
      • Docs MCP server
      • llms.txt
    • Installation
      • Docker
      • Docker Compose
      • Memgraph Cloud
      • Kubernetes
      • Linux
      • WSL
      • Direct download links
      • Source build
      • Packaging
    • CLI
  • AI applications
    • Knowledge graph creation
      • Structured2Graph
      • Unstructured2Graph
    • GraphRAG
      • Atomic GraphRAG
        • Text2Cypher
        • Local graph search
        • Query-focused summarization
      • Agentic GraphRAG
    • AI memory
      • Get started
      • Recall and memory
      • The graph
      • Privacy
      • Supported agents
      • Custom agents
      • Shared spaces
      • Configuration
    • Agentic AI
    • Graph machine learning
    • AI Toolkit and integrations
      • Memgraph MCP server
      • LangChain
      • LlamaIndex
      • LightRAG
      • Cognee
      • Mem0
  • Client libraries
    • C#
    • Go
    • GraphQL
    • Java
    • JavaScript
    • Node.js
    • PHP
    • Python
    • Rust
  • Fundamentals
    • Constraints
    • Data types
    • Data durability
    • Indexes
    • Storage access
    • Storage memory usage
    • Telemetry
    • Transactions
    • Triggers
  • Data modeling
    • Graph data model
      • LPG vs. RDF
    • Modeling guides
      • Model a knowledge graph
      • Model a graph from CSV file
    • Best practices
  • Data migration
    • Best practices
    • CSV
    • PARQUET
    • JSON
    • CYPHERL
    • Data streams
      • Transformation modules
        • Transformation modules C API
        • Transformations Python API
      • Manage streams with queries
      • Graph stream processing with Kafka
      • Kafka Connect
    • Migrate from Neo4j
      • Using CSV files
      • Using a single Cypher query
    • Migrate from RDBMS using CSV files
    • Migrate from RDBMS using MAGE modules
    • Migrate Apache Iceberg tables from data lake using Dremio
    • Migrate to Memgraph using Apache Spark
    • Migrate from Memgraph Platform to Memgraph MAGE
    • Export data
  • Querying
    • Best practices
    • Differences in Cypher implementations
    • Create graph objects
    • Read and modify data
    • Clauses
      • ALTER
      • CALL
      • CASE
      • CREATE
      • DELETE
      • DROP
      • EXPLAIN
      • FOREACH
      • LOAD CSV
      • LOAD PARQUET
      • MATCH
      • MERGE
      • OPTIONAL MATCH
      • PROFILE
      • REMOVE
      • RETURN
      • SET
      • UNION
      • UNWIND
      • USING PARALLEL EXECUTION
      • WHERE
      • WITH
    • Functions
    • Expressions
    • Subquery expressions
    • Schema
    • Text search
    • Vector search
    • Time to live
    • Query plan
    • Parallel execution
    • Exploring datasets
      • Analyzing TED talks
      • Backpacking through Europe
      • Exploring the European road network
      • Football transfers
      • GoT deaths
      • Graphing the premier league
      • Marvel universe
      • Movie recommendation
    • Sample datasets
  • Advanced algorithms
    • Available algorithms
      • algo
      • betweenness_centrality_online
      • betweenness_centrality
      • biconnected_components
      • bipartite_matching
      • bridges
      • collections
      • csv_utils
      • community_detection_online
      • community_detection
      • convert
      • convert_c
      • do
      • create
      • cross_database
      • cugraph
      • cycles
      • date
      • degree_centrality
      • distance_calculator
      • elasticsearch_synchronization
      • embeddings
      • export_util
      • gnn
      • gnn_link_prediction
      • gnn_node_classification
      • graph_analyzer
      • graph_coloring
      • graph_util
      • igraphalg
      • import_util
      • json_util
      • knn
      • katz_centrality_online
      • katz_centrality
      • kmeans_clustering
      • label
      • leiden_community_detection
      • llm
      • llm_util
      • map
      • math
      • max_flow
      • merge
      • meta_util
      • meta
      • migrate
      • mgps
      • neighbors
      • node_similarity
      • node
      • node2vec_online
      • node2vec
      • nodes
      • nxalg
      • pagerank_online
      • pagerank
      • path
      • periodic
      • refactor
      • search
      • set_cover
      • set_property
      • temporal
      • text
      • temporal_graph_networks
      • tsp
      • union_find
      • util_module
      • uuid_generator
      • vrp
      • weakly_connected_components
      • xml_module
    • Deep path traversal algorithms
    • Install MAGE
    • Run algorithms
    • Utilize NetworkX
  • Custom query modules
    • C
      • C API
      • C example
    • C++
      • C++ API
      • C++ example
    • Python
      • Python API
      • Mock Python API
      • Python example
      • Implement custom query module in Python
      • Understanding music with modules
    • Rust
      • Rust API
      • Rust example
    • Manage query modules
    • Contributing
  • Database management
    • Authentication and authorization
      • Users
      • Role-based access control
      • Migrating to v3.7 LBAC
      • Multiple roles per user and multi-tenant roles
      • Auth system integrations
      • Impersonate user
      • User profiles
      • Query privileges reference
    • Backup and restore
    • Clustering
      • Replication
        • Under the hood
        • Set up replication cluster with Docker
        • Set up replication cluster with K8s
        • Best practices
        • Reference commands
        • System replication
      • High availability
        • Under the hood
        • Querying the cluster in HA
        • Coordinator authentication
        • Set up HA cluster with Docker
        • Set up HA cluster with Docker Compose
        • Set up HA cluster with K8s
        • Best practices
        • Bulk import in analytical mode
        • Reference commands
        • Reference architectures
        • Migrating to v3.9 HA
      • FAQ
    • Configuration
    • Debugging
    • Enabling Memgraph Enterprise
    • Logs
    • Monitoring
    • Multi-tenancy
    • Tenant profiles
    • Query metadata
    • Server-side descriptions
    • Server-side parameters
    • Server stats
    • SSL encryption
    • System configuration
    • Upgrades
      • Specific versions
      • Deprecated features
  • Deployment
    • Best practices
    • Workloads
      • Memgraph in cybersecurity
      • Memgraph in GraphRAG use cases
      • Memgraph in high-throughput workloads
      • Memgraph in mission critical workloads
      • Memgraph in fraud detection
      • Memgraph in supply chain
    • Environments
      • Docker
      • Linux
      • AWS
      • GCP
      • Azure
    • Benchmarking
  • Memgraph Lab
    • Getting started
      • Installation and deployment
      • Connection types
      • Data migration
    • Querying
    • Features
      • Collections
      • CSV file import
      • Custom configuration
      • Data modeling
      • GraphChat
      • Graph schema
      • Graph Style Script (GSS)
        • Main building blocks
        • Built-in elements
        • Directive properties
        • Tutorial
      • Layout
      • Logs
      • Multi-tenancy
      • Monitoring
      • Run history
      • Sharing features
      • Single sign-on
      • Query modules
      • Streams
    • Configuration
  • Help center
    • FAQ
    • Errors
      • Auth
      • Connection
      • Durability
      • High availability
      • Memory
      • Modules
      • Ports
      • Python modules
      • Replication
      • Snapshots
      • Socket
      • SSL
      • Transactions
      • Unknown
  • Release notes

  • Memgraph Zero
    • MemGQL
      • Quick Start
      • Use Cases
        • Federated GQL
        • Public-Private Data
        • Distributed Compute
        • Enterprise Context
        • Agentic Access
      • Features
      • Connect
        • to Clickhouse
        • to DuckDB
        • to Microsoft Fabric
        • to SAP HANA
        • to Iceberg
        • to Memgraph
        • to MongoDB
        • to Neo4j
        • to Oracle
        • to PostgreSQL
        • to MySQL
        • to SQL Server
        • to Pinot
        • to Amazon Redshift
        • to Snowflake
      • Schema File
      • Multiple Graphs
      • Docker Compose
      • Reference
      • Licensing
      • Changelog
      • Telemetry

  • What's coming soon to Memgraph?
Custom query modulesPythonMock Python API

Mock Python query module API

The mock Python query module API enables you to develop and test query modules for Memgraph without having to run a Memgraph instance by simulating its behavior. As the mock API is compatible with the Python API, you can add modules developed with it to Memgraph as-is, without modifying the code.

It is implemented in mgp_mock.py, which contains definitions of all classes and functions provided for developing query module procedures and functions. The source file is located in the Memgraph installation directory, inside /usr/include/memgraph.

API reference

Because the mock API’s classes and functions are compatible with the corresponding Python API classes and functions, the Python API reference applies, with the following exceptions:

  • Query procedure returns (Record class) are printable.
  • The mock API doesn’t throw errors having to do with Memgraph-internal behavior (UnableToAllocateError, InsufficientBufferError, OutOfRangeError, KeyAlreadyExistsError, SerializationError and AuthorizationError).
  • The mock API doesn’t contain two Python API methods dealing with Memgraph-internal behavior (must_abort and check_must_abort). These methods are used to check whether Memgraph has notified the query module to abort its execution.
  • The constructors of the ProcCtx and FuncCtx classes take a NetworkX MultiDiGraph because that’s the data structure the mock API uses for internal graph representations.
  • Transformation modules are currently not implemented.

Graph representation

The mock Python API uses a graph representation based on the NetworkX MultiDiGraph, which is a directed graph that supports parallel edges (relationships) and custom node/relationship attributes.

All elements of a Memgraph graph are supported by the mock API, with the following rules about representing node labels and relationship types:

  • Node labels are stored in the node attribute named "labels" as a ":"-separated string, e.g. the node (n:Actor:Director) has {"labels": "Actor:Director"}.
  • Edge types are strings stored in "type".

Using the mock API

Importing

Before importing the mock API, you need to make it visible to the query module, e.g. by adding the path of mgp_mock.py to PYTHONPATH or copying mgp_mock.py to the directory containing the module.

Running

The following code block contains an example query procedure and a runner for query procedures:

import mgp_mock as mgp
import networkx as nx
 
@mgp.read_proc
def example_procedure(context: mgp.ProcCtx) -> mgp.Record(status=str):
    return mgp.Record(status="Hello, world!")
 
graph = nx.MultiDiGraph() # Empty graph
context = mgp.ProcCtx(graph) # Create a context instance
 
result = example_procedure(context) # Run the procedure
print(result) # Hello, world!

Running the module with Memgraph

As the mock Python API is compatible with the Python query module API, adding a module developed with the mock API to Memgraph is a simple task.

  1. Replace the mgp_mock import with import mgp
    • This includes refactoring the usages of mgp_mock (or alias) to mgp.
  2. Load the query module.
Python APIPython example
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.