Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
235 changes: 127 additions & 108 deletions website/pages/en/quick-start.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,18 @@
title: Quick Start
---

This guide will quickly take you through how to initialize, create, and deploy your subgraph to Subgraph Studio.
Learn how to easily publish and query a [subgraph](/developing/developer-faqs/#1-what-is-a-subgraph) on The Graph.

Ensure that your subgraph will be indexing data from a [supported network](/developing/supported-networks).

This guide is written assuming that you have:
## Prerequisites for this guide

- A crypto wallet
- A smart contract address on the network of your choice

## 1. Create a subgraph on Subgraph Studio

Go to the [Subgraph Studio](https://thegraph.com/studio/) and connect your wallet.
- A smart contract address on one of the [supported networks](/developing/supported-networks/)

Once your wallet is connected, you can begin by clicking “Create a Subgraph." It is recommended to name the subgraph in Title Case: "Subgraph Name Chain Name."
## Step-by-step

## 2. Install the Graph CLI
### 1. Install the Graph CLI

The Graph CLI is written in TypeScript and you will need to have `node` and either `npm` or `yarn` installed to use it. Check that you have the most recent CLI version installed.
You must have [Node.js](https://nodejs.org/) and a package manager of your choice (`npm`, `yarn` or `pnpm`) installed to use the Graph CLI. Check for the [most recent](https://github.com/graphprotocol/graph-tooling/releases?q=%40graphprotocol%2Fgraph-cli&expanded=true) CLI version.

On your local machine, run one of the following commands:

Expand All @@ -35,133 +29,158 @@ Using [yarn](https://yarnpkg.com/):
yarn global add @graphprotocol/graph-cli
```

## 3. Initialize your subgraph from an existing contract
### 2. Create your subgraph

Initialize your subgraph from an existing contract by running the initialize command:
If your contract has events, the `init` command will automatically create a scaffold of a subgraph.

#### Create via Graph CLI

Use the following command to create a subgraph in Subgraph Studio using the CLI:

```sh
graph init --product subgraph-studio
```

#### Create via Subgraph Studio

Subgraph Studio lets you create, manage, deploy, and publish subgraphs, as well as create and manage API keys.

1. Go to [Subgraph Studio](https://thegraph.com/studio/) and connect your wallet.
2. Click "Create a Subgraph". It is recommended to name the subgraph in Title Case: "Subgraph Name Chain Name".

For additional information on subgraph creation and the Graph CLI, see [Creating a Subgraph](/developing/creating-a-subgraph).

### 3. Initialize your subgraph

#### From an existing contract

The following command initializes your subgraph from an existing contract:

```sh
graph init --studio <SUBGRAPH_SLUG>
```

> You can find commands for your specific subgraph on the subgraph page in [Subgraph Studio](https://thegraph.com/studio/).
> Note: If your contract was verified on Etherscan, then the ABI will automatically be created in the CLI.

When you initialize your subgraph, the CLI tool will ask you for the following information:
You can find commands for your specific subgraph on the subgraph page in [Subgraph Studio](https://thegraph.com/studio/).

- 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 to create the subgraph in: choose your local directory
- 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 autopopulated, you will need to input it manually as a JSON file
- Start Block: it is suggested that you input the start block to save time while your subgraph indexes blockchain data. You can 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
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 to create the subgraph in: Choose your local directory.
- 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.

See the following screenshot for an example for what to expect when initializing your subgraph:

![Subgraph command](/img/subgraph-init-example.png)

## 4. Write your subgraph
### 4. Write your subgraph

The previous commands create 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:
The `init` command in the previous step creates a scaffold subgraph that you can use as a starting point to build your subgraph.

- Manifest (`subgraph.yaml`) - The manifest defines what datasources your subgraphs will index.
- Schema (`schema.graphql`) - The GraphQL schema defines what data you wish to retrieve from the subgraph.
- AssemblyScript Mappings (`mapping.ts`) - This is the code that translates data from your datasources to the entities defined in the schema.
When making changes to the subgraph, you will mainly work with three files:

For more information on how to write your subgraph, see [Creating a Subgraph](/developing/creating-a-subgraph).
- 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.

## 5. Deploy to Subgraph Studio
For a detailed breakdown on how to write your subgraph, check out [Creating a Subgraph](/developing/creating-a-subgraph/).

Once your subgraph is written, run the following commands:
### 5. Deploy your subgraph

```sh
$ graph codegen
$ graph build
```
Remember, deploying is not the same as publishing.

- Authenticate and deploy your subgraph. The deploy key can be found on the Subgraph page in Subgraph Studio.
- When you deploy a subgraph, you push it to [Subgraph Studio](https://thegraph.com/studio/), where you can test, stage and review it.
- When you publish a subgraph, you are publishing it onchain to the decentralized network.

```sh
$ graph auth --studio <DEPLOY_KEY>
$ graph deploy --studio <SUBGRAPH_SLUG>
```
1. Once your subgraph is written, run the following commands:

You will be asked for a version label. It's strongly recommended to use [semver](https://semver.org/) for versioning like `0.0.1`. That said, you are free to choose any string as version such as:`v1`, `version1`, `asdf`.

## 6. Test your subgraph

In Subgraph Studio's playground environment, you can test your subgraph by making a sample query.

The logs will tell you if there are any errors with your subgraph. The logs of an operational subgraph will look like this:

![Subgraph logs](/img/subgraph-logs-image.png)

If your subgraph is failing, you can query the subgraph health by using the GraphiQL Playground. Note that you can leverage the query below and input your deployment ID for your subgraph. In this case, `Qm...` is the deployment ID (which can be located on the Subgraph page under **Details**). The query below will tell you when a subgraph fails, so you can debug accordingly:

```graphql
{
indexingStatuses(subgraphs: ["Qm..."]) {
node
synced
health
fatalError {
message
block {
number
hash
}
handler
}
nonFatalErrors {
message
block {
number
hash
}
handler
}
chains {
network
chainHeadBlock {
number
}
earliestBlock {
number
}
latestBlock {
number
}
lastHealthyBlock {
number
}
}
entityCount
}
}
```
```sh
graph codegen
graph build
```

## 7. Publish your subgraph to The Graph’s Decentralized Network
2. Authenticate and deploy your subgraph. The deploy key can be found on the subgraph's page in Subgraph Studio.

Once your subgraph has been deployed to Subgraph Studio, you have tested it out, and you are ready to put it into production, you can then publish it to the decentralized network.
![Deploy key](/img/subgraph-studio-deploy-key.jpg)

In Subgraph Studio, you will be able to click the publish button on the top right of your subgraph's page.
```sh

Select the network you would like to publish your subgraph to. It is recommended to publish subgraphs to Arbitrum One to take advantage of the [faster transaction speeds and lower gas costs](/arbitrum/arbitrum-faq).
graph auth --studio <DEPLOY_KEY>

The (upgrade Indexer)[/sunrise/#about-the-upgrade-indexer] will begin serving queries on your subgraph regardless of subgraph curation, and it will provide you with 100,000 free queries per month.
graph deploy --studio <SUBGRAPH_SLUG>
```

For a higher quality of service and stronger redundancy, you can curate your subgraph to attract more Indexers. At the time of writing, it is recommended that you curate your own subgraph with at least 3,000 GRT to ensure 3-5 additional Indexers begin serving queries on your subgraph.
- The CLI will ask for a version label.
- It's strongly recommended to use [semantic versioning](https://semver.org/), e.g. `0.0.1`. That said, you can choose any string for the version such as: `v1`, `version1`, `asdf`, etc.

To save on gas costs, you can curate your subgraph in the same transaction that you published it by selecting this button when you publish your subgraph to The Graph’s decentralized network:
### 6. Review your subgraph

![Subgraph publish](/img/publish-and-signal-tx.png)
If you’d like to examine your subgraph before publishing it to the network, you can use [Subgraph Studio](https://thegraph.com/studio/) to do the following:

- Run a sample query.
- Analyze your subgraph in the dashboard to check information.
- Check the logs on the dashboard to see if there are any errors with your subgraph. The logs of an operational subgraph will look like this:

## 8. Query your subgraph
![Subgraph logs](/img/subgraph-logs-image.png)

### 7. Publish your subgraph to The Graph Network

Publishing a subgraph to the decentralized network makes it available for [Curators](/network/curating/) to begin curating it and [Indexers](/network/indexing/) to begin indexing it.

#### Publishing with Subgraph Studio

1. To publish your subgraph, click the Publish button in the dashboard.
2. Select the network to which you would like to publish your subgraph.

#### Publishing from the CLI

As of version 0.73.0, you can also publish your subgraph with the Graph CLI.

1. Open the `graph-cli`.
2. Use the following commands:

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

Then,

```sh
graph publish
```

3. A window will open, allowing you to connect your wallet, add metadata, and deploy your finalized subgraph to a network of your choice.

![cli-ui](/img/cli-ui.png)

To customize your deployment, see [Publishing a Subgraph](/publishing/publishing-a-subgraph/).

#### Adding signal to your subgraph

1. To attract indexers to query your subgraph, you should add GRT curation signal to it.

- This action improves quality of service, reduces latency, and enhances network redundancy and availability for your subgraph.

2. If eligible for indexing rewards, Indexers receive GRT rewards based on the signaled amount.

- It’s recommended to curate at least 3,000 GRT to attract 3 Indexers. Check reward eligibility based on subgraph feature usage and supported networks.

To learn more about curation, read [Curating](/network/curating/).

To save on gas costs, you can curate your subgraph in the same transaction you publish it by selecting this option:

![Subgraph publish](/img/publish-and-signal-tx.png)

Now, you can query your subgraph by sending GraphQL queries to your subgraph’s Query URL, which you can find by clicking on the query button.
### 8. Query your subgraph

If you don't have your API key, you can query via the free, rate-limited development query URL, which can be used for development and staging.
Now, 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 more [here](/querying/querying-the-graph/).
For more information about querying data from your subgraph, read [Querying The Graph](/querying/querying-the-graph/).
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading