Today we're going to talk about measuring the cost of a GraphQL query with Mercury's Explain. First, let me introduce myself. My name is Marco Ippolito, I'm a software developer at NearForm and a Node.js collaborator. I'm from Spoltore, which is a small city in Abruzzo, Italy, but I currently live in Milan.
So let's get started. I like to compare GraphQL as our personal shopper. So, we just create a list of items that we want. We don't need to know where the items are located. We don't need to know in which supermarket, in which aisle. We just have a query, which is basically a list, and he's gonna fetch them for us. So unlike REST, there are a few differences. For example, with REST you have to know the path of where the item is located, while with GraphQL you simply don't care. If you change the store layout, you don't care, but this hides some problems.
So what about the cost? Well, there are some implications. For example, when we have a GraphQL query, we can only see the total cost. For example, how many milliseconds it took for the query to resolve, which is not ideal because if we fetch dozens of fields, we don't know which field is more expensive than the other. So this means it's very hard to debug and optimize a slow query, especially if you use fragments. So in big production applications, we tend to use fragments with centralized fields that we want to request, but this makes it hard to spot extra fields that we don't need in a specific query. So we can say that in GraphQL it is very important to know the cost of each field of a query. Otherwise, you're going to have some very big problems with performance.
So this is one of the main reasons why in NearForm we created Mercurius Explain. So Mercurius is the GraphQL adapter for Fastify, and Mercurius Explain is a plugin that can easily add it to your Mercurius instance. The main features of this plugin are the Profiler, which basically retards the resolution time of each GraphQL resolver in the extension Explain attribute of your GraphQL response. This comes very handy because you can see the resolution time in milliseconds. As you can see in this JSON, there is the begin, the end, and the total time a specific resolver took for a specific field of your query. It also features resolver calls, which is basically a counter of how many times a resolver is invoked. It works perfectly in a gateway in a Federation, which is basically what we use for large production applications. It's secured by the sign so you can use your own authorization policies, for example check if in the request there is a specific header, and it's also very fast. We created this plugin which is intended to be used specifically in production, but also during development. That's why this plugin features also a GraphQL plugin. So as you can see from this image, on the left side there is the resolution time of each field for our query. So while you are in development phase you can spot very easily a slow field or if there are any performance issues with this plugin, you can spot it immediately. We also have the waterfall for every resolver. As you can see on the left we have it in miliseconds. For each resolution you can see when it started, when it ended, it's very specific. So I advise you to use this plugin in basically every circumstance, production, development, small, big, because it comes very handy and it's very important in GraphQL to know the cost of a query. So thanks for listening, I'm leaving here my social accounts, and also I want to thank Davide Fiorello who is the mind behind this plugin.
Comments