Démarrage rapide
Reading time: 6 minutes
Learn how to easily build, publish and query a subgraph on The Graph.
Prerequisites
- Un portefeuille crypto
- A smart contract address on a supported network
- Node.js installed
- A package manager of your choice (
npm
,yarn
orpnpm
)
How to Build a Subgraph
1. Create a subgraph in Subgraph Studio
Accédez à Subgraph Studio et connectez votre portefeuille.
Subgraph Studio lets you create, manage, deploy, and publish subgraphs, as well as create and manage API keys.
Cliquez sur « Créer un subgraph ». Il est recommandé de nommer le subgraph en majuscule : « Nom du subgraph Nom de la chaîne ».
2. Installez la CLI Graph
Sur votre machine locale, exécutez l’une des commandes suivantes :
Utilisation de npm :
npm install -g @graphprotocol/graph-cli@latest
Using yarn:
yarn global add @graphprotocol/graph-cli
3. Initialize your subgraph
You can find commands for your specific subgraph on the subgraph page in Subgraph Studio.
The graph init
command will automatically create a scaffold of a subgraph based on your contract’s events.
The following command initializes your subgraph from an existing contract:
graph init
If your contract is verified on the respective blockscanner where it is deployed (such as Etherscan), then the ABI will automatically be created in the CLI.
When you initialize your subgraph, the CLI will ask you for the following information:
- Protocol: Choose the protocol your subgraph will be indexing data from.
- Subgraph slug: Create a name for your subgraph. Your subgraph slug is an identifier for your subgraph.
- Directory: Choose a directory to create your subgraph in.
- Ethereum network (optional): You may need to specify which EVM-compatible network your subgraph will be indexing data from.
- Contract address: Locate the smart contract address you’d like to query data from.
- ABI: If the ABI is not auto-populated, you will need to input it manually as a JSON file.
- Start Block: You should input the start block to optimize subgraph indexing of blockchain data. Locate the start block by finding the block where your contract was deployed.
- Contract Name: Input the name of your contract.
- Index contract events as entities: It is suggested that you set this to true, as it will automatically add mappings to your subgraph for every emitted event.
- Add another contract (optional): You can add another contract.
La capture d’écran suivante donne un exemple de ce qui vous attend lors de l’initialisation de votre subgraph :

4. Edit your subgraph
The init
command in the previous step creates a scaffold subgraph that you can use as a starting point to build your subgraph.
When making changes to the subgraph, you will mainly work with three files:
- Manifest (
subgraph.yaml
) - defines what data sources your subgraph will index. - Schema (
schema.graphql
) - defines what data you wish to retrieve from the subgraph. - AssemblyScript Mappings (
mapping.ts
) - translates data from your data sources to the entities defined in the schema.
For a detailed breakdown on how to write your subgraph, check out Creating a Subgraph.
5. Déployer votre subgraph
Remember, deploying is not the same as publishing.
When you deploy a subgraph, you push it to Subgraph Studio, where you can test, stage and review it. A deployed subgraph’s indexing is performed by the Upgrade Indexer, which is a single Indexer owned and operated by Edge & Node, rather than by the many decentralized Indexers in The Graph Network. A deployed subgraph is free to use, rate-limited, not visible to the public, and meant to be used for development, staging, and testing purposes.
Une fois votre subgraph écrit, exécutez les commandes suivantes :
```shgraph codegen && graph build```
Authentifiez-vous et déployez votre subgraph. La clé de déploiement se trouve sur la page du subgraph dans Subgraph Studio.

```shgraph auth <DEPLOY_KEY>graph deploy <SUBGRAPH_SLUG>```
The CLI will ask for a version label. It’s strongly recommended to use semantic versioning, e.g. 0.0.1
.
6. Examiner votre subgraph
If you’d like to test your subgraph before publishing it, you can use Subgraph Studio to do the following:
-
Exécuter un exemple de requête.
-
Analyser votre subgraph dans le tableau de bord pour vérifier les informations.
-
Vérifier les logs sur le tableau de bord pour voir si des erreurs surviennent avec votre subgraph. Les logs d’un subgraph opérationnel ressembleront à ceci :
Logs du subgraph
7. Publier votre subgraph sur The Graph Network
When your subgraph is ready for a production environment, you can publish it to the decentralized network. Publishing is an onchain action that does the following:
- It makes your subgraph available to be to indexed by the decentralized Indexers on The Graph Network.
- It removes rate limits and makes your subgraph publicly searchable and queryable in Graph Explorer.
- It makes your subgraph available for Curators to curate it.
The greater the amount of GRT you and others curate on your subgraph, the more Indexers will be incentivized to index your subgraph, improving the quality of service, reducing latency, and enhancing network redundancy for your subgraph.
Publier avec Subgraph Studio
Pour publier votre subgraph, cliquez sur le bouton “Publish” dans le tableau de bord.

Sélectionnez le réseau sur lequel vous souhaitez publier votre subgraph.
Publication à partir de la CLI
À partir de la version 0.73.0, vous pouvez également publier votre subgraph avec Graph CLI.
Ouvrez le graph-cli
.
Utilisez les commandes suivantes :
```shgraph codegen && graph build```Then,```shgraph publish```
- Une fenêtre s’ouvrira, vous permettant de connecter votre portefeuille, d’ajouter des métadonnées et de déployer votre subgraph finalisé sur le réseau de votre choix.

To customize your deployment, see Publishing a Subgraph.
Ajout de signal à votre subgraph
-
To attract Indexers to query your subgraph, you should add GRT curation signal to it.
- Cette action améliore la qualité du service, réduit la latence et renforce la redondance et la disponibilité du réseau pour votre subgraph.
-
Si éligibles aux récompenses d’indexation, les Indexeurs reçoivent des récompenses en GRT proportionnelles au montant signalé.
- Il est recommandé de curer au moins 3 000 GRT pour attirer 3 Indexeurs. Vérifiez l’éligibilité aux récompenses en fonction de l’utilisation des fonctionnalités du subgraph et des réseaux supportés.
To learn more about curation, read Curating.
Pour économiser sur les frais de gas, vous pouvez curer votre subgraph dans la même transaction que celle où vous le publiez en sélectionnant cette option :

8. Interroger votre subgraph
You now have access to 100,000 free queries per month with your subgraph on The Graph Network!
You can query your subgraph by sending GraphQL queries to its Query URL, which you can find by clicking the Query button.
For more information about querying data from your subgraph, read Querying The Graph.