So, um, that might not be very intriguing. Uh, you're like, okay, I can do all of this with rest already. It might not be as like fancy or snazzy as your, you know, little JSON shaped requests, but I can already do that. Um, one of the places where GraphQL really shines, uh, is in real time APIs. So, um, we'll cover this a little bit later, but, um, subscriptions are the third operation type and they allow you to, as the name implies, subscribe the data. So you could run a subscription and say, um, subscription, uh, uh, messages within the last 10 minutes. And then your, uh, subscription body would be like messages where, um, updated at is less than or equal, you know, date.now minus 10 minutes. Right. And anytime the database, um, has a new record that meets that requirement, you're going to get a real time push of the new data. And that allows you to build reactive, uh, and real time UIs super, super easily. Uh, and this sort of functionality for any of you who have dealt with building a web socket based, uh, applications using things like Socket IO, Socket IO, uh, is typically a lot of work in addition to your regular CRUD API. So the kind of way that real time APIs work before is, uh, you have one option, which is polling. So you just, you know, every X seconds, you're making this repeated request, uh, which is not the most efficient kind of grace. So the other option that you have is, uh, uh, web sockets and, uh, that allows the server to push the data to the client. Um, it, I don't know that it's necessarily like a nightmare, but it, it can get very complicated, especially when you have a lot of subscribed clients. Um, and you need to manage the connections properly. Uh, particularly things like back pressure, um, and handling like connect and disconnecting state. Um, so after, um, the way that it works in GraphQL is you basically point your GraphQL client or your web socket client to your GraphQL endpoint. Again, same single endpoint and you write your subscription. And so, uh, like here, we're saying, uh, I would like to subscribe to the order with this ID. And I want to know if it's been paid and I want to know if it's been dispatched. When that order is updated, uh, our UI will be immediately notified and we'll have the current, uh, and most recent values of paid and dispatched. And we can kind of do with that what we will. So another huge benefit of GraphQL APIs and, uh, maybe one of the biggest is how easy is, is to document and share API. So, um, I don't know how many of you work in startups, uh, or have, you know, tried to bootstrap, uh, you know, soft stuff, but, uh, the typical life cycle of dev docs is that the API developer, builds the API, the API developer writes documentation, which winds up in like Google sheets or like postman or swagger. Um, you read the documentation and then finally you start integrating the API, but the docs are missing, they're out of date, uh, or they're just wrong. Um, or the more realistic scenario is a API developer builds API and there is no documentation. Um, so with graph QL, the way that it works is that you build your graph, QL API, and that's it. Um, you or whoever it is that's responsible for the, the integration, whether it's server side or client side. Uh, cause graph QL works on both. And we'll see that today is you just start integrating it and it's always, always, always up to sync. Um, and the reason for that is because of the pipe system. Um, so we won't go too deeply into this, but a graph QL has its own little, like, uh, what they call SDL schema definition language. and it's a language for defining types, uh, for your graphic service and also, um, operations that you can do on a service. Um, if a type where an operation doesn't exist in the schema definitions, you can't write a resolver for it. So there is no scenario in which something would be available in API and not documented. Uh, the types look very similar to type script, uh, or, uh, SWIFT or like Kotlin or any other like post fix type language. Right. So the really neat part, uh, is this introspection ability that GraphQL has, which is basically you can tell GraphQL, uh, you can send it a special query and you can basically say, Hey, tell me about all the types in your API. Tell me about all the operations in your API. Um, and it's self documenting and then you can generate, you know, all sorts of nice like doc sites and like ERD visualizations. Um, nobody has an excuse of being too lazy to write the docs because they're just the types of there for you. Um, um, so this is the part where we're going to start building stuff. Um, there's a couple of slides I have after this. That's just accessory stuff. Um, I should have put this one at the end, so outside of this, there's some really neat benefits of using GraphQL so you can get type checking and IntelliSense and operations inside of your IDE. Um, and these can be run against a live schema, so it's kind of like having a typed schema docs, uh, at your disposal. Uh, and the other way that you can do this is to use cool as like GraphQL code generator, um, which can generate a typed code for a lot of different frameworks react, view Apollo, um, and spelled. It can also do backend. Uh, so it can generate SDKs that work in node, um, and even has plugins for things like C sharp and Scala and Java and Kotlin.
Comments