So you often want to use these things in part of your application, not everything. But let's go back to this definition. And if we take a look at this definition, he's talking about queries and also commands, but he says that commands are also called modifiers or mutators, which gives me an excellent idea, or maybe not an excellent, but an idea.
If we have queries and mutators or commands in CQRS and query subscriptions and mutations in GraphQL, maybe they're somehow related in a way that I can just use like GraphQL queries as queries and GraphQL mutations as commands.
So let me tell you about the thing that we were building. And again, thanks to GPT for helping with these slides. So here's what we want to build. We want to have some kind of like GraphQL API that has a command here with some kind of a resolver or reducer, whatever, that stores some events to that events database, which is basically, each event is stored there the way it sends.
We send it, and then we want to have queries, which basically allows us to basically, oh, there's a typo here, but sorry for that. These things at the bottom are queries where we want to read the data from the database as a state, not as a series of events. So what we can do, we can basically send GraphQL mutation, that mutation can go to that resolver or reducer or whatever. Then store something inside that like events database, some event happened. And then it can create projection or reduce that to a current state and store that current state in some other database. Whenever a user wants to read something from my application, they can just go simply and read that from that cache table basically. If I delete this table at the bottom, I can just replay all the events and get the same value.
So bonus point here is subscriptions because this is not a synchronous application, basically user on the front end doesn't know when the process finishes. The good thing about GraphQL is that it defines subscriptions, which basically allows us to send a real-time message back to our front end and tell the user, hey, this is finished. So let's try to build this with AWS. I'll do this really quickly because you can implement this with many different things. But let me walk you through this process.
So let's try to make it serverless so it's fully managed, we don't need to do anything by ourselves. First, we need some GraphQL layer. We'll use something called AWS AppSync for that. AppSync is completely managed and scalable GraphQL by AWS. It gives you subscriptions out of the box. It integrates with many different services, so you can just write a small integration piece called Resolver. With JavaScript now, there was some weird language called BTL before that, and it scales really well. You don't pay anything at the beginning until you have a lot of users and things like this. Then we want to store these events and these like states, current states somewhere, we can use something called DynamoDB, which is managed and scalable, NoSQL database by AWS. It's basically something like Mongo, but slightly different.
Comments