Acerca de The Graph
Reading time: 4 min
The Graph is a powerful decentralized protocol that enables seamless querying and indexing of blockchain data. It simplifies the complex process of querying blockchain data, making dapp development faster and easier.
Projects with complex smart contracts such as and NFTs initiatives like store data on the Ethereum blockchain, making it very difficult to read anything other than basic data directly from the blockchain.
In the case of the example listed above, Bored Ape Yacht Club, you can perform basic read operations on . You can read the owner of a certain Ape, read the content URI of an Ape based on their ID, or read the total supply.
-
This can be done because these read operations are programmed directly into the smart contract itself. However, more advanced, specific, and real-world queries and operations like aggregation, search, relationships, and non-trivial filtering, are not possible.
-
For instance, if you want to inquire about Apes owned by a specific address and refine your search based on a particular characteristic, you would not be able to obtain that information by directly interacting with the contract itself.
-
To get more data, you would have to process every single event ever emitted, read the metadata from IPFS using the Token ID and IPFS hash, and then aggregate it.
It would take hours or even days for a decentralized application (dapp) running in a browser to get an answer to these simple questions.
Alternatively, you have the option to set up your own server, process the transactions, store them in a database, and create an API endpoint to query the data. However, this option is , needs maintenance, presents a single point of failure, and breaks important security properties required for decentralization.
Blockchain properties, such as finality, chain reorganizations, and uncled blocks, add complexity to the process, making it time-consuming and conceptually challenging to retrieve accurate query results from blockchain data.
The Graph solves this challenge with a decentralized protocol that indexes and enables the efficient and high-performance querying of blockchain data. These APIs (indexed "subgraphs") can then be queried with a standard GraphQL API.
Today, there is a decentralized protocol that is backed by the open source implementation of that enables this process.
Indexing blockchain data is very difficult, but The Graph makes it easy. The Graph learns how to index Ethereum data by using subgraphs. Subgraphs are custom APIs built on blockchain data that extract data from a blockchain, processes it, and stores it so that it can be seamlessly queried via GraphQL.
-
The Graph uses subgraph descriptions, which are known as the subgraph manifest inside the subgraph.
-
The subgraph description outlines the smart contracts of interest for a subgraph, the events within those contracts to focus on, and how to map event data to the data that The Graph will store in its database.
-
When creating a subgraph, you need to write a subgraph manifest.
-
After writing the
subgraph manifest
, you can use the Graph CLI to store the definition in IPFS and instruct an Indexer to start indexing data for that subgraph.
The diagram below provides more detailed information about the flow of data after a subgraph manifest has been deployed with Ethereum transactions.
El flujo sigue estos pasos:
- Una aplicación descentralizada (dapp) añade datos a Ethereum a través de una transacción en un contrato inteligente.
- El contrato inteligente emite uno o más eventos mientras procesa la transacción.
- Graph Node escanea continuamente la red de Ethereum en busca de nuevos bloques y los datos de tu subgrafo que puedan contener.
- Graph Node encuentra los eventos de la red Ethereum, a fin de proveerlos en tu subgrafo mediante estos bloques y ejecuta los mapping handlers que proporcionaste. El mapeo (mapping) es un módulo WASM que crea o actualiza las entidades de datos que Graph Node almacena en respuesta a los eventos de Ethereum.
- La dapp consulta a través de Graph Node los datos indexados de la blockchain, utilizando el del nodo. El Nodo de The Graph, a su vez, traduce las consultas GraphQL en consultas para su almacenamiento de datos subyacentes con el fin de obtener estos datos, haciendo uso de las capacidades de indexación que ofrece el almacenamiento. La dapp muestra estos datos en una interfaz muy completa para el usuario, a fin de que los end users que usan este subgrafo puedan emitir nuevas transacciones en Ethereum. El ciclo se repite.
The following sections provide a more in-depth look at subgraphs, their deployment and data querying.
Before you write your own subgraph, it's recommended to explore and review some of the already deployed subgraphs. Each subgraph's page includes a GraphQL playground, allowing you to query its data.