@@ -30,16 +30,19 @@ const resolvers = {
3030}
3131
3232const sentryMiddleware = sentry ({
33- dsn: process .env .SENTRY_DSN ,
3433 config: {
34+ dsn: process .env .SENTRY_DSN ,
3535 environment: process .env .NODE_ENV ,
3636 release: process .env .npm_package_version
3737 },
38- extras: [
39- { name:' body' , path: ' request.body' },
40- { name:' origin' , path: ' request.headers.origin' },
41- { name:' user-agent' , path: " request.headers['user-agent']" }
42- ]
38+ withScope : (scope , error , context ) => {
39+ scope .setUser ({
40+ id: context .authorization .userId ,
41+ });
42+ scope .setExtra (' body' , context .request .body )
43+ scope .setExtra (' origin' , ctx .request .headers .origin )
44+ scope .setExtra (' user-agent' , context .request .headers [' user-agent' ])
45+ },
4346})
4447
4548const server = GraphQLServer ({
@@ -54,35 +57,37 @@ serve.start(() => `Server running on http://localhost:4000`)
5457## API & Configuration
5558
5659``` ts
57- export interface Options {
58- dsn: string
59- config? : Sentry .NodeOptions
60- extras? : Extra []
60+ export interface Options <Context > {
61+ config: Sentry .NodeOptions
62+ withScope? : ExceptionScope <Context >
6163 captureReturnedErrors? : boolean
6264 forwardErrors? : boolean
6365}
6466
65- function sentry(options : Options ): IMiddlewareFunction
67+ function sentry< Context > (options : Options < Context > ): IMiddlewareFunction
6668```
6769
68- ### Extra Content
70+ ### Sentry context
71+
72+ To enrich events sent to Sentry , you can modify the [context ](https :// docs.sentry.io/enriching-error-data/context/?platform=javascript).
73+ This can be done using the ` withScope ` configuration option .
74+
75+ The ` withScope ` option is a function that is called with the current Sentry scope, the error, and the GraphQL Context.
6976
7077```ts
71- interface Extra {
72- name: string
73- path: string // path from ctx
74- }
78+ type ExceptionScope<Context > = (
79+ scope : Sentry .Scope ,
80+ error : any ,
81+ context : Context ,
82+ ) => void
7583```
7684
77- The ` path ` is used to get a value from the context object . It uses [lodash .get ](https :// lodash.com/docs/4.17.11#get) and it should follow its rules.
78-
7985### Options
8086
8187| property | required | description |
8288| ----------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
83- | ` dsn ` | true | Your [Sentry DSN ](https :// docs.sentry.io/error-reporting/quickstart/?platform=node#configure-the-sdk) |
84- | ` config ` | false | [Sentry ' s config object](https://docs.sentry.io/error-reporting/configuration/?platform=node) |
85- | ` extras ` | false | [Extra content ](https :// docs.sentry.io/enriching-error-data/context/?platform=node#extra-context) to send with the captured error. |
89+ | `config` | true | [Sentry's config object](https : // docs.sentry.io/error-reporting/configuration/?platform=node) |
90+ | ` withScope ` | false | Function to modify the [Sentry context ](https : // docs.sentry.io/enriching-error-data/context/?platform=node) to send with the captured error. |
8691| ` captureReturnedErrors ` | false | Capture errors returned from other middlewares , e.g., ` graphql-shield ` [returns errors ](https : // github.com/maticzav/graphql-shield#custom-errors) from rules and resolvers |
8792| ` forwardErrors ` | false | Should middleware forward errors to the client or block them . |
8893
0 commit comments