-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql.js
85 lines (81 loc) · 2.21 KB
/
graphql.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { ApolloServer, ApolloError } from 'apollo-server-lambda';
import schema from './modules';
import models from './models';
import createLoaders from './loaders';
import * as Sentry from '@sentry/node';
Sentry.init({
dsn: process.env.SENTRY_DSN,
release: process.env.VERSION ? `apollo-server-lambda-boilerplate@${process.env.VERSION}` : undefined,
tracesSampleRate: 1.0,
});
const sentryLogger = {
requestDidStart(requestContext) {
if (!process.env.SENTRY_DSN) return; // Disable context logging
return {
didEncounterErrors(ctx) {
requestContext.logger.log('Did Encounter Error')
if (!ctx.operation) {
return;
}
for (const err of ctx.errors) {
if (err instanceof ApolloError) {
continue;
}
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: Sentry.Severity.Debug
});
}
Sentry.captureException(err);
});
}
}
}
}
}
const server = new ApolloServer({
schema,
context: async ({ event, context }) => {
let user;
Sentry.configureScope(scope => scope.clear());
const auth = event.headers.Authorization || event.headers.authorization;
if(auth){
const token = auth.replace(/^Bearer\s/, '');
user = await models.Session.getUser(token);
Sentry.setUser(user);
if (!user) {
Sentry.captureException(new ApolloError('Token expired'));
throw new ApolloError('Token expired');
}
}
return {
headers: event.headers,
functionName: context.functionName,
event,
context,
models,
loaders: createLoaders(models),
user,
};
},
plugins: [sentryLogger],
playground: {
tabs: [
{
endpoint: '/dev/graphql',
},
],
},
});
export const graphqlHandler = server.createHandler({
cors: {
origin: '*',
credentials: true
}
});