Skip to main content

networkx-graphql

The QL in the nx.Graph


Package PyPI - Version PyPI - Status PyPI - Python Version PyPI - Downloads Libraries.io SourceRank
Code GitHub GitHub repo size GitHub last commit (by committer) Contributors
Tools PyCharm uv Ruff Hatch project
CI/CD Test Publish Publish
Scans Codecov Coverage Quality Gate Status Security Rating Maintainability Rating Reliability Rating Lines of Code Vulnerabilities Bugs Codacy Quality Codacy Coverage CodeFactor DeepSource DeepSource Snyk

Overview

networkx-graphql brings GraphQL querying to NetworkX graph structures in Python. By leveraging Strawberry GraphQL and Graphinate, it automatically generates strongly-typed GraphQL schemas directly from NetworkX graph instances and serves them via an interactive GraphQL web interface.

Whether you are working with simple undirected graphs, complex directed networks, or multi-edge graphs, networkx-graphql allows you to explore and query node relationships with zero GraphQL boilerplate.


Features

  • ⚡ Instant Schema Generation: Automatically derive Strawberry GraphQL schemas from NetworkX graphs in a single line of code.
  • 🕸️ Full NetworkX Compatibility: Seamlessly supports nx.Graph, nx.DiGraph, nx.MultiGraph, and nx.MultiDiGraph.
  • 🏷️ Custom Node Classification: Supply custom extractor functions to dynamically extract node types and structure GraphQL schemas accordingly.
  • 🌐 Embedded GraphQL Server: Built-in HTTP server with an interactive GraphiQL playground powered by Uvicorn and Starlette.
  • 🐍 Fully Typed: Written with Python 3.11+ type annotations for maximum IDE support and type safety.

Installation

Install networkx-graphql using pip:

pip install networkx-graphql

Or using uv:

uv add networkx-graphql

Server Extra

To include web server dependencies (uvicorn, starlette-prometheus), install with the server extra:

pip install "networkx-graphql[server]"
# or with uv
uv add "networkx-graphql[server]"

Quickstart

Spin up a GraphQL server for a NetworkX graph in 3 lines:

import networkx as nx
import networkx_graphql as nxg

# 1. Create any NetworkX graph
graph = nx.ladder_graph(5)

# 2. Generate the GraphQL schema
schema = nxg.schema(graph)

# 3. Start the GraphQL server (default port: 8073)
nxg.run(schema, port=8073)

Now navigate in your browser to http://localhost:8073 (which will redirect to `http://localhost:8073/viewer'), to view the graph using 3D force-directed graph visualizer for the underlying NetworkX graph. The viewer is powered by Graphinate.

Explore the GraphQL schema and run queries using the built-in GraphiQL interface at http://localhost:8073/graphiql.


Usage Examples

Custom Node Type Extractor

By default, all nodes are categorized under a generic type ('node'). You can provide a custom node_type_extractor function to map graph nodes to distinct GraphQL types:

import networkx as nx
import networkx_graphql as nxg

# Create a graph with different node types
graph = nx.DiGraph(name="CompanyNetwork")
graph.add_node("alice", role="manager")
graph.add_node("bob", role="developer")
graph.add_edge("alice", "bob", relationship="manages")


# Define a custom node type extractor
def extract_node_type(node: str) -> str:
    role = graph.nodes[node].get("role", "employee")
    return role.capitalize()


# Generate schema with custom node types
schema = nxg.schema(graph, node_type_extractor=extract_node_type)

# Serve the GraphQL schema
nxg.run(schema, port=8073)

API Reference

networkx_graphql.schema(graph, node_type_extractor=None)

Generates a strawberry.Schema from a NetworkX graph instance.

Parameter Type Description Default
graph nx.Graph Any NetworkX graph instance (Graph, DiGraph, MultiGraph, MultiDiGraph). Required
node_type_extractor Extractor | None A function or callable (node) -> str that returns a GraphQL type name for a node. None

Returns: strawberry.Schema - The generated Strawberry GraphQL schema object.


networkx_graphql.server(graphql_schema, port=8073)

Launches an HTTP server to serve the Strawberry GraphQL schema. Alias: networkx_graphql.run.

Parameter Type Description Default
graphql_schema strawberry.Schema The Strawberry GraphQL schema to serve. Required
port int The HTTP port to run the server on. 8073

Development & Testing

This project uses uv for dependency management and pytest for testing.

Setup Development Environment

# Clone the repository
git clone https://github.com/erivlis/networkx-graphql.git
cd networkx-graphql

# Install dependencies with uv
uv sync --all-extras

Running Tests & Linting

# Run pytest test suite
uv run pytest

# Run linter checks
uv run ruff check .

License

Distributed under the terms of the GNU Lesser General Public License v3.0 or later (LGPLv3+).

Metadata

Release files for networkx-graphql 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for networkx-graphql 0.1.2
File Size Uploaded
networkx_graphql-0.1.2.tar.gz 14.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for networkx-graphql 0.1.2
File Interpreter ABI Platform
networkx_graphql-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 22.0 kB

Release files / networkx_graphql-0.1.2.tar.gz

Download URL networkx_graphql-0.1.2.tar.gz
Size 14.5 kB
Tags Source
SHA-256 checksum
How to use checksums
8664ed42e11e56855337d9392eba1547a29072961abbec6a2cc9dc9c4592de4b
BLAKE2b-256 checksum
How to use checksums
dd618a7c18a0b49e1c8bf3f7de7875092f58121448a7d3315a72040f85fc6946
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / networkx_graphql-0.1.2-py3-none-any.whl

Download URL networkx_graphql-0.1.2-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b0e406c63633ad6e8c2c70013f118e1aa6b0b7509e1146a28d256a3219de6164
BLAKE2b-256 checksum
How to use checksums
5fb996819d54d86ea8a2a7d0eb54035d8a74d2a84d70dc4a1bf2833c1ec23a6d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page