And you can do that. So right here, we're just getting a list. We're getting a list of the first 10. But we can change this query pretty easily to allow us to query for a specific product instead of just a, you know, list. And that query is again in the doc and in the tutorial. So you'll notice we change it from, I'll actually paste this right after it so you can see it. We go from querying products, plural, and putting some and asking for the first 10, to querying a single product that has a handle snowboard. So we'll drop that off, run this, and now we'll get just one product back. And again, just like we did before, you can hit control space, you can see what other information is on here, we can grab the description, we can rerun that, we can grab whatever other useful stuff is in here, and a lot of this gets pretty deep. Like we can grab images and under images there's another set of edges and nodes, and so those have like URLs and we can go and we can go and you can grab all this stuff. We'll talk about this a little bit more in a minute, I messed that up, but ignore my bad GraphQL. But the point is you can get very deep with this, because of how GraphQL works. We don't want to go too far into it, we will be using some advanced queries in a little bit, but like I said, it's not terribly important you understand it, because hydrogen will tell you if you get something wrong if you're missing data that it needs, it'll let you know and we'll see that in a bit. But anyway, we can now take that, and then we can query for a specific handle item. We're querying for snowboard, which would be fine, but again, this is a dynamic page, we're not always going to want to pull for just the snowboard. So, we can change this query one more time, and we can actually make it a query that can take a variable. So, again, I'll paste it right below so we can see, we're changing the type of query definition, I suppose you would call it, and setting it up to take a $handle variable, and then we're passing that handle down into our query, so that'll just let us pass in whatever string we want to be able to query for that product. So, whatever is in our URL, we'll pass in as an argument. You can't really see it here, but down here there's this little query variables thing that if you're very careful, you can actually drag it up, and this is a little area that takes some JSON that fulfills the variables of the query we have up top. And again, it's really smart, which is cool. So, I can start typing, and it'll say, oh, you obviously need to supply us the variable handle because it's defined in our query, and here we can type snowboard. And we can run that, and it looks exactly the same except I took out description, but it looks exactly the same. We can change that now to the oxygen or whatever. If we query for a product that doesn't exist, it just gives back null. So, this is us now having a hard-coded query that we can then pass in a dynamically variable to. So, with that, we can then take that and actually put it in the product page. Yeah, so, in our product page, Hydrogen comes with a use shop query hook, which essentially we can take the query that we just wrote in the graphical and translate it to our React code to essentially do the same thing that we were just trying out. So, to try this, let's first import the use shop query hook and the GraphQL tag library. And I just want to point out here that the graphical and this GraphQL tag library are pretty common if you're comfortable working with GraphQL applications elsewhere. This isn't specific to Hydrogen. The use shop query is obviously specific, but if you're comfortable with GraphQL, this should all look pretty familiar. And if you're like, oh my gosh, I don't know what is happening, a lot of this stuff is just some basic GraphQL. So you can learn more about that in like GraphQL specific resources, not so much Shopify. OK. So, using that U shop gray hook, we can pick a paste in that same query that we just had in the graphical on the left. Here, we're just using that shop Curry hook. It accepts to items the query, which is the same query that we just had, and then the variables, which are the same as the variables that we saw in the query variables at the bottom. And that cons data is just going to take the data back. Once that hook has completed, we store it in a data object. And yeah, essentially, we've just kind of copied from the graphical and translated it to our react code. If we check out the products slash example page, that is going to execute this hook now. And you can see because in the URL, the example we know that's not actually a valid product handle. So we're getting a null back in the server terminal. If we update the URL to something that we know exist, like that snowboard that we just saw before, we should be able to get both the ID and the title logged in the server terminal there. So essentially, same thing that we were doing before we had it working in the graphical translated it down to our React component. One thing to do while we're kind of in this area is we're going to extract out that GraphQL query. Queries can get pretty large and unwieldy, and spoiler alert, they will for us when we get to more product information. So we're just going to extract it to a variable and stick it at the bottom of the page. So it's kind of out of our way. Nice.
Comments