New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

The Graph 🛠️ Quickstart to Subgraph Development & Querying - Marcus Rein

ETHGlobalMon, Oct 9, 2023, 12:00 AM

Join Marcus Rein from Edge & Node for a workshop titled "Quickstart to Subgraph Development & Querying". Get building with subgraphs fast! Learn all about The Graph and subgraphs with this code-along workshop were you will learn to deploy, query, and customize a subgraph to fuel your dapp's data needs. Thank you to everyone who joined us for ETHGlobal Paris 2023! Paris is the city of light and birthplace of many revolutions. Therefore we were thrilled to be back in one of the most buzzing and free-spirited cities in Europe for ETHGlobal Paris 2023. A lot has changed in the European ecosystem since the last time we hosted a hackathon in 2019. ----------------- Follow us: https://twitter.com/ETHGlobal​ Check us out: https://ethglobal.co​m Email us at: hello@ethglobal.com

Transcript

foreign [Music] relations at Edge and node developer success and this is going to be a quick start to subgraph development and querying really getting you guys started for the hackathon as soon as possible and the best way to get started with sub graphs is to start from the beginning and that's really understanding uh where you are in the hackathon so if you would like to scan this to understand our prizes also to have a hacker help telegram group quick start videos repos these slides I really tried to put a lot of information in this QR code so that can be very helpful for you and once I see phones down I will move on everyone got it phone up okay I got you all right and real quick I'll go over the prizes for sub graphs as well uh three main prizes we got best new subgraph with those prizes best use of existing subgraph and then pool prize also we just announced substreams literally yesterday so if you want to dive into sub streams and what that is that's also in this QR code sub streams are cool that's the summary diabeted you can it's the same QR code as before they do if you already got it you got it cool all right so if anyone wants to live code you can join adrenaline right here and then this is the repo with all the links no more QR codes after this I promise we'll just get through all this real quick all right everyone is good let's go for it so this Workshop there's gonna be an introduction oh sure I got you sorry sorry sorry yeah I'll wait I'll wait Workshop repo live coding links pretty much if you want to do this along with me or have anything that you learned from today you displayed in front of you we use that all right phone there we go no more QRS I promise all right so this Workshop overview uh it's going to be an introduction to the graph we'll start with then what exactly are subgraphs we're going to find those we'll actually deploy a subgraph live we'll compare queries with two different subgraphs we'll use a subgraph that is pointed to a Smart contract we'll leave another subgraph that's a little bit more well built out looking at the exact same contract and we'll see how it's a little bit different and then finally we'll finish with a little plug for scaffolding 2.0 which connects very nicely with subgraphs now which is pretty cool so let's get into the introduction to the graph first thing when you go to the graph.com you see this access the world's blockchain data so let's dive into that really the data storage of a smart contract is very similar to this it's just very very isolated if you only look at it in it's one point however if you want to understand how that smart contract has existed in time how do you actually do that how do you look at how it has existed over time stamps what you want to know about the provenance you want to know about the history you want to index that data so something like this very simple but we're actually looking back in time just like this apple mock-up for the backups and really we're seeing this kind of data layer backing up backing up backing up over time and having all of this time stamped information over time in a disorganized way just because it's so right optimized we have so much data being piped in any given second as no organization it needs indexing all right so that eventually you think is an issue well there's also another issue we have a read issue with the blockchain where if we're going to be writing to the blockchain so disorganized okay so that's fine we're going to be getting a lot of data back also it's not just the right issue it's a read issue we've got disorganization on the way in and a lot coming back out for example who here used Instagram today anybody who posted on Twitter today anybody I post on Twitter there we go Twitter all right so that was one post that was one right and how many followers do you have or anyone have you know a thousand five hundred followers that's potentially a thousand reads to one right that's the exact same thing we're seeing with data retrieval from the blockchain so it's a problem so there's a web 2.5 solution for reading which is centralized indexing indexing being organizing of the data and that works it totally does you're able to get your data from a certain block and actually get it to your front end totally fine however if that centralized insects in service goes down for whatever reason we are sad that's not a fun time okay so the question was in the beginning getting introduced to the graphs the web 3.0 read solution we have a lot of indexers working permissionlessly in the graph ecosystem and they are all providing you options for your data retrieves all right so they're all indexing that same data piece that you want on the blockchain and you can get it through the graph and that's the graph right there so if any of the indexers for whatever reason to go down you have other options okay all permissionlessly working through the topenomic system of the graph so I love this hope you guys do too so it really provides a foundation for truly decentralized apps if you're really needing indexing you want to be decentralized and have that Security Agency also with Incredible uh speed and uptime it really is there that is the graph Network 450 indexers worldwide it's permissionless redundant uh fast cheaper reliable and a global API so let's go into subgraphs who here's heard of sub graphs buildable subgraphs okay who here has never built with subgraph before cool awesome so we're just getting started awesome so we now know we are swimming in blockchain data all the time as developers it's disorganized horribly but we can index it how is that happening this is basically what a subgraph does it takes all the data and puts a nice clean little bucket that's basically it alright so let's go ahead and put this to words subgraphs are permissionless customizable instructions for indexers to organize your data if you build these instructions awesome they are going to be sent to indexers for you to get your data back and here's basically the pathway it goes through you have on-sain data and off-chain data that's ipfs that is available today and it goes into subgraph.

