Querying Blockchain Data with GraphQL

Rate this content
Bookmark

Curious about how data works in the world of blockchain? Join Simon in an engaging session about The Graph, the decentralized indexing protocol that makes it easy for blockchain developers to search and query blockchain data. 


Table of the contents:

- Understanding blockchain data

- Anatomy of a smart contract

- Indexing blockchain data with The Graph

- Accessing data on The Graph

- Recommended subgraphs

- Writing subgraphs overview

FAQ

A blockchain is a decentralized and geographically distributed system where data, like transactions, is stored across thousands of independent validators. It's known for being tamper-proof due to its cryptographic security, making it a reliable database for transactions involving substantial value.

Ethereum differs from Bitcoin primarily through its support for smart contracts. Smart contracts on Ethereum can store data and set rules for modifying that data, enabling more complex applications like NFTs, unlike Bitcoin, which does not support smart contracts.

Smart contracts are programs stored on a blockchain that run when predetermined conditions are met. They automatically execute, control, or document legally relevant events and actions according to the terms of a contract or an agreement.

Ethereum validators are responsible for maintaining the blockchain's integrity. They verify and validate transactions and smart contracts, adding them to the Ethereum blockchain. This decentralized validation process helps ensure security and reliability of the data recorded on the blockchain.

NFTs (Non-Fungible Tokens) are unique digital assets verified using blockchain technology, representing ownership of specific items. They can be bought, sold, or traded. Interactions with NFTs, like transfers, are treated as transactions on the blockchain.

Challenges with blockchain data include the difficulty in accessing historical data, high storage requirements, and the lack of incentivization for data retrieval, which can lead to inefficiencies in accessing and utilizing stored data.

The Graph is a decentralized protocol for indexing and querying data from blockchains using GraphQL. It allows developers to efficiently and securely query blockchain data without having to deal with the complexity of directly interfacing with the blockchain.

The Graph enhances blockchain data accessibility by providing a structured way of indexing and querying data using GraphQL. This abstraction allows developers to access the data they need without managing the lower-level details of blockchain interactions.

Simon Emanuel Schmid
Simon Emanuel Schmid
64 min
29 May, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Simon introduces the basics of blockchain and smart contracts, including decentralization and immutability. The Graph is an indexing protocol that serves as the data layer of web three, providing a solution to challenges with blockchain data. The CryptoPunk example demonstrates the use of subgraphs as a database for specific protocols. The importance of data accuracy and trust in the blockchain is highlighted. The Graph's business model is based on query fees and allows for easy querying and retrieval of NFTs.

1. Introduction to Blockchain and Smart Contracts

Short description:

Simon introduces himself as a developer relations engineer at ActionNode and discusses the basics of blockchain, including decentralization and the concept of a blockchain as an ordered list of transactions. He explains the immutability of blocks and the role of smart contracts in Ethereum. Simon also touches on the idea of using blockchain for data storage and the importance of rules in maintaining the security and ownership of assets like NFTs.

Thank you so much. I'm so excited to welcome Simon to talk about React. Welcome to the DevCon. We'll get started.

Yeah, hi. So my name is Simon. I'm a developer relations engineer at ActionNode. This is one of the companies, we call them core devs, that are working on the graph. Like we discussed together, how blockchain, or how data is stored on the blockchain, and how we can create, we start very high level and go deeper into the actual stuff. I mean, like the whole data on the blockchain is a big domain that could probably be a day of a workshop. Let's start here, and then we can go with questions depending on where you want to go. And as I said, feel free to always just interrupt me to go into a topic a little bit deeper.

