Next.js Discord

Discord Forum

Unable to spin apollo server with API route | next 14.2

Unanswered
Naeemgg posted this in #help-forum
Open in Discord
//app/api/graphql/route.ts
import { startServerAndCreateNextHandler } from '@as-integrations/next';
import { ApolloServer } from '@apollo/server';
import { gql } from 'graphql-tag';
import { NextRequest } from 'next/server';

const resolvers = {
  Query: {
    hello: () => 'world',
  },
};

const typeDefs = gql`
  type Query {
    hello: String
  }
`;

const server = new ApolloServer({
  resolvers,
  typeDefs,
});

const handler = startServerAndCreateNextHandler<NextRequest>(server, {
    context: async req => ({ req }),
  });

export { handler as GET, handler as POST };

When I tried to visit http://localhost:3000/api/graphql I got this response
{
    "errors": [
        {
            "message": "module.require is not a function",
            "extensions": {
                "code": "INTERNAL_SERVER_ERROR",
                "stacktrace": [
                    "TypeError: module.require is not a function",
                    "    at createHash (.next\\server\\chunks\\node_modules__pnpm_f22ca6._.js:5380:23)",
                    "    at Object.html (.next\\server\\chunks\\node_modules__pnpm_7ff79c._.js:241:323)",
                    "    at ApolloServer.executeHTTPGraphQLRequest (.next\\server\\chunks\\53229_@apollo_server_dist_157170._.js:6283:77)"
                ]
            }
        }
    ]
}

what am I doing wrong here?

4 Replies

If I add
export const runtime = 'edge';

its working fine in development server
but unable to build the app because of some errors
Error: The edge runtime does not support Node.js 'path' module.

specifically this one
just for clarity I'm not using path anywhere in my entire app
maybe graphql or some other deps using it but not me