| name | graphinate |
| description | Use this skill to turn data into graphs. Activate when the user wants to understand relationships, dependencies, or structure within their data, even if they don't explicitly mention "graph". This is ideal for tasks involving data lineage, code analysis, or dependency mapping.
|
Skill: Graphinate - Data to Graphs
1. Objective
To use the graphinate library to "hydrate" a Graph Model from a data source, streamlining the pipeline from raw data
to a structured graph representation.
2. Core Concepts & Personas
Graphinate simplifies the creation of graphs by assigning clear roles to its components:
- The Architect (
graphinate.modeling): You define the blueprint of your graph using @model.node and
@model.edge decorators on simple generator functions.
- The Construction Crew (
graphinate.builders): They take your blueprint and construct various outputs, like a
NetworkX object or a GraphQL schema.
- The Artists (
graphinate.renderers): They visualize your graph in different formats, such as matplotlib plots
or Mermaid diagrams.
3. Key Patterns for "The Architect"
You can define a graph in two main ways:
Pattern 1: "Edge First" (Implicit Nodes)
This approach focuses on relationships and is the most straightforward way to generate a graph. Nodes are created
implicitly from the edges you define.
import graphinate
model = graphinate.GraphModel(name="Edge-First Example")
@model.edge()
def dependency():
yield {'source': 'package-a', 'target': 'package-b'}
yield {'source': 'package-a', 'target': 'package-c'}
Pattern 2: "Node First" (Explicit Nodes & Attributes)
For more detailed graphs, define nodes explicitly. This is necessary when you need to attach specific attributes to
them. You can use functions (extractors) to dynamically determine node properties from your data.
type_: A function that returns the node's type.
key: A function that returns a unique identifier for the node.
label: A function that returns the display label for the node.
import graphinate
import operator
model = graphinate.GraphModel(name="Node-First Example")
def get_source_code_items():
yield {'commit_sha': 'abc', 'summary': 'Fix bug', 'type': 'commit'}
yield {'file_path': 'src/main.py', 'type': 'file'}
@model.node(operator.itemgetter('type'),
key=lambda item: item.get('commit_sha') or item.get('file_path'),
label=lambda item: item.get('summary') or item.get('file_path'))
def source_code():
yield from get_source_code_items()
4. Protocols for "The Construction Crew" and "The Artists"
Once you have a model, you use a Builder to construct a graph object, and a Renderer to visualize it.
import graphinate
import networkx as nx
builder = graphinate.builders.NetworkxBuilder(model)
graph: nx.Graph = builder.build()
graphinate.renderers.matplotlib.plot(graph)
mermaid_string = graphinate.builders.MermaidBuilder(model).build()
print(mermaid_string)
5. Gotchas & Best Practices