So what is a blockchain? So there is this whole buzzword in the blockchain space. So we call it the Web 3 space that talks about the decentralization. And a blockchain, when we look at it from that angle, it is like a geographically decentralized system. So like the blockchain is copied over like actually thousands of times depends on which one. We are a little bit focused on Ethereum. So there are a lot of like, I think, several thousand Ethereum validators right now. And they copy the whole blockchain. So it's like geographically distributed. Also these validators are independent. So it's also juristically decentralized. But logically, the blockchain feels as one thing. Like there is the one blockchain we interact with and the detail that the blockchain is kind of just copied around the world, usually as a user, we don't really feel this, right? So to come that. And the blockchain is in the end like, it's just an ordered list of transactions. So every participant can send a transaction to the blockchain and the blockchain will be included by the validators in an ordered list. And then like one block consists of multiple transactions. And the block, we say it's finalized and added to the chain by the validators and so on and so forth. And like indefinitely it creates these blocks and also like a very important feature about the blockchain is like the blocks are also immutable. Like once a block is included in the blockchain, it stays there forever. That's kind of cool because it makes it this tamper proof system. So like one old block can never be changed like it's cryptographicly almost impossible. And that's why also it works well as a database for financial or like as soon as a lot of value, we work with value, then it's preserved. So that's why people trust it. Yes, and so that's the blockchain. So that's kind of true for Bitcoin and Ethereum. What's different with Ethereum and Bitcoin is that you also have smart contracts. And the smart contract is basically like really on a low level, it is also just a transaction. So when I deploy a smart contract to Ethereum, then this is also just one transaction that are sent to the blockchain and it's stored there in the block. But in a smart contract, what smart contract can do, it can store data and it can... And I can code or we can code X like rules how to change that data. And these two primitives lead to actually a lot of cool stuff. And now when someone else wants to interact with that contract, so let's say my main example are always NFTs, they are simple to understand. So then when someone wants to send an NFT around, then again, like a transaction that is sent to the blockchain, that is basically like calling a function on that smart contract, authenticated as the user. So that's how it works. So we have blockchain blocks, transactions, everything is a transaction, a deployment of a contract is a transaction, interacting with the contract like in the right manner is a transaction, and these are the fundamentals. As I said, like a contract on the blockchain, usually we can think about just simple data storage. It's actually really variables. But one feature about the blockchain is like the, when we have a blockchain node and we ask for the state of the smart contract, the blockchain node always knows about like the current state, so when we look at the crypto punks, for example, these fancy NFTs, pixelated punks that are so super expensive, then we see something like, yeah, which ID is owned by which owner and where is the metadata stored. But like a problem with that data is also, and then they are obviously we have rules, so only the owner can send the NFT, then it makes it really like secure or owned, so to say, like if anybody can just like change it, change it, that would be a little bit stupid.

2. Challenges with Blockchain Data

Short description:

Blockchain data presents challenges in terms of historical state accessibility, provenance, and incentivization. Accessing historical data is difficult due to the large storage capacity and the need to uncompress data. The lack of incentivization for reading from the blockchain and the reliance on third-party node providers further complicate data access. Additionally, the ordering of data and the low-level nature of the JSON-RPC interface pose challenges for data extraction. These issues result in slow loading times and a need for improved protocols. The graph offers a solution to these challenges.

But what we don't see is like who owns it before, so the historical state is not well, I mean, like it's there, but it's not easily accessible. But we know it's, there are some transactions down in the blockchain that changed the state, but to compute back the state, like how was it? I know, like three months ago, basically we would need to redo all these transactions in an opposite way. So the blockchain is an event-sourcing system similar to Redux, if you probably know Redux. Now it is not so cool anymore, but yeah.

Cool, yeah, and I already touched a little bit on the problem that we don't have the so-called provenance, especially for NFT, so provenance actually, it's a term that comes from art and in art provenance means like when I buy, I don't know, like a very expensive image or a picture that I wanna hang in my villa that I don't have. Then I wanna know who owned it before and also like what's also part of the provenance is actually, is it not a fake one, is it the correct one? So when I see that one that was owned by this gallery or that like very famous collector, then, yeah, the chances are high that I don't have a fake one. And on the blockchain, yeah, the provenance kind of record it, but it's hard to get. So what we don't really have is this time machine. So, I mean, theoretically possible the data is there, but you need to understand, like that the Ethereum blockchain for example, when we run a full archive node that has like all the blocks stored since Ethereum started, we end up with a storage capacity of three terabytes currently roughly. But that's even like heavily compressed. So then to access that data, we need to uncompress and stuff, so that makes it slow. Plus, yeah, and you can think, you can see like there are always new blocks produce, so that probably doesn't get away, right? Like we always have, we always need to think about where to store that data and how to access that data. And then to really get the historical view, it gets a bit hard. So that's kind of one of the problems that we see with blockchain data.

