-
Notifications
You must be signed in to change notification settings - Fork 66
/
apolloLogPlugin.ts
49 lines (44 loc) · 1.43 KB
/
apolloLogPlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import {
ApolloServerPlugin,
GraphQLRequestListener,
GraphQLRequestContext
} from 'apollo-server-plugin-base'
import * as Sentry from '@sentry/nextjs'
import { SeverityLevel } from '@sentry/types'
import { BaseContext } from 'next/dist/shared/lib/utils'
const levelDebug: SeverityLevel = 'debug'
/**
* Plugin to capture errors during apollo transaction and send a copy to sentry/logflare
*/
export const apolloLogPlugin: ApolloServerPlugin = {
requestDidStart<TContext>(
_: GraphQLRequestContext<TContext>
): GraphQLRequestListener<BaseContext> {
return {
didEncounterErrors(ctx) {
for (const err of ctx.errors) {
Sentry.withScope(scope => {
scope.setTag('kind', ctx.operation?.operation)
scope.setExtra('query', ctx.request.query)
scope.setExtra('variables', ctx.request.variables)
if (err.path) {
scope.addBreadcrumb({
category: 'query-path',
message: err.path.join(' > '),
level: levelDebug
})
}
const transactionId =
ctx.request.http?.headers.get('x-transaction-id')
if (transactionId) {
scope.setTransactionName(transactionId)
}
Sentry.captureException(err)
})
// Log error so logflare can also receive a copy of the error
console.error(err)
}
}
}
}
}