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.
Apollo Server Entry Point
Let’s start with an opening file exporting Apollo Server handler and add Sentry initialization to it.
In the file exporting resolvers, we are going to wrap every resolver with the function that will report errors to Sentry.
Map Over Object
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.
Finally, we are ready to write the function that receives an object with resolvers and returns an object with wrapped resolvers.
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.