The other one is actually the incentivization problem. So, for which is linked to the other one, so we have this idea, or especially in the early days of this fully decentralized apps. That means we have a front end that we can distribute the code. I mean, basically it's HTML, JavaScript and CSS, we can distribute it however we want. I profess is one of these technologies that's worked very similar to a BitTorrent. And then the idea was that everybody runs a blockchain node on their computer, and then the front end directly interact with the blockchain node. This is the bad dream of blockchain researchers is this fully decentralized thing. The cool thing is there are never scaling issues with such a setup, regardless of 10 or 10 billion of users, although 10 billion will be a little bit too much. It scales because there's no server, everything works on their computers, so it's only the blockchain nodes that interact with each other. That was the initial idea, but there's also a problem with the architecture. And also because it went a little bit the way, nowadays almost nobody runs a node on their own computers. People usually rely on third party node providers. When we use Metamask, for example, in the background, we usually use Infura, which is one of the companies that works with Metamask for that stuff. And then when we write data onto the blockchain, we also pay transaction fees in gas. And that kind of incentivized the whole system to work. So the validators get the transactions in, then they can keep some parts of these gas fees and yeah, that's why they bring their blockchain to the chain. But reading from the blockchain is kind of an unused problem or it's not incentivized on the protocol level, for what they say. So that's also where a lot of companies spun up, as I said, like in info around all those networks with mental masks to provide access to the data, but also they need to find a business model around it. And so there is this whole thing. And it's not easy to get to that data. Also, like what I said before is on the blockchain, we store the same space that's ordered by, yeah, like the timestamp, with all the other smart contracts and everything else that goes on on the blockchain. So in order to see, so for example, Maker, let's say Uniswap is probably the most common name here. So in order to see what happened on Uniswap, we need to look at all these transactions and just filter out the ones for Uniswap because all the other protocols share the same block space. Yes, and then that ends up with getting, and also the protocol or the interface to get that data is called JSON-RPC. That's a very low-level thing to interact with a blockchain node. It's really like an RPC. I can call simple functions, but not really nice data extraction or I don't know like what else would be like indexing instructions and so on and so forth. It's very low-level. So that's actually code that I saw in the wild, so when we wanna try to get a token ID, which has all the tokens by an owner, we see all these await statements here, and a lot of them are actually a call back to the JSON RPC endpoint, and maybe it take 100 to 200 milliseconds to resolve. And then we get into stuff like this, like, okay, I get to first this one and I get this one, and then I wait. And if someone has 20 tokens, and for each token, it takes 200 milliseconds, we end up with a page that takes multiple seconds to load. And we know that users, as soon as we go above 100, 200, 300 milliseconds, they usually get impatient, although people that are using Web3 or blockchain applications are a little bit more used to slow waiting time, but I don't think that's a good excuse. So this is kind of the problems. The data extraction, it's not really incentivized. It's the protocol, the underlying protocol is very basic, and so it's quite a hard task. And that's exactly where the graph comes in.

QnA

Watch more workshops on topic

Web3 Workshop - Building Your First Dapp
React Advanced Conference 2021React Advanced Conference 2021
145 min
Web3 Workshop - Building Your First Dapp
Top Content
Featured WorkshopFree
Nader Dabit
Nader Dabit
In this workshop, you'll learn how to build your first full stack dapp on the Ethereum blockchain, reading and writing data to the network, and connecting a front end application to the contract you've deployed. By the end of the workshop, you'll understand how to set up a full stack development environment, run a local node, and interact with any smart contract using React, HardHat, and Ethers.js.
Build with SvelteKit and GraphQL
GraphQL Galaxy 2021GraphQL Galaxy 2021
140 min
Build with SvelteKit and GraphQL
Top Content
Featured WorkshopFree
Scott Spence
Scott Spence
Have you ever thought about building something that doesn't require a lot of boilerplate with a tiny bundle size? In this workshop, Scott Spence will go from hello world to covering routing and using endpoints in SvelteKit. You'll set up a backend GraphQL API then use GraphQL queries with SvelteKit to display the GraphQL API data. You'll build a fast secure project that uses SvelteKit's features, then deploy it as a fully static site. This course is for the Svelte curious who haven't had extensive experience with SvelteKit and want a deeper understanding of how to use it in practical applications.

