Skip to content
πŸ—ƒ A GraphQL Middleware plugin for Sentry.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci semantic-release May 21, 2018
src
.gitignore Core May 21, 2018
.prettierrc Core May 21, 2018
LICENSE.md Added and linked standalone license file May 21, 2018
README.md fix(readme): fixed CircleCI badge Jan 25, 2019
package.json
renovate.json renovate automerge minor patch Dec 28, 2018
tsconfig.json
tslint.json Core May 21, 2018
yarn.lock feat: make @sentry/node a peer dependency Jan 23, 2019

README.md

graphql-middleware-sentry

CircleCI npm version

GraphQL Middleware plugin for Sentry.

Usage

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
  },
  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`)

Using a Sentry instance

In cases where you want to use your own instance of Sentry to use it in other places in your application you can pass the sentryInstance. The config property should not be passed as an option.

Example usage with a Sentry instance

Sentry.init({
  dsn: process.env.SENTRY_DSN,
})

const sentryMiddleware = sentry({
  sentryInstance: Sentry,
  withScope: (scope, error, context) => {
    scope.setExtra('origin', context.request.headers.origin)
  },
})

API & Configuration

export interface Options<Context> {
  sentryInstance?: Sentry
  config?: Sentry.NodeOptions
  withScope?: ExceptionScope<Context>
  captureReturnedErrors?: boolean
  forwardErrors?: boolean
}

function sentry<Context>(options: Options<Context>): IMiddlewareFunction

Sentry context

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: Error,
  context: Context,
) => void

Options

property required description
sentryInstance false Sentry's instance
config false 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.

Note

If sentryInstance is not passed then config.dsn is required and vice-versa.

License

This project is licensed under the MIT License.

You can’t perform that action at this time.