General

General overview over Subgraphs

A Subgraph is a piece of code which gets deployed to a TheGraph Node. It indexes data from the blockchain through handlers which listen on emitted events of smart contracts.

Aavegotchi provides multiple subgraphs to fetch fast information from the chain, e.g. stats about the aavegotchi, the marketplace or even the svgs from the aavegotchis itself.

How to Query a Subgraph

The following describes how to interact with Graph endpoints in general. If you know how to work with GraphQL endpoints you can skip this section.

How to Query the Graphs in your app

In order to query with your app the graph you need a graphQL client. There are tons of libraries out there: https://graphql.org/code/. In the following example we work with apollo-fetch. A tiny GraphQL NodeJS Library which has a wide acceptance https://www.npmjs.com/package/apollo-fetch.

You create a client for the subgraph:

const { createApolloFetch } = require('apollo-fetch');
const uri = 'https://api.thegraph.com/subgraphs/name/aavegotchi/aavegotchi-core-matic';
const apolloFetch = createApolloFetch({ uri });

and afterwards create and send a query and log the response:

let query = `{
aavegotchi(id: "4430"){
name
}
}`;
apolloFetch({ query }).then(result => {
const { data, errors, extensions } = result;
if(errors !== undefined) {
console.error(JSON.stringify(errors))
}
console.log(data);
}).catch(error => {
console.error(error);
});

The output should be:

{ aavegotchi: { name: 'ChillETH' } }

In case you do an error the subgraph responses with an error. For example:

{
aavegotchi(id: "4430"){
fieldWhichDoesNOtExist
}
}

returns

[{"locations":[{"line":4,"column":9}],"message":"Type `Aavegotchi` has no field `fieldWhichDoesNOtExist`"}]

Query historical data

If a graph gets deployed it starts indexing the data from a specific block. It iterates through all the events and maintains its own state in a postgresql. The state contains not only the latest information, but also all the previous information. This allows you to fetch data from the history. The following for example will return the name of the aavegotchi at block 18250000:

{
aavegotchi(id: "4430" block: { number: 18250000 }){
name
}
}

Query for specific items or a set of items

You can either fetch single items or multiple. The difference is a s at the end of the entity. This query will return one aavegotchi:

{
aavegotchi(id: "4430"){
name
}
}

whereas this query will return a list of aavegotchis

{
aavegotchis(orderBy: name){
name
}
}