> For the complete documentation index, see [llms.txt](https://docs.aavegotchi.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aavegotchi.com/developers/subgraphs/general.md).

# General

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
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aavegotchi.com/developers/subgraphs/general.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