yaml which is your manifest sorry you define exactly how your sub graph should look like from a high level and then it gets a little bit more into like a medium level almost it's kind of like the mappings and the logic and then eventually you present your data you have your schema which you send queries to and they're nice clean little buckets okay and when you have that all of these subgraphs exist in a permissionless market each of these subgraphs are valuable and have incentivizations mechanisms that exist within the tokenomic system of the graph it's super duper cool so that's a high level overview it can go super deep into this but let's keep on going about how subgraphs work eventually it all comes to this you guys want query responses so we use graphql send out a query and then we enjoy that so that's the high level summary of the graph and sub graphs 1.1 to 2 billion queries per day are running to the graph there's over a thousand subgraphs published on the graph Network which is super cool those are the individual subgraph in the marketplace and there's over 450 indexes okay I said no more QR codes I lied you could take that or not that's fine oh I'm okay I feel bad now yeah all right all right one slows down okay nice on time okay so let's go ahead and get into the code we're going to deploy a starter subgraph and then we're going to compare that to a published oh there we go we're going to uh compare that to a published sub graph that's both subgraphs are gonna be looking at the same smart contract so you can see how different it's going to be to get data back from a basic sub rest this is the sub graph that we're going to play thank you guys that we're going to be comparing our starter sub graph to we're going to look at a little bit more well-built out subgraphs so you have a learning environment where you could see a basic one and a little bit more advanced one um to start we'll go to the graph.com studio if anyone's cool thing along and also mini scan all these links are available um to pop into and throughout this little next section all you're going to do is just trace the punk transfer event that's all I want and we're going to look how the punk transfer event goes all the way to your schema.graphql from Dale you'll learn how a subgraph functions let's deploy a sub graph everyone so what we're going to start with is we will go to the graph.com and we'll make this a little bigger we'll go to products subgraph Studio and we're going to create a subgraph and we'll call it live youth Global Paris demo and we know this smart contract I'll be indexing like I said is crypto punks we know it's on ethereum so I chose ethereum and then we have this dashboard pop up and you're free to actually populate this as you would like like I said these subgraphs exist in a Marketplace so other people will see them it's good to have good documentation and also good dashboard uh etiquette so fill that out if you are looking to join in a published way let's go ahead and initialize the subgraph so go into this copy paste and we'll go ethereum automatically populates the subgraphs slug from the actual CLI and then it'll create a directory automatically as well we know this is on mainnet so we'll select mainnet now we find the contract address so I've already pulled up the crypto punks uh smart contract address and I'm going to bring that right in here it's going to fetch the ABI automatically sometimes depending on the Chain there might be an issue with that and that's okay there's totally a solution for this you need to get the ABI manually so you'll actually go to miniscan which is all Linked In the repo and you can get all that information very easily so you go to ethereum put in the contract address I love miniscan of course you can go to etherscan but it provides everything right there in front of you very useful for subgraph development miniscan is awesome you got the ABI the code all the different events which are awesome so let's go ahead and go back there we go it did it automatically it automatically also found the start Block in which this marked contract was initially deployed so you don't want to index room smart from block zero do you want to go where the smart contract was deployed finds it for you and then the contract name once again etiquette because we are making a subgraph that is public we want to actually have good naming so guess what the name of the smart contract is it's right there in miniscan so copy and paste a lot of copying and pasting we try to make this as easy as possible for you guys to get up and run okay index contract events as entities this is now we're going to be talking about that kind of portal that goes through the subgraph and how we Define it how we have the mappings and then we get the schema this is automatically generating that for you right now crossing fingers on the Wi-Fi we'll see we'll see all right so while that is spinning up we'll see there we go okay awesome so here we could also add more contracts we're not going to do this this is just a starter sub graph we'll press no now it has information on the CLI you could follow through I personally like to go right back to my dashboard and bigger just copy paste once again a lot of copy pasting authenticating your CLI paste authenticated and enter the subgraph paste very easy and then pogen which is actually compile it's actually doing type safety and then graph build is compilate so we have that going through putting everything together and then now we'll write awesome what so uploading to idfs and there we go the dashboard oh all right that's okay we'll just keep on going so just in case this happened I have a recording of this though we'll just go through this together like so so we got right to here to the deploy and that was not working very well earlier so I was having some difficulty on that all right we went through this went through this and here we are okay so we got the deploy going through so at this point we have the dashboard loaded and we could actually see the different entities that have been populated let's go ahead and dive into those entities right now through vs code you guys see that cool so I'll just look at the screen we'll go through this together so we have the subgraph.

