GraphQL Middleware plugin for Sentry.
With GraphQL Yoga
import { GraphQLServer } from 'graphql-yoga'
import { sentry } from 'graphql-middleware-sentry'
const typeDefs = `
type Query {
hello: String!
bug: String!
}
`
const resolvers = {
Query: {
hello: () => `Hey there!`
bug: () => {
throw new Error(`Many bugs!`)
}
}
}
const sentryMiddleware = sentry({
config: {
dsn: process.env.SENTRY_DSN,
environment: process.env.NODE_ENV,
release: process.env.npm_package_version
},
withScope: (scope, error, context) => {
scope.setUser({
id: context.authorization.userId,
});
scope.setExtra('body', context.request.body)
scope.setExtra('origin', context.request.headers.origin)
scope.setExtra('user-agent', context.request.headers['user-agent'])
},
})
const server = GraphQLServer({
typeDefs,
resolvers,
middlewares: [sentryMiddleware]
})
serve.start(() => `Server running on http://localhost:4000`)
export interface Options<Context> {
config: Sentry.NodeOptions
withScope?: ExceptionScope<Context>
captureReturnedErrors?: boolean
forwardErrors?: boolean
}
function sentry<Context>(options: Options<Context>): IMiddlewareFunction
To enrich events sent to Sentry, you can modify the context.
This can be done using the withScope
configuration option.
The withScope
option is a function that is called with the current Sentry scope, the error, and the GraphQL Context.
type ExceptionScope<Context> = (
scope: Sentry.Scope,
error: any,
context: Context,
) => void
property | required | description |
---|---|---|
config |
true | Sentry's config object |
withScope |
false | Function to modify the Sentry context to send with the captured error. |
captureReturnedErrors |
false | Capture errors returned from other middlewares, e.g., graphql-shield returns errors from rules and resolvers |
forwardErrors |
false | Should middleware forward errors to the client or block them. |
This project is licensed under the MIT License.