KCC Documents
English
Search
K

The Graph

Overview

The Graph, a decentralized protocol for indexing and querying chain data, supports the KCC network. Data defined by subgraphs is easy to query and explore. You may create subgraphs locally or use a free hosted explorer for indexing and displaying data.

Steps

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.
# NPM
$ npm install -g @graphprotocol/graph-cli
# Yarn
$ yarn global add @graphprotocol/graph-cli

Initialize your Subgraph

The following command creates a subgraph that indexes all events of an existing contract. It attempts to fetch the contract ABI from BlockScout and returns to requesting a local file path. If any optional arguments are missing, it takes you through an interactive form.
graph init \
--from-contract <CONTRACT_ADDRESS> \
[--network ethereum ] \
[--abi <FILE>] \
<GITHUB_USER>/<SUBGRAPH_NAME> [<DIRECTORY>]
--network: choose “ethereum” both for KCC mainnet and KCC Testnet.
--from-contract <CONTRACT_ADDRESS> is the address of your existing contract which you have deployed on the KCC network: Testnet or Mainnet.
--abi <FILE> is a local path to a contract ABI file (optional, If verified in BlockScout, the graph will grab the ABI, otherwise you will need to manually add the ABI. You can save the abi from BlockScout or by running truffle compile or solc on a public project.)
The <GITHUB_USER> is your github user or organization name, <SUBGRAPH_NAME> is the name for your subgraph, and <DIRECTORY> is the optional name of the directory where graph init will put the example subgraph manifest.
Your subgraph slug is an identifier for your subgraph. The CLI tool will walk you through creating a subgraph, such as a contract address, network, etc., as shown in the screenshot below.

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:
  • Manifest (subgraph.yaml) - The manifest defines what data sources 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 code translates data from your data sources to the entities defined in the schema.
For more information on how to write your subgraph, see Creating a Subgraph.

Authenticate with the hosted service(if you have)

graph auth https://api.thegraph.com/deploy/ <your-access-token>
You can find the access token by going to your dashboard on the graph website.
  • cd to the directory you created and started defining the subgraph. Information on creating a subgraph is available in the Graph Docs here. https://thegraph.com/docs/define-a-subgraph
  • When you are ready, deploy your subgraph. You can always test and redeploy as needed.
If your previously deployed subgraph is still in status Syncing, it will be immediately replaced with the newly deployed version. If the previously deployed subgraph is already fully synced, Graph Node will mark the newly deployed version as the Pending Version, sync it in the background, and only replace the currently deployed version with the new one once syncing the new version has finished. This ensures that you have a subgraph to work with while the new version is syncing.
yarn deploy
Your subgraph will be deployed and can be accessed from your dashboard.
You can learn about querying the subgraph here: https://thegraph.com/docs/query-the-graph#using-the-graph-explorer
If you want to make your subgraph public, you can do so by accessing your subgraph from your dashboard and then click on edit button. You will see the slider at the bottom of edit page.