yaml which is also known as the Manifest of the subject it really defines at a high level what the subgraph is I'm opening up all the three files right here so we could all just go one by one the subgraph.yaml has the smart contract in there that we defined through the CLI we also have the star Block in there and has high level you're naming the entities you're not really going into all the properties of them also you're naming the event handlers it's just a high level overview to just understand where your subgraph is doing it really is the Manifest so you can see we have that punk transfer the font not defined just named right there let's keep on tracing that punk transfer we'll go down to the automatically generated Punk transfer Handler which is named handle Punk transfer so you have an event being emitted from chain and then from there this is going to start populating the entity of which you will start query so you can see it's right here a let entity equal new Punk transfer type and now we have the entity being populated at the event that param's from becomes transferred or assigned to The Entity Dot from property and literally it's just signing blockchain data directly to the entity for your query okay so let's go ahead and look at where your front-end queries will go you have the schema.graphql this is how you will query using graphql queries you have a entity with the ID from to punk index bikes all that good information there for your front end to gather information from historical data so once again here we transfer we went through the punk transfer event that is there for reference if you need ideas of kind of how to go through a little bit more advanced let's go into some queries right now guys so if you guys have time to uh start to learn a little bit more about subgraphs use these questions and compare the questions that are here could I answer these questions using the starter subgraph or do I need the more advanced stuff I'm like how do you figure that out how do you actually dive into that let's start with figuring out the punk transfers the first 10 in the history of uh crypto punks so just going into the right here through the Explorer you can just go ahead and start sending some test queries and go and I want the first 10 so I ordered by the block time stamp and then I had order Direction being ascending and then we're going to send out that query for a test query and this is the exact shape of the query on the left that you would send out to the API in so you're just testing the queries in the playground seeing what you want to do with that so that was pretty basic question the first hen transfers in the history of crypto punks um oh yeah I wanted to convert the timestamp to show you guys it actually is accurate so you could actually confirm that this is the timestamp of the first transaction in the crypto Street 2017. on that date plan yeah what about the next one find the total value in each sales of all punks ever let's just start with that all right that's a that's a pretty cool question now with a starter subgrass you're like well how do we get that how do we actually compute that in this next subgraph through Gerry okolo he developed the subgraph and extended it a little bit and in a sub graph you can do a little bit of computation you could actually do some processing and in this subgraph he does that so what I'd recommend for you guys figure out how he was able to compute that in his mappings you can do that too you can gather data from the blockchain and actually say hey I want this information to have a little bit of computation and publish this to the query and then you can actually gather that data it really does the work for you in that way it's really nice so let's go ahead and go and looking for the question here find the total value in each sales Jerry did very good documentation and I'd recommend that if you are going to be going through this you want to remember your computation so you go to the schema that graphql remember this is the last point where you actually send your queries to your graphql queries and on this I'm looking for the total Eve sales in the history uh of this I mean going through going through and I'm like wait a minute where is it where is it I know it's here somewhere and there it is total use sales for pump so total sales forever that's not going to be on the blockchain that requires some computation done in the mappings and we don't have to dive into that right now because what I want to do is just show you how you can go through the schema.graphql query his sub graphs and we know it's on the contract there we go and and the ID is going to be the contract ID you could actually he defined it like so should be on the ID the contact address though and we want the total amount traded ever and then total sales and total Supply he named these through the mappings.

