Cookbook > Construction de subgraphs sur Base

Construction de subgraphs sur Base

Reading time: 3 min

Ce guide vous montrera rapidement comment initialiser, créer et déployer votre subgraph sur le réseau de test de la base.

Ce dont vous avez besoin :

  • A Base Sepolia testnet contract address
  • Un portefeuille cryptographique (par exemple MetaMask ou Coinbase Wallet)

Subgraph Studio

Lien vers cette section

1. Installez la CLI de The Graph

Lien vers cette section

La CLI Graph (>=v0.41.0) est écrite en JavaScript et vous devrez avoir installé « npm » ou « yarn » pour l'utiliser.

# NPM
npm install -g @graphprotocol/graph-cli
# Yarn
yarn global add @graphprotocol/graph-cli

2. Create your subgraph in Subgraph Studio

Lien vers cette section

Go to Subgraph Studio and connect your crypto wallet.

Once connected, click "Create a Subgraph", enter a name for your subgraph and click Create a Subgraph.

3. Initialiser votre subgraph

Lien vers cette section

You can find specific commands for your subgraph in Subgraph Studio.

Assurez-vous que le graph-cli est mis à jour vers la dernière version (supérieure à 0.41.0)

graph --version

Initialiser votre subgraph à partir d'un contrat existant.

graph init --studio <SUBGRAPH_SLUG>

Votre nom de subgraph est un identifiant pour votre subgraph. L'outil CLI vous guidera à travers les étapes de la création d'un subgraph, y compris :

  • Protocol: Ethereum
  • Subgraph slug: <SUBGRAPH_SLUG>
  • Répertoire dans lequel créer le subgraph : <SUBGRAPH_SLUG>
  • Ethereum network: base-sepolia
  • Contract address: <CONTRACT_ADDRESS>
  • Start block (optional)
  • Contract name: <CONTRACT_NAME>
  • Yes/no to indexing events (yes means your subgraph will be bootstrapped with entities in the schema and simple mappings for emitted events)

3. Write your Subgraph

Lien vers cette section

If emitted events are the only thing you want to index, then no additional work is required, and you can skip to the next step.

The previous command creates a scaffold subgraph that you can use as a starting point for building your subgraph. When making changes to the subgraph, you will mainly work with three files:

  • Manifest (subgraph.yaml) - The manifest defines what datasources your subgraphs will index. Make sure to add base-sepolia as the network name in manifest file to deploy your subgraph on Base Sepolia.
  • Schema (schema.graphql) - The GraphQL schema defines what data you wish to retreive from the subgraph.
  • Mappages AssemblyScript (mapping.ts) - Il s'agit du code qui traduit les données de vos sources de données vers les entités définies dans le schéma.

If you want to index additional data, you will need extend the manifest, schema and mappings.

For more information on how to write your subgraph, see Creating a Subgraph.

4. Deploy to Subgraph Studio

Lien vers cette section

Before you can deploy your subgraph, you will need to authenticate with Subgraph Studio. You can do this by running the following command:

Authenticate the subgraph on studio

graph auth --studio <DEPLOY_KEY>

Next, enter your subgraph's directory.

cd <SUBGRAPH_DIRECTORY>

Build your subgraph with the following command:

```
graph codegen && graph build
```

Finally, you can deploy your subgraph using this command:

```
graph deploy --studio <SUBGRAPH_SLUG>
```

5. Query your subgraph

Lien vers cette section

Once your subgraph is deployed, you can query it from your dapp using the Development Query URL in Subgraph Studio.

Note - Studio API is rate-limited. Hence should preferably be used for development and testing.

To learn more about querying data from your subgraph, see the Querying a Subgraph page.

Modifier une page

Précédente
Categorize NFT Marketplaces Using Enums
Suivante
How to Secure API Keys Using Next.js Server Components
Modifier une page