Skip to content

Commit

Permalink
Updating quickstart (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMacaulay authored Apr 18, 2023
1 parent 6b7da2b commit 29d307d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 121 deletions.
178 changes: 57 additions & 121 deletions website/pages/en/cookbook/quick-start.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@ This guide will quickly take you through how to initialize, create, and deploy y

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

## Subgraph Studio
This guide is written assuming that you have:

### 1. Install the Graph CLI
- A smart contract address on the network of your choice
- GRT to curate your subgraph
- A crypto wallet

### 1. Create a subgraph on Subgraph Studio

Go to the Subgraph Studio [https://thegraph.com/studio/](https://thegraph.com/studio/) and connect your wallet.

Once connected, you can begin by clicking “create a subgraph.” Select the network of your choice and click continue.

### 2. Install the Graph CLI

The Graph CLI is written in JavaScript and you will need to have either `npm` or `yarn` installed to use it.

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

```sh
# NPM
$ npm install -g @graphprotocol/graph-cli
Expand All @@ -20,21 +32,34 @@ $ npm install -g @graphprotocol/graph-cli
$ yarn global add @graphprotocol/graph-cli
```

### 2. Initialize your Subgraph
### 3. Initialize your Subgraph

> You can find commands for your specific subgraph on the subgraph page in [Subgraph Studio](https://thegraph.com/studio/).
- Initialize your subgraph from an existing contract.
When you initialize your subgraph, the CLI tool 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 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

Initialize your subgraph from an existing contract by running the following command:

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

- Your subgraph slug is an identifier for your subgraph. The CLI tool will walk you through the steps for creating a subgraph, such as contract address, network, etc as you can see in the screenshot below.

![Subgraph command](/img/Subgraph-Slug.png)
See the following screenshot for an example for what to expect when initializing your subgraph:

> Note: Consider adding `--index-events` to the command above to save time. It bootstraps the subgraph with entities in the schema and simple mappings for each emitted event.
![Subgraph command](/img/subgraph-init-example.png)

### 3. 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:

Expand All @@ -44,11 +69,9 @@ The previous commands create a scaffold subgraph that you can use as a starting

For more information on how to write your subgraph, see [Creating a Subgraph](/developing/creating-a-subgraph).

### 4. Deploy to the Subgraph Studio
### 5. Deploy to the Subgraph Studio

- Go to the Subgraph Studio [https://thegraph.com/studio/](https://thegraph.com/studio/) and connect your wallet.
- Click "Create" and enter the subgraph slug you used in step 2.
- Run these commands in the subgraph folder
Once your subgraph is written, run the following commands:

```sh
$ graph codegen
Expand All @@ -62,11 +85,17 @@ $ graph auth --studio <DEPLOY_KEY>
$ graph deploy --studio <SUBGRAPH_SLUG>
```

- You will be asked for a version label. It's strongly recommended to use the following conventions for naming your versions. Example: `0.0.1`, `v1`, `version1`
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

You can test your subgraph by making a sample query in the playground section.

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

The logs should tell you if there are any errors. If your subgraph is failing, you can query the subgraph health by using the [GraphiQL Playground](https://graphiql-online.com/). Use [this endpoint](https://api.thegraph.com/index-node/graphql). 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:
![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:

```sh
{
Expand Down Expand Up @@ -110,119 +139,26 @@ The logs should tell you if there are any errors. If your subgraph is failing, y
}
```

### 6. Query your Subgraph

You can now query your subgraph by following [these instructions](/querying/querying-the-graph). You can query from your dApp if you don't have your API key via the free, rate-limited temporary query URL that can be used for development and staging. You can read the additional instructions for how to query a subgraph from a frontend application [here](/querying/querying-from-an-application).

## Hosted Service
### 7. Publish Your Subgraph to The Graph’s Decentralized Network

### 1. Install the Graph CLI
Once your subgraph has been deployed to the Subgraph Studio, you have tested it out, and are ready to put it into production, you can then publish it to the decentralized network.

"The Graph CLI is an npm package and you will need `npm` or `yarn` installed to use it.

```sh
# NPM
$ npm install -g @graphprotocol/graph-cli

# Yarn
$ yarn global add @graphprotocol/graph-cli
```
In the Subgraph Studio, click on your subgraph. On the subgraph’s page, you will be able to click the publish button on the top right.

### 2. Initialize your Subgraph
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-faq).

- Initialize your subgraph from an existing contract.

```sh
$ graph init --product hosted-service --from-contract <Address>
```

- You will be asked for a subgraph name. The format is `<Github>/<Subgraph Name>`. Ex: `graphprotocol/examplesubgraph`

- If you'd like to initialize from an example, run the command below:

```sh
$ graph init --product hosted-service --from-example
```

- In the case of the example, the subgraph is based on the Gravity contract by Dani Grant that manages user avatars and emits `NewGravatar` or `UpdateGravatar` events whenever avatars are created or updated.

### 3. Write your Subgraph

The previous command will have created a scaffold from where you can build your subgraph. When making changes to the subgraph, you will mainly work with three files:

- Manifest (subgraph.yaml) - The manifest defines what datasources your subgraph will index
- Schema (schema.graphql) - The GraphQL schema define 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

For more information on how to write your subgraph, see [Creating a Subgraph](/developing/creating-a-subgraph).

### 4. Deploy your Subgraph

- Sign into the [Hosted Service](https://thegraph.com/hosted-service/) using your GitHub account
- Click Add Subgraph and fill out the required information. Use the same subgraph name as in step 2.
- Run `codegen` in the subgraph folder

```sh
# NPM
$ npm run codegen

# Yarn
$ yarn codegen
```
Before you can query your subgraph, Indexers need to begin serving queries on it. In order to streamline this process, you can curate your own subgraph using GRT.

- Add your Access token and deploy your subgraph. The access token is found on your dashboard in the Hosted Service.
At the time of writing, it is recommended that you curate your own subgraph with 10,000 GRT to ensure that it is indexed and available for querying as soon as possible.

```sh
$ graph auth --product hosted-service <ACCESS_TOKEN>
$ graph deploy --product hosted-service <GITHUB_USER>/<SUBGRAPH NAME>
```
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:

### 5. Check your logs
![Subgraph publish](/img/publish-and-signal-tx.png)

The logs should tell you if there are any errors. If your subgraph is failing, you can query the subgraph health by using the [GraphiQL Playground](https://graphiql-online.com/). Use [this endpoint](https://api.thegraph.com/index-node/graphql). 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:
### 8. Query your Subgraph

```sh
{
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
}
}
```
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.

### 6. Query your Subgraph
You can query from your dapp if you don't have your API key via the free, rate-limited temporary query URL that can be used for development and staging.

Follow [these instructions](/querying/querying-the-hosted-service) to query your subgraph on the Hosted Service.
For more information about querying data from your subgraph, read more [here](../querying/querying-the-graph/).
Binary file added website/public/img/publish-and-signal-tx.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added website/public/img/subgraph-init-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added website/public/img/subgraph-logs-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 29d307d

Please sign in to comment.