Sentry with AWS Lambda running Apollo Server

Image for post
Image for post

Do you have AWS Lambda running GraphQL Apollo Server and want to add Sentry for errors reporting? Let me show you pretty quick how I did it in Pomodoro by Increaser.

Let’s start with an opening file exporting Apollo Server handler and add Sentry initialization to it.

src/lambda.js

In the file exporting resolvers, we are going to wrap every resolver with the function that will report errors to Sentry.

src/graphql/resolvers.js

To map over the object, we will write the function that receives an object and mapper function. The mapper will receive object property value as a first parameter and key as a second.

src/utils/generic.js

Finally, we are ready to write the function that receives an object with resolvers and returns an object with wrapped resolvers.

src/utils/graphql.js

If we catch the error while calling initial resolver, we want to check if an error was thrown intentionally by looking if it is an instance of one of the errors we used in resolvers. If this is the case, we don’t need to report to Sentry.

If resolver failed for unclear reasons, we console log it, just in case. Then we take variables and context(second and third arguments) and send error with all information to Sentry.

We want to wait until Sentry sends the error to see it on the dashboard. We explicitly wait because Lambda can stop after the request is finished and we won’t know that something had happened. In the end, we return internal server error because the client doesn’t care if there some strange error happened in the service or somewhere else.

Reach the next level of focus and productivity with increaser.org.

Image for post
Image for post
Increaser

Written by

Software engineer, creator of increaser.org. More at geekrodion.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store