Upgrading Next.js example to apollo-server v3 #5503
-
I tried upgrading this example to use I can then create a PR for the Next.js examples EDIT: you'll find a working solution down below. Feel free to suggest improvements :) You can clone this repo to test it |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
In my case, I do const serverStart = ApolloServer.start() // only needs to be called once
const handler: NextApiHandler = async (req, res) => {
await serverStart
const graphqlHandler = ApolloServer.createHandler({ path: '/api/graphql' }) to avoid "suprising state" from Apollo. |
Beta Was this translation helpful? Give feedback.
-
Update: This solution worked: import { PageConfig } from "next";
import Cors from "micro-cors";
import { ApolloServer, gql } from "apollo-server-micro";
const typeDefs = gql`
type Query {
users: [User!]!
}
type User {
name: String
}
`;
const resolvers = {
Query: {
users(parent, args, context) {
return [{ name: "Nextjs" }];
},
},
};
const cors = Cors({
origin: "https://studio.apollographql.com",
allowCredentials: true,
});
const apolloServer = new ApolloServer({ typeDefs, resolvers });
const startServer = apolloServer.start();
export default cors(async (req, res) => {
if (req.method === "OPTIONS") {
res.end();
return false;
}
await startServer;
await apolloServer.createHandler({
path: "/api/graphql",
})(req, res);
});
// // Apollo Server Micro takes care of body parsing
export const config: PageConfig = {
api: {
bodyParser: false,
},
}; You can clone this repo to test it |
Beta Was this translation helpful? Give feedback.
-
FWIW I don't know if next.js is different from micro here, but with micro you can do |
Beta Was this translation helpful? Give feedback.
Update:
This solution worked: