banner image 1 banner image 2

How to Monitor Express GraphQL Apps in New Relic

February 22, 2022
4 mins
command
blog-img 1
Rajesh krishnakumar
Author

A guide on how you can instrument your express GraphQl apps and arrest the bottleneck to improve your app performance.

By Rajesh krishnakumar — “Go above and beyond!


Express graphql Server, an open-source GraphQL server that helps you connect a GraphQL schema to an HTTP server in Express node js application.


What is GraphQL?

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, minimizes the amount of data transferred over the network and improves performance, makes it easier to evolve APIs over time, and enables powerful developer tools. With GraphQL, you can fetch and retrieve data in a single endpoint.

Challenges to Monitor

As we read above the GraphQL helps us to query/mutate the data using a single endpoint. This has a downside too, We can’t measure the performance of individual GraphQL operations. Such as calling external services, DB operations, etc. As you see below fig. 1 all the graphql operations mounted under a single endpoint.

GraphQL operation under single endpoint
(Fig. 1 All GraphQL operation under single endpoint)

Solution

To address this problem we will use New relic Node.js agent API -> setTransactionName

Using this api we will set the transaction name. So we can segregate the graphql operations.

As you see in the below Fig. 2 now all the graphql operations are segregated.

Segregated GraphQL operations
(Fig. 2 All GraphQL operations are segregated)

Implementation

There are two ways you can implement this.

  1. Using graphql middleware and updating the transaction name
    a)
    Install graphql-middleware npm package
    b) To Integrate Create the middleware file where you like in your project directory and add below
https://gist.github.com/rajeshkrishnakumar/e66a0e30b667304183d1b4ed268cb8bb#file-graphql-middleware-js

In the above code snippet, you can see we have added a function processRequest in that function we are setting the transaction name.

newrelic.setTransactionName(info.fieldName);

Here we are calling the new relic node agent api to set the transaction name.

info.fieldName -> Get individual graphql operation name

info contains the query AST and more execution information

fieldName: each field in your GraphQL schema needs to be backed by a resolver. The fieldName contains the name for the field that belongs to the current resolver.

c) Integrate this middleware to your graphql server so the request goto the middleware first then it goes to the resolver.

  1. Import the middleware code snippet file 
     2. Apply the middleware 
     3. Registering the schemaWithMiddleware
https://gist.github.com/rajeshkrishnakumar/653300e2225748b7de4d137b1d56b2ef#file-graphql-route-js

Note: Your graphql server initialization may look different.

2. The second approach updates the transaction name in the resolver of each graphql operation.

https://gist.github.com/rajeshkrishnakumar/732017a7b63139240dc0302c3ffc6aae#file-resolver-js

As you see above code snippet we need to import a new relic package and call the node js agent api

Note -> Using the Middleware approach you don’t need to call a new relic agent api in all resolver

We are done. After doing any one of these approaches, boot your app and give some load to various individual operations in your app and check the new relic dashboard you will be able to view the individual graphql operation under the transaction section as shown in Fig. 2.

Conclusion

This was a little guide to show you how you can instrument your express graphql apps and drill down to arrest the bottleneck to improve your app performance. If you are using the apollo graphQL server you can use the below mention plugin and enable graphQL monitoring

[embed]https://github.com/newrelic/newrelic-node-apollo-server-plugin[/embed]

Resource

New relic node api documentation https://docs.newrelic.com/docs/agents/nodejs-agent/api-guides/nodejs-agent-api/


[embed]https://github.com/newrelic/newrelic-node-apollo-server-plugin[/embed]

Meet the team!

Author 
Rajesh Krishnakumar

Reviewer
Muthukumar K

Editor
Mridula Saravanan


We at CaratLane are solving some of the most intriguing challenges to make our mark in the relatively uncharted omnichannel jewellery industry. If you are interested in tackling such obstacles, feel free to drop your updated resume/CV to careers@caratlane.com!
blog-img 2

Discussions

blog-img 3
5 mins
May 17, 2023
Sharing Data Between Controllers: Best Practices S...

This article will help you to understand the diffe

By Naveen C

blog-img 3
5 mins
March 21, 2023
Understanding Auto Layout and Constraints in Swift...

This article gives you an easy way of understandin

By Ramasamy P