diff --git a/pages/api/graphql.ts b/pages/api/graphql.ts index fab9828b..608679a5 100644 --- a/pages/api/graphql.ts +++ b/pages/api/graphql.ts @@ -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'; @@ -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) { @@ -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 => { const me = await getMe(req, res); diff --git a/src/api/middleware/getMe.ts b/src/api/middleware/me.ts similarity index 92% rename from src/api/middleware/getMe.ts rename to src/api/middleware/me.ts index 97e093cd..f92fc930 100644 --- a/src/api/middleware/getMe.ts +++ b/src/api/middleware/me.ts @@ -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'; diff --git a/src/api/middleware/sentry.ts b/src/api/middleware/sentry.ts new file mode 100644 index 00000000..e42874d2 --- /dev/null +++ b/src/api/middleware/sentry.ts @@ -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']); + }, +}); diff --git a/src/api/schema/index.ts b/src/api/schema/index.ts index b83516c8..c799eb48 100644 --- a/src/api/schema/index.ts +++ b/src/api/schema/index.ts @@ -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'; @@ -42,5 +17,4 @@ // export default applyMiddleware( // schema, // authorization, -// sentryMiddleware // ); diff --git a/src/api/typeDefs/index.ts b/src/api/typeDefs/index.ts deleted file mode 100644 index 60b0b570..00000000 --- a/src/api/typeDefs/index.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { gql } from 'apollo-server-express'; - -import mirgationSchema from './migration'; -import sessionSchema from './session'; -import userSchema from './user'; -import storefrontSchema from './storefront'; -import paypalSchema from './paypal'; -import stripeSchema from './stripe'; -import courseSchema from './course'; -import bookSchema from './book'; -import upgradeSchema from './upgrade'; -import couponSchema from './coupon'; -import partnerSchema from './partner'; -import communitySchema from './community'; - -const linkSchema = gql` - scalar DateTime - - enum CourseId { - THE_ROAD_TO_LEARN_REACT - TAMING_THE_STATE - THE_ROAD_TO_GRAPHQL - THE_ROAD_TO_REACT_WITH_FIREBASE - } - - enum BundleId { - STUDENT - INTERMEDIATE - PROFESSIONAL - } - - type Query { - _: Boolean - } - - type Mutation { - _: Boolean - } - - type Subscription { - _: Boolean - } -`; - -export default [ - linkSchema, - mirgationSchema, - sessionSchema, - userSchema, - storefrontSchema, - paypalSchema, - stripeSchema, - courseSchema, - bookSchema, - upgradeSchema, - couponSchema, - partnerSchema, - communitySchema, -];