|
| 1 | +--- |
| 2 | +title: Building Subgraphs on Base |
| 3 | +--- |
| 4 | + |
| 5 | +This guide will quickly take you through how to initialize, create, and deploy your subgraph on Base testnet. |
| 6 | + |
| 7 | +What you'll need: |
| 8 | + |
| 9 | +- A Base testnet contract address |
| 10 | +- A crypto wallet (e.g. MetaMask or Coinbase Wallet) |
| 11 | + |
| 12 | +## Subgraph Studio |
| 13 | + |
| 14 | +### 1. Install the Graph CLI |
| 15 | + |
| 16 | +The Graph CLI (>=v0.41.0) is written in JavaScript and you will need to have either `npm` or `yarn` installed to use it. |
| 17 | + |
| 18 | +```sh |
| 19 | +# NPM |
| 20 | +npm install -g @graphprotocol/graph-cli |
| 21 | + |
| 22 | +# Yarn |
| 23 | +yarn global add @graphprotocol/graph-cli |
| 24 | +``` |
| 25 | + |
| 26 | +### 2. Create your subgraph in the Subgraph Studio |
| 27 | + |
| 28 | +Go to the [Subgraph Studio](https://thegraph.com/studio/) and connect your crypto wallet. |
| 29 | + |
| 30 | +Once connected, click "Create a Subgraph" and enter a name for your subgraph. |
| 31 | + |
| 32 | +Select "Base (testnet)" as the network and click Create Subgraph. |
| 33 | + |
| 34 | +### 3. Initialize your Subgraph |
| 35 | + |
| 36 | +> You can find specific commands for your subgraph in the Subgraph Studio. |
| 37 | +
|
| 38 | +Make sure that the graph-cli is updated to latest (above 0.41.0) |
| 39 | + |
| 40 | +```sh |
| 41 | +graph --version |
| 42 | +``` |
| 43 | + |
| 44 | +Initialize your subgraph from an existing contract. |
| 45 | + |
| 46 | +```sh |
| 47 | +graph init --studio <SUBGRAPH_SLUG> |
| 48 | +``` |
| 49 | + |
| 50 | +Your subgraph slug is an identifier for your subgraph. The CLI tool will walk you through the steps for creating a subgraph, including: |
| 51 | + |
| 52 | +- Protocol: ethereum |
| 53 | +- Subgraph slug: `<SUBGRAPH_SLUG>` |
| 54 | +- Directory to create the subgraph in: `<SUBGRAPH_SLUG>` |
| 55 | +- Ethereum network: base-testnet \_ Contract address: `<CONTRACT_ADDRESS>` |
| 56 | +- Start block (optional) |
| 57 | +- Contract name: `<CONTRACT_NAME>` |
| 58 | +- Yes/no to indexing events (yes means your subgraph will be bootstrapped with entities in the schema and simple mappings for emitted events) |
| 59 | + |
| 60 | +### 3. Write your Subgraph |
| 61 | + |
| 62 | +> 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. |
| 63 | +
|
| 64 | +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: |
| 65 | + |
| 66 | +- Manifest (subgraph.yaml) - The manifest defines what datasources your subgraphs will index. Make sure to add `base-testnet` as the network name in manifest file to deploy your subgraph on Base testnet. |
| 67 | +- Schema (schema.graphql) - The GraphQL schema defines what data you wish to retreive from the subgraph. |
| 68 | +- AssemblyScript Mappings (mapping.ts) - This is the code that translates data from your datasources to the entities defined in the schema. |
| 69 | + |
| 70 | +If you want to index additional data, you will need extend the manifest, schema and mappings. |
| 71 | + |
| 72 | +For more information on how to write your subgraph, see [Creating a Subgraph](/developing/creating-a-subgraph). |
| 73 | + |
| 74 | +### 4. Deploy to the Subgraph Studio |
| 75 | + |
| 76 | +Before you can deploy your subgraph, you will need to authenticate with the Subgraph Studio. You can do this by running the following command: |
| 77 | + |
| 78 | +Authenticate the subgraph on studio |
| 79 | + |
| 80 | +```sh |
| 81 | +graph auth --studio <DEPLOY_KEY> |
| 82 | +``` |
| 83 | + |
| 84 | +Next, enter your subgraph's directory. |
| 85 | + |
| 86 | +````sh |
| 87 | + cd <SUBGRAPH_DIRECTORY> |
| 88 | + ``` |
| 89 | + |
| 90 | +Build your subgraph with the following command: |
| 91 | + |
| 92 | + ``` |
| 93 | + graph codegen && graph build |
| 94 | + ``` |
| 95 | + |
| 96 | +Finally, you can deploy your subgraph using this command: |
| 97 | + |
| 98 | + ``` |
| 99 | + graph deploy --studio <SUBGRAPH_SLUG> |
| 100 | + ``` |
| 101 | + |
| 102 | +### 5. Query your subgraph |
| 103 | + |
| 104 | +Once your subgraph is deployed, you can query it from your dapp using the `Development Query URL` in the Subgraph Studio. |
| 105 | + |
| 106 | +Note - Studio API is rate-limited. Hence should preferably be used for development and testing. |
| 107 | + |
| 108 | +To learn more about querying data from your subgraph, see the [Querying a Subgraph](/querying/querying-the-graph) page. |
| 109 | +```` |
0 commit comments