The development experience with GraphQL makes for a simple and immediate way to access data. Working on the backend means focusing on how and where the data is obtained from, whereas the frontend focuses on retrieving the data necessary for computation and display. This freedom of action, however, hides some possible performance problems. Fragments let you construct sets of fields, and then include them in queries where they’re needed. Often a client requests unnecessary data in an API call with the assumption that the extra fields are free. Instead, they are the cause of extra queries, sometimes even expensive ones. Not knowing the cost of a query makes it impossible to perform optimisation correctly.
Measuring the Cost of a GraphQL Query with mercurius-explain
Hello, everybody. 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, in 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 going to 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 on 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, which centralize the 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. So the main features of this plugin is 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 right here, there is the begin, the end, and the total time a query, a specific resolver took for a specific field of your query. So 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 phases, you can spot very easily a slow field, or if there are any performance issues with this one, 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 the milliseconds. So 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. And that's it. Thank you very much.