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
AI Generated Video Summary
Today we're going to talk about measuring the cost of a GraphQL query with Mercury's Explain. GraphQL allows us to create a list of items we want without needing to know their locations. NearForm created Mercurius Explain, a plugin that adds a Profiler to your GraphQL instance. This plugin provides detailed information about the resolution time and number of resolver calls for each field, making it easier to optimize and debug queries. Mercurius Explain is a valuable tool for understanding the cost of your GraphQL queries.
1. Measuring the Cost of a GraphQL Query
Today we're going to talk about measuring the cost of a GraphQL query with Mercury's Explain. GraphQL allows us to create a list of items we want without needing to know their locations. However, this can lead to problems when it comes to understanding the cost of a query. To address this, NearForm created Mercurius Explain, a plugin that adds a Profiler to your GraphQL instance. This plugin provides detailed information about the resolution time and number of resolver calls for each field, making it easier to optimize and debug queries. Whether you're in production or development, Mercurius Explain is a valuable tool for understanding the cost of your GraphQL queries.
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.