Skip to content

Commit

Permalink
sentry middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
rwieruch committed Apr 16, 2020
1 parent fc96a5b commit 6503542
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 87 deletions.
7 changes: 5 additions & 2 deletions pages/api/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApolloServer } from 'apollo-server-micro';
import cors from 'micro-cors';
import { buildSchema } from 'type-graphql';
import { applyMiddleware } from 'graphql-middleware';
import 'reflect-metadata';

import getConnection from '@models/index';
Expand All @@ -12,7 +13,8 @@ import { ServerRequest, ServerResponse } from '@typeDefs/server';
import { ResolverContext } from '@typeDefs/resolver';

import resolvers from '@api/resolvers';
import getMe from '@api/middleware/getMe';
import getMe from '@api/middleware/me';
import sentryMiddleware from '@api/middleware/sentry';
import firebaseAdmin from '@services/firebase/admin';

if (process.env.FIREBASE_ADMIN_UID) {
Expand Down Expand Up @@ -50,7 +52,8 @@ export default async (req: ServerRequest, res: ServerResponse) => {
});

const server = new ApolloServer({
schema,
schema: applyMiddleware(schema, sentryMiddleware),

context: async ({ req, res }): Promise<ResolverContext> => {
const me = await getMe(req, res);

Expand Down
1 change: 1 addition & 0 deletions src/api/middleware/getMe.ts → src/api/middleware/me.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AuthenticationError } from 'apollo-server-micro';

import firebaseAdmin from '@services/firebase/admin';

import { ResolverContext } from '@typeDefs/resolver';
import { ServerResponse, ServerRequest } from '@typeDefs/server';
import { User } from '@typeDefs/user';

Expand Down
27 changes: 27 additions & 0 deletions src/api/middleware/sentry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { sentry } from 'graphql-middleware-sentry';
import * as Sentry from '@sentry/node';

import { ResolverContext } from '@typeDefs/resolver';

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

export default sentry({
sentryInstance: Sentry,
config: {
environment: process.env.NODE_ENV,
},
forwardErrors: true,
captureReturnedErrors: true,
withScope: (scope, error, context: ResolverContext) => {
scope.setUser({
id: context.me?.uid,
email: context.me?.email,
});

scope.setExtra('body', context.req.body);
scope.setExtra('origin', context.req.headers.origin);
scope.setExtra('user-agent', context.req.headers['user-agent']);
},
});
26 changes: 0 additions & 26 deletions src/api/schema/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,8 @@
// import { mergeSchemas } from 'graphql-tools';
// import { applyMiddleware } from 'graphql-middleware';
// import { sentry } from 'graphql-middleware-sentry';
// import * as Sentry from '@sentry/node';

// import { ResolverContext } from '@typeDefs/resolver';

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

// const sentryMiddleware = sentry({
// sentryInstance: Sentry,
// config: {
// environment: process.env.NODE_ENV,
// },
// forwardErrors: true,
// captureReturnedErrors: true,
// withScope: (scope, error, context: ResolverContext) => {
// scope.setUser({
// id: context.me?.uid,
// email: context.me?.email,
// });

// scope.setExtra('body', context.req.body);
// scope.setExtra('origin', context.req.headers.origin);
// scope.setExtra('user-agent', context.req.headers['user-agent']);
// },
// });

// import { Resolvers } from '@generated/server';

// import authorization from '@api/authorization';
Expand All @@ -42,5 +17,4 @@
// export default applyMiddleware(
// schema,
// authorization,
// sentryMiddleware
// );
59 changes: 0 additions & 59 deletions src/api/typeDefs/index.ts

This file was deleted.

0 comments on commit 6503542

Please sign in to comment.