Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgrading from apollo-server-micro to apollo server 4

I know this might be an easy task but, I am not understanding the documentation on how to migrate to apollo server 4 . I am currently using Next JS as a framework. I am not using express. I am aiming for a serverless framework

this is my current configuration

Pages/api/index.js

import Cors from "micro-cors";
import ApolloServer from "../../apollo/config/server";

const cors = Cors();

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

const serverStarted = ApolloServer.start();

export default cors(async (req, res) => {
  if (req.method === "OPTIONS") {
    res.end();
    return false;
  }

  await serverStarted;
  await ApolloServer.createHandler({
    path: "/api",
  })(req, res);
});

As for the server file imported

server.js

import { ApolloServer } from "apollo-server-micro";
import schema from "./schema";
import resolvers from "./resolvers";
import { context } from "./context";
import { ApolloServerPluginLandingPageDisabled } from "apollo-server-core";
const typeDefs = schema;

export default new ApolloServer({
  typeDefs,
  resolvers,
  context,
  csrfPrevention: true,
  cache: "bounded",
  plugins: [ApolloServerPluginLandingPageDisabled()],
});
like image 695
Orozcorp Avatar asked Feb 21 '26 21:02

Orozcorp


1 Answers

As Watson from the Apollo team shared, and @DanCrews mentioned there is this integration as-integrations/next

this would go in Pages/api/index.js

import { ApolloServer } from "@apollo/server";
import { ApolloServerPluginLandingPageDisabled } from '@apollo/server/plugin/disabled';
import { startServerAndCreateNextHandler } from "@as-integrations/next";
const server = new ApolloServer({
  resolvers,
  typeDefs,
  csrfPrevention: true,
  cache: "bounded",
  plugins: [ApolloServerPluginLandingPageDisabled()],
});
const handler = startServerAndCreateNextHandler(server, { context });
export default handler;

and there would be no need of an server.js file

like image 111
Orozcorp Avatar answered Feb 24 '26 17:02

Orozcorp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!