Skip to content

Commit

Permalink
#23 Migrated GraphQL endpoint configuration to `apollo-server-integra…
Browse files Browse the repository at this point in the history
…tion-next` (#24)

* Migrated to apollo-server-integration-next

* Removed microCors module

* Removed .DS_Store
  • Loading branch information
zaxovaiko authored Oct 10, 2023
1 parent 310a16a commit 65bb3ed
Show file tree
Hide file tree
Showing 8 changed files with 1,493 additions and 915 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ node_modules/
generated/
.swc/
.vercel

.DS_Store
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 };
};
14 changes: 0 additions & 14 deletions server/lib/micro.ts

This file was deleted.

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

1 comment on commit 65bb3ed

@vercel
Copy link

@vercel vercel bot commented on 65bb3ed Oct 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

apollo-blog – ./

apollo-blog-zaxoavoki.vercel.app
apollo-blog.vercel.app
apollo-blog-git-dev-zaxoavoki.vercel.app

Please sign in to comment.