Okay, the last before going to the real code is the Code-Gen Mercurius function that use the idea of the JQL file to convert the JQL file to TypeScript. So in the target path as source resolver generated TS, you can find all the definition of your server. So you can find, for instance, the mutation, the query and the mutation. This type is used by the developer to be sure the server respect the JQL definition.
Now, if I go back to the GraphQL part, when you set up this, you have only to pay attention or take care about creating the code for your business. So for creating the loader and the resolver. So today, I speak only about the resolver because I have only 20 minutes, but it's similar for the loader. Inside of the resolver folder, you can find the indexer. The resolver is a simple object with a three property, the query, the mutation and the subscription. And in my case, I create a simple folder for each of one of these. So inside of the query, the mutation, you can find all the mutation for the pizza, for the topping and so on. Inside of the query, you can find all the query for the pizza and the topping and so on. And pretty simple in this case. I want to show you, for instance, the mutation for the createPizza. In this case, the implementation is pretty simple. So using this syntax, the typically TypeScript syntax, I create a new function called createPizza. And using the type of the mutation, I say to this function that must respect the signature of the createPizza in the mutation types. In this way, I'm sure that the code must respect the jQuery file described before.
So the function is pretty simple. So there is the parent. In this case, you don't have to pay much attention to the parent. That is the first parameter. The second parameter is the data arrived from the client, in this case, the pizza, so the name and the list of the topping. The third parameter contain the GraphQL context, if you want, the instance of the Fastify application, and also the pubsub object in this case. So using the app, you can get, for instance, the logger, in this case, Pino, or the DbContext. The DbContext, as you can see, is the Prisma client, registered before with the plugin. And in this way, you can call, for instance, the DbContext.pizza to insert the pizza in your database. The DbContext is pretty simple to use, so you can use DbContext, and you can have the pizza, in our case, the topping, and the recipe, if you want. Each model create an object with all the possibility method for insert, select, update, and delete. Pretty simple.
Comments