So let's just import those. So an ID, a name, and an email. And they're all of type string. And let's just do that. Okay, finally, we're going to create a new field under posts where it's called comments, and that's an array of type comment. And then, are we done? Well, kinda.
If we go back to our playground, and we, whoops, you can see now we have this comments type when I can ask for the name and the email of each comment. But we get back a no. Why? Well, that's because we haven't actually told type where to get the data for this comments field yet. So that's where we have to go and define that next.
So under here, we're going to go under post, under comments. I'm going to define a data source. I'm going to say, not an existing one. We're going to define a new one. It's TykRest, the data source. And then we're going to select our JSON placeholder that we just imported. And now we can even select the endpoint itself. And instead of that, we're going to say comments where the post ID equals. And then we're going to inject the ID of the type post and update. And we're going to give it a name, comments endpoint, and then update and update. And now we've told Type, okay, when a request comes in, and somebody asks for a comments field that belongs to posts, go to the comments endpoint and inject the post ID from the parent object. And so now if I fire off the same request, well, I screwed it up. So that sucks. But the idea is that's how it should have worked. So demo gods and all that, but I don't know. Anyway, we have 45 seconds left before we end. So I'm not going to get into debug mode and figure out what I did wrong. But that's the idea in a nutshell.
So what we wanted to talk about was the difference between querying a request for GraphQL, so versus REST. And so with REST, you can see if we wanted all this data, not only did we have to go to three different endpoints, but in addition to that, we had to figure out the relationships. So us on the front end building out, let's say a landing page for a social media application, we had to make a call to user. And then we have to make a second call to get all the posts that belong to this user. So we have to inject the user ID into this call. And then finally, we have to go under comments and comments and inject all of the post IDs and make one API call for each of my posts. So there's potentially dozens and dozens of API calls that we had to make. That is no longer the case. It's just a single GraphQL API request, if it worked, and we got all the data back in one go. So I'm over time now, so I'm going to go ahead and stop.
Comments