How to Create a Subgraph: A Clear Step‑by‑Step Guide.
Article Structure
If you want to learn how to create a subgraph, you first need to know what a subgraph is and why you need one. In simple terms, a subgraph is a smaller graph formed from a larger graph, using a subset of its vertices, edges, or both. Subgraphs appear in pure mathematics, algorithms, and also in blockchain indexing tools like The Graph.
This guide explains how to create a subgraph in two common contexts: basic graph theory and building a subgraph project for The Graph protocol. You can read the whole article or move to the section that matches your use case.
What a Subgraph Is and Why You Might Need One
A subgraph is a graph whose vertices and edges all come from another graph, called the original or parent graph. The subgraph keeps the same connections that appear in the original, but only for the chosen vertices and edges.
People create subgraphs for different reasons. In math and algorithms, subgraphs help simplify a large problem, test a property, or run a faster search. In web3 and blockchain, a subgraph project defines how to read and index on‑chain data so apps can query it easily.
Because the term “subgraph” is used in both fields, the exact steps depend on what you are doing. The next sections walk through both cases, starting with pure graph theory and then moving to The Graph protocol.
How to Create a Subgraph in Graph Theory
In graph theory, a graph is a set of vertices (nodes) and edges (links). To create a subgraph, you select some of those vertices, some of those edges, or both, under clear rules.
There are two common types of subgraphs you should know: general subgraphs and induced subgraphs. The type you choose depends on how strict you want the relationship with the original graph to be.
General Subgraph vs Induced Subgraph
A general subgraph lets you pick any subset of vertices and any subset of edges from the original graph, as long as each chosen edge connects two chosen vertices. You can skip edges even if both endpoints are in your vertex subset.
An induced subgraph is stricter. You first pick a subset of vertices. Then you must include every edge from the original graph that connects any two of those vertices. You cannot choose to leave out a valid edge between selected vertices.
This difference matters in proofs and algorithms. If a statement refers to “any subgraph,” you can usually choose either type. If it refers to an “induced subgraph,” you must follow the stricter rule.
The table below gives a quick comparison of general and induced subgraphs so you can pick the right one for your task.
| Feature | General Subgraph | Induced Subgraph |
|---|---|---|
| Vertex choice | Any subset of vertices from the original graph | Any subset of vertices from the original graph |
| Edge choice | Any subset of edges between chosen vertices | All edges between chosen vertices must be included |
| Flexibility | More flexible, you may drop edges freely | Less flexible, edge set is fully determined by vertices |
| Use cases | Custom filters, partial structures, heuristic search | Proofs, structural properties, strict comparisons |
Once you know which type of subgraph you need, the next step is to apply a clear process. The process is easy to follow by hand and simple to implement in code.
Step‑by‑Step: How to Create a Subgraph from a Given Graph
This section gives a simple process you can follow on paper or in code. The steps work for both directed and undirected graphs, as long as you keep directions consistent.
-
Write down or store the original graph.
Define the vertex set and edge set clearly. For example, let the vertices be V = {A, B, C, D, E} and the edges be E = {{A, B}, {A, C}, {B, C}, {C, D}, {D, E}}. In code, you might store this as an adjacency list or an edge list. -
Decide the purpose of your subgraph.
Ask what you want to study or use. Maybe you care only about vertices near C, or edges that match a rule, like weight below a threshold. Your goal guides which vertices and edges you keep. -
Select a subset of vertices.
Choose the vertices that matter for your task. For example, pick V′ = {A, B, C, D}. Make sure V′ is a subset of V. You can base this on distance, labels, degrees, or any property you define. -
Choose the type of subgraph.
Decide if you need a general subgraph or an induced subgraph. If you need to preserve all connections between chosen vertices, choose an induced subgraph. If you want to drop some edges between chosen vertices, choose a general subgraph. -
Build the edge set for the subgraph.
For an induced subgraph on V′, include every edge from E whose endpoints are both in V′. In the example, that gives E′ = {{A, B}, {A, C}, {B, C}, {C, D}}. For a general subgraph, you may pick any subset of these, such as E′ = {{A, B}, {C, D}}. -
Check that all edges are valid.
Verify that every edge in your subgraph’s edge set connects vertices that are both in V′. If an edge references a vertex outside V′, either remove that edge or add the missing vertex to V′. -
Draw or output the subgraph.
Sketch the subgraph using only V′ and E′, or output the new data structure in your program. Label the subgraph clearly so you do not confuse it with the original graph in later steps.
Once you follow these steps, you have a valid subgraph. You can now run algorithms like search, coloring, or shortest paths only on this smaller structure, which may be easier to handle and faster to process.
Creating a Subgraph Programmatically (Adjacency List Example)
If you work in code, you often represent graphs as adjacency lists or edge lists. The basic idea of how to create a subgraph stays the same, but you apply it with loops and filters.
With an adjacency list, each vertex has a list of neighbors. To form a subgraph, you filter the vertex set and then filter each neighbor list so that only neighbors inside the new vertex set remain.
For example, if you have an adjacency list in a language like Python or JavaScript, you can build a new map or object that includes only the chosen vertices, then copy over edges that connect two allowed vertices. The logic mirrors the step‑by‑step process from the previous section.
How to Create a Subgraph for The Graph Protocol
In web3, “subgraph” usually refers to a project that defines how to index blockchain data using The Graph protocol. This type of subgraph is different from the pure math version, but the idea of working with a smaller, focused view of a large data source is similar.
Here, a subgraph describes which smart contracts to listen to, which events to handle, how to transform the data, and how to expose it through GraphQL queries. The process has more setup, but you can follow clear steps.
The short checklist below highlights the basic pieces you need to think about before and during development of a Graph protocol subgraph.
- Target network and contract addresses you plan to index
- Events and function calls that carry the data you need
- Entities and fields in your GraphQL schema
- Mapping logic that turns events into stored entities
- Deployment target, such as hosted service or decentralized network
Keeping these points in mind from the start helps you design a subgraph that stays clear, stable, and easy to extend as your project grows.
Prerequisites for Building a Graph Protocol Subgraph
Before you create a subgraph for The Graph, you need a few tools and basic skills. You should be comfortable with a command line, Git, and JavaScript or TypeScript. Some knowledge of smart contracts and events also helps.
Make sure you have Node.js, Yarn or npm, and the Graph CLI installed. You also need access to an Ethereum or EVM‑compatible network endpoint, which can be a public RPC URL or a node from a provider.
Step‑by‑Step: How to Create a Subgraph on The Graph
The process for how to create a subgraph on The Graph follows a clear flow. You define the project, set the schema, map events, and then deploy. Here is how the typical workflow looks.
First you scaffold the project, then you configure the files, and finally you deploy and test queries. Each step builds on the previous one, so move in order.
Scaffold and Configure Your Subgraph Project
Start by running the Graph CLI to initialize a new subgraph. You usually provide a name, the network, and the contract address you want to index. The CLI generates a folder with default files, including a manifest, a schema, and mapping stubs.
Next, open the manifest file, often named subgraph.yaml. This file lists the data sources, such as smart contracts, and the events or calls you want to handle. Adjust the contract address, ABI file paths, start block, and any template definitions you need.
Then, edit the GraphQL schema file, usually schema.graphql. Define the entities you want to store, like User, Order, or Transfer, with fields and types. These entities describe the shape of the indexed data that clients can query later.
Write Mappings and Deploy the Subgraph
Once the schema and manifest are set, move to the mapping files. These are AssemblyScript or TypeScript‑like files that turn events and calls from the blockchain into entity updates. For each event handler, you read the event parameters, load or create entities, and save them.
After writing or editing mappings, run the Graph CLI code generation command. This step creates type bindings for your entities and contracts, which simplifies your mapping code. Fix any type errors that appear and run the build command to check the project.
Finally, authenticate with your Graph account and deploy the subgraph to the hosted service or decentralized network endpoint. Once deployed and synced, you can open the GraphQL playground for your subgraph and run queries to check that the indexed data matches your expectations.
Common Mistakes When Creating Subgraphs and How to Avoid Them
Whether you work with pure graphs or The Graph protocol, some errors appear often. Being aware of them helps you avoid wasted time and confusing results.
In graph theory, a frequent mistake is including edges whose endpoints are not both in the chosen vertex set. This breaks the definition of a subgraph. Another mistake is calling a subgraph “induced” even though some edges between chosen vertices were dropped.
For Graph protocol subgraphs, common issues include wrong contract addresses, mismatched ABIs, schema fields that do not match mapping code, and forgetting to regenerate types after schema changes. Careful checks at each step and small test deployments help catch these problems early.
Choosing the Right Subgraph Approach for Your Goal
To close, match your method for how to create a subgraph to your real goal. If you study algorithms, use the clear vertex and edge subset rules from graph theory. Decide early whether you need a general or induced subgraph and stick to that choice.
If you build data APIs on blockchain, focus on the Graph protocol flow. Plan your entities, understand the contracts you index, and treat the subgraph project as a versioned, testable codebase. This mindset keeps your indexed data reliable and easier to maintain.
In both cases, a subgraph is a way to focus on what matters inside a larger structure. With the steps in this guide, you can create subgraphs that are clear, consistent, and useful for your next project.