Table of contents:
- Kick-off and Svelte introduction
- Initialise frontend project
- Tour of the SvelteKit skeleton project
- Configure backend project
- Query Data with GraphQL
- Fetching data to the frontend with GraphQL
- Styling
- Svelte directives
- Routing in SvelteKit
- Endpoints in SvelteKit
- Deploying to Netlify
- Navigation
- Mutations in GraphCMS
- Sending GraphQL Mutations via SvelteKit
- Q&A
End-To-End Type Safety with React, GraphQL & Prisma
React Advanced Conference 2022React Advanced Conference 2022
95 min
End-To-End Type Safety with React, GraphQL & Prisma
Featured WorkshopFree
Sabin Adams
Sabin Adams
In this workshop, you will get a first-hand look at what end-to-end type safety is and why it is important. To accomplish this, you’ll be building a GraphQL API using modern, relevant tools which will be consumed by a React client.
Prerequisites: - Node.js installed on your machine (12.2.X / 14.X)- It is recommended (but not required) to use VS Code for the practical tasks- An IDE installed (VSCode recommended)- (Good to have)*A basic understanding of Node.js, React, and TypeScript
GraphQL for React Developers
GraphQL Galaxy 2022GraphQL Galaxy 2022
112 min
GraphQL for React Developers
Featured Workshop
Roy Derks
Roy Derks
There are many advantages to using GraphQL as a datasource for frontend development, compared to REST APIs. We developers in example need to write a lot of imperative code to retrieve data to display in our applications and handle state. With GraphQL you cannot only decrease the amount of code needed around data fetching and state-management you'll also get increased flexibility, better performance and most of all an improved developer experience. In this workshop you'll learn how GraphQL can improve your work as a frontend developer and how to handle GraphQL in your frontend React application.
Build a Headless WordPress App with Next.js and WPGraphQL
React Summit 2022React Summit 2022
173 min
Build a Headless WordPress App with Next.js and WPGraphQL
Top Content
WorkshopFree
Kellen Mace
Kellen Mace
In this workshop, you’ll learn how to build a Next.js app that uses Apollo Client to fetch data from a headless WordPress backend and use it to render the pages of your app. You’ll learn when you should consider a headless WordPress architecture, how to turn a WordPress backend into a GraphQL server, how to compose queries using the GraphiQL IDE, how to colocate GraphQL fragments with your components, and more.
Relational Database Modeling for GraphQL
GraphQL Galaxy 2020GraphQL Galaxy 2020
106 min
Relational Database Modeling for GraphQL
Top Content
WorkshopFree
Adron Hall
Adron Hall
In this workshop we'll dig deeper into data modeling. We'll start with a discussion about various database types and how they map to GraphQL. Once that groundwork is laid out, the focus will shift to specific types of databases and how to build data models that work best for GraphQL within various scenarios.
Table of contentsPart 1 - Hour 1      a. Relational Database Data Modeling      b. Comparing Relational and NoSQL Databases      c. GraphQL with the Database in mindPart 2 - Hour 2      a. Designing Relational Data Models      b. Relationship, Building MultijoinsTables      c. GraphQL & Relational Data Modeling Query Complexities
Prerequisites      a. Data modeling tool. The trainer will be using dbdiagram      b. Postgres, albeit no need to install this locally, as I'll be using a Postgres Dicker image, from Docker Hub for all examples      c. Hasura

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

From GraphQL Zero to GraphQL Hero with RedwoodJS
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
From GraphQL Zero to GraphQL Hero with RedwoodJS
Top Content
We all love GraphQL, but it can be daunting to get a server up and running and keep your code organized, maintainable, and testable over the long term. No more! Come watch as I go from an empty directory to a fully fledged GraphQL API in minutes flat. Plus, see how easy it is to use and create directives to clean up your code even more. You're gonna love GraphQL even more once you make things Redwood Easy!
Local State and Server Cache: Finding a Balance
Vue.js London Live 2021Vue.js London Live 2021
24 min
Local State and Server Cache: Finding a Balance
Top Content
How many times did you implement the same flow in your application: check, if data is already fetched from the server, if yes - render the data, if not - fetch this data and then render it? I think I've done it more than ten times myself and I've seen the question about this flow more than fifty times. Unfortunately, our go-to state management library, Vuex, doesn't provide any solution for this.For GraphQL-based application, there was an alternative to use Apollo client that provided tools for working with the cache. But what if you use REST? Luckily, now we have a Vue alternative to a react-query library that provides a nice solution for working with server cache. In this talk, I will explain the distinction between local application state and local server cache and do some live coding to show how to work with the latter.
Batteries Included Reimagined - The Revival of GraphQL Yoga
GraphQL Galaxy 2021GraphQL Galaxy 2021
33 min
Batteries Included Reimagined - The Revival of GraphQL Yoga
The Guild has recently released Envelop - a new, modern GraphQL Server Framework and plugin system. In this talk I’ll share a brief overview of Envelop and why you should probably upgrade your existing GraphQL server to it.
Rock Solid React and GraphQL Apps for People in a Hurry
GraphQL Galaxy 2022GraphQL Galaxy 2022
29 min
Rock Solid React and GraphQL Apps for People in a Hurry
In this talk, we'll look at some of the modern options for building a full-stack React and GraphQL app with strong conventions and how this can be of enormous benefit to you and your team. We'll focus specifically on RedwoodJS, a full stack React framework that is often called 'Ruby on Rails for React'.