ts5 he organically created these and documented very well please go into his documentation and see how he did this I want you this to be a learning opportunity for you guys to see how a basic starter sub graph does it and how a little bit more advanced a little bit of math a little bit of diving so let's go ahead and continue with the slides before I go on we're doing good on time any questions thus far yeah was my question is just about the delay between the uh the event uh application on chain and the recording in the lithograph uh index are so yeah how long does that take to uh so if it index how long does it take to index yeah depends on how much you want to index I mean if you have a smart contract deployed at a certain point you define that start block and it'll start indexing I mean like uh once it's already deployed then um if you want yeah a systems that want like uh like very um uh near real-time uh information how fast response time yeah this is last I heard on the network it's around uh point I think it's 42 milliseconds that's pretty good okay yeah for a team okay okay cool and just one quick other question is about the tokeny mix if um yeah you want to run your uh use a graph I'll go over this really quickly essentially when you publish a subgraph to the network you have to incentivize indexes around the world to get to work so essentially you're saying how much do I want decentralization in my stat if you only put a few tokens you're not going to hold have a whole lot of indexers hop in or start indexing your sub graph if you put in a good number that we say across the board is 10 000 zrt that gets many indexers to hop in and start indexing so it just depends on the indexer their hardware and the sub graphs that they want to index they want to make sure their Hardware can handle maybe you're deploying a massive subgraph so you want to make sure that the indexer can handle that and so they have a choice on if they index or not but 450 the market is pretty healthy with diving in with any sub graph you publish and have a reasonable amount of DRT you're going to get indexed thank you how can I integrate it into my debt do you have any SDK or what whatever so we have an API endpoint and you can use we have what's called graph client Apollo those those are two different options for you and you can send queries to then it's very it's nicely designed so yeah check check out Apollo or graph client API key uh yes there's an API key as well and so I can go through that in a little bit as well yeah okay let's keep um yes quick one how's the schema uh design is it a standard schema yeah I mean I can quickly go through that right now let's do that so this is the schema right here I mean this is does that help answer your question you can definitely customize it you can extend it to your heart's content there's reverse lookups there's there's a lot of things you can do with this yeah that's a lot of power in there too yeah good question all right let's keep on going I got seven minutes awesome okay so we're able to uh go through the punk transfer event all the way through and you can see how just with the boilerplate code of a subgraph being spun up you have a lot of power already to do a little bit more mathematics you need Jerry's subgraph or your own creativity let's keep on going like I said these are for your reference go into this slideshow that's in that repo and you can actually learn a little bit more about patterns that you're commonly seeing in sub graphs and how people are using them I'll just skim through these really quickly please dive in on your own accord and we already compared queries with two different sub graphs and then finally this is something that I'm really excited about because you guys are all Builders scaffold Leaf two who's tried or even scaffold is one I love it that's amazing yeah there we go Kevin so uh Kevin back there has put in an amazing uh uh and also Simone has worked very hard together at building this repo Raider I don't know why I said no more QR codes that's just I apologize again but this is a blog entry written by Kevin Jones uh specific uh to combining a scaffold East to repo with a subgraph it's all dockerized it's all contained and it has super fast feedback loop happiness that feels really good when you are able to deploy your smart contract on hard hat you're actually able to see the exact front end respond immediately and have a subgraph respond as well right away it's this really amazing feedback Niche so um Kevin uh Simone cheers to you guys uh great work with this and if I were starting on a hackathon this is where I would go so um Workshop takeaways decentralized indexing is love uh that's a good one deploy a starter sub graph uh in subgraph studio all right just deploying a starter sub graph and querying it in your hackathon project is enough to get some money so that's pretty cool you don't have to do anything crazy just a starter and query it that's awesome uh you can trace an event that's a really good way to learn you can start from front to back from the schema all the way down to the blockchain or go from the blockchain all the way to schema and see how that is handled all the way through um hopefully this video helped this interaction help um and then also really good way to learn sub graphs and sub graph development is just to play a starter sub graph looking at a smart contract and look at one a little bit more advanced and I've provided that for you today so you could see how that's done compare had a little bit more advanced one is done with some good documentation and just you're off to the race you're good to go and scaffold Leaf is helpful too okay so if you would like to join the graph ecosystem jobs Network roles and Community go ahead and scan here I'll never say that QR code thing again and once phones are down all right okay oh there we go home website who we're good all right and uh q a 53. all right uh yeah I was just wondering is it possible to have any access control over the entity mappings that you were showing so there is the authentication that you have so it is on your computer authenticated and so if there's anyone who's going to be hopping in they need that authentication key I showed mine publicly because that's my developer key I don't really care um but that would need an authentication code you just need to authenticate that a role base access as well um so you could have access into the subgraph there is way there are ways to have like a gnosis multi-sig that has access into a subgraph and can actually use it and alter it at the dashboard level and also at the code level you have the authentication that is that I copy pasted into the CLI so anyone that has that authentication code got it by logging in appropriately either through the multi-sig and that allows a lot of people to use it and then copy paste it into the oh thanks so uh yes I was wondering is there a way to ask the indexer to like simulate an event so that simulate an event so to simulate the transaction and index the resulting events this would have use cases like for example in uni swap when you want to get the fees that are accumulating e you could just ask the indexer to simulate that several times that's a good that's a good question I want to open that up a little bit that's a good question let's talk afterwards afterwards oh perfect all right thank you Marcus thank you so much

Automatic transcript — names and jargon may be misspelled.