Skip to content

Commit

Permalink
Migrated to apollo-server-integration-next
Browse files Browse the repository at this point in the history
  • Loading branch information
zaxovaiko committed Oct 10, 2023
1 parent 310a16a commit 00a7944
Show file tree
Hide file tree
Showing 7 changed files with 1,491 additions and 901 deletions.
Binary file added .DS_Store
Binary file not shown.
2,323 changes: 1,472 additions & 851 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
},
"dependencies": {
"@apollo/client": "3.7.0",
"@apollo/server": "4.9.4",
"@as-integrations/next": "2.0.2",
"@emotion/react": "11.10.4",
"@emotion/server": "11.10.0",
"@mantine/core": "6.0.5",
Expand All @@ -32,16 +34,13 @@
"@mantine/notifications": "6.0.5",
"@prisma/client": "4.13.0",
"@tabler/icons": "1.111.0",
"apollo-server-micro": "3.10.3",
"axios": "1.1.3",
"dayjs": "1.11.6",
"firebase": "9.14.0",
"firebase-admin": "11.2.0",
"graphql": "16.6.0",
"graphql-scalars": "1.20.1",
"lodash": "4.17.21",
"micro": "9.4.1",
"micro-cors": "0.1.1",
"next": "13.0.3",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand All @@ -64,7 +63,6 @@
"@testing-library/jest-dom": "5.16.5",
"@testing-library/react": "13.4.0",
"@types/lodash": "4.14.186",
"@types/micro-cors": "0.1.2",
"@types/node": "18.8.3",
"@types/react": "18.0.21",
"@types/react-dom": "18.0.6",
Expand Down
28 changes: 5 additions & 23 deletions pages/api/graphql.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
import { RequestHandler } from 'micro';
import { startServerAndCreateNextHandler } from '@as-integrations/next';
import { createContextHandler } from 'server/lib/context';

import { createServer } from '../../server/lib/apollo';
import { cors } from '../../server/lib/micro';
import { prisma } from '../../server/prisma';

const apolloServer = createServer();
const startServer = apolloServer.start();

export const handler: RequestHandler = async (req, res) => {
if (req.method === 'OPTIONS') {
res.end();
return;
}

await startServer;
await prisma.$connect();

await apolloServer.createHandler({ path: '/api/graphql' })(req, res);
};

export const config = {
api: {
bodyParser: false,
},
};

export default cors(handler);
export default startServerAndCreateNextHandler(apolloServer, {
context: createContextHandler,
});
4 changes: 1 addition & 3 deletions server/lib/apollo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ApolloServer } from '@apollo/server';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { ApolloServer } from 'apollo-server-micro';
import { DateTimeTypeDefinition, DateTimeResolver } from 'graphql-scalars';

import { createContextHandler } from './context';
import { NodeEnvs } from './enums';
import GraphQLSchema from '../../schema';
import { prisma } from '../prisma';
Expand All @@ -21,7 +20,6 @@ export const createServer = () =>
schema,
cache: 'bounded',
introspection: process.env.NODE_ENV === NodeEnvs.Development,
context: createContextHandler,
});

export const startServer = async (server: ApolloServer) => {
Expand Down
27 changes: 9 additions & 18 deletions server/lib/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ContextFunction } from '@apollo/server';
import { User } from '@prisma/client';
import { MicroRequest } from 'apollo-server-micro/dist/types';
import { DecodedIdToken } from 'firebase-admin/lib/auth/token-verifier';
import { NextApiRequest, NextApiResponse } from 'next';

import { fireAuth } from './firebase';
import { prisma } from '../prisma';
Expand All @@ -10,11 +11,10 @@ export type ApolloContext = {
user: User | null;
};

export const createContextHandler = async ({
req,
}: {
req: MicroRequest;
}): Promise<ApolloContext> => {
export const createContextHandler: ContextFunction<
[NextApiRequest, NextApiResponse<unknown>],
ApolloContext
> = async req => {
const [, token] = req.headers.authorization?.split('Bearer ') ?? [];
if (token) {
try {
Expand All @@ -32,19 +32,10 @@ export const createContextHandler = async ({
},
});

return {
decodedToken,
user,
};
return { decodedToken, user };
} catch {
return {
decodedToken: null,
user: null,
};
return { decodedToken: null, user: null };
}
}
return {
decodedToken: null,
user: null,
};
return { decodedToken: null, user: null };
};
4 changes: 2 additions & 2 deletions server/resolvers/mutations/updateUser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ValidationError } from 'apollo-server-micro';
import { MutationResolvers } from 'generated/server';
import { GraphQLError } from 'graphql';
import { isNil, omitBy } from 'lodash';
import { DEFAULT_USER_AVATAR, ErrorNames } from 'server/lib/enums';
import { checkUserPermissionsOrThrow } from 'server/lib/utils';
Expand All @@ -15,7 +15,7 @@ export const updateUser: MutationResolvers['updateUser'] = async (
const { displayName, avatar } = input;

if (displayName && displayName.trim() === '') {
throw new ValidationError(ErrorNames.UserAlreadyExists);
throw new GraphQLError(ErrorNames.UserAlreadyExists);
}

return prisma.user.update({
Expand Down

0 comments on commit 00a7944

Please sign in to comment.