Open2

Next.js v13 のAPIで ApolloServer + type-graphql を動かす

yooyoo

Versions

"@apollo/server": "4.9.3",
"@as-integrations/next": "2.0.2"
"next": "13.5.3",
"encoding": "0.1.13",
"type-graphql": "2.0.0-beta.3",
yooyoo

/app/graphql/route.ts

import "reflect-metadata";
import { startServerAndCreateNextHandler } from "@as-integrations/next";
import { ApolloServer } from "@apollo/server";
import { NextRequest } from "next/server";
import { buildSchema } from "type-graphql";
import { resolvers } from "@/graphql/resolvers";
import { Role } from "@/shared/role.enum";

async function createServer(request: NextRequest) {
  const server = new ApolloServer({
    schema: await buildSchema({
      resolvers,
      validate: true,
      authChecker: () => {
        return true;
      },
    }),
  });
  const handler = startServerAndCreateNextHandler<NextRequest>(server);
  return handler(request);
}

export const GET = createServer;
export const POST = createServer;