Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
Error: You must `await server.start()` before calling `server.createHandler()` in next.js

I am getting this error when trying to check if apollo graphql is working fine. Error: You must await server.start() before calling server.createHandler() Note : There is a similar question ,but I am not using express Error: You must `await server.start()` before calling `server.applyMiddleware()`

//api/graphql.js

  import  {  ApolloServer  }  from  "apollo-server-micro";

import  {  resolvers  }  from  "../../apis/resolver";
import  {  typeDefs  }  from  "../../apis/schemas";

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

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

export  default  apolloServer.createHandler({ path:  "/api/graphql"  });

//resolver

import axios from "axios";

export const resolvers = {
  Query: {
    getUsers: async () => {
      try {
        const users = await axios.get("https://api.github.com/users");
        return users.data.map(({ id, login, avatar_url }) => ({
          id,
          login,
          avatar_url
        }));
      } catch (error) {
        throw error;
      }
    },
    getUser: async (_, args) => {
      try {
        const user = await axios.get(
          `https://api.github.com/users/${args.name}`
        );
        return {
          id: user.data.id,
          login: user.data.login,
          avatar_url: user.data.avatar_url
        };
      } catch (error) {
        throw error;
      }
    }
  }
};

//schema

import  {  gql  }  from  "apollo-server-micro"; 

export  const  typeDefs  =  gql`
    type  User {
        id: ID
        login: String
        avatar_url: String
    }

    type  Query {
        getUsers: [User]
        getUser(name: String!): User!
    }`
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

This works for me, copied from the guys at prisma.

import Cors from "micro-cors";
import schema from "graphql/schema";
import { ApolloServer } from "apollo-server-micro";
import { PageConfig } from "next";
import { createContext } from "graphql/context";

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

const cors = Cors();

const server = new ApolloServer({
  context: createContext,
  schema,
});

const startServer = server.start();

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

  await startServer;
  await server.createHandler({ path: "/api/graphql" })(req, res);
});

about 4 years ago · Juan Pablo Isaza Report

0

If you're using an integration package for a non-serverless framework (like express, micro), you must await a call to start immediately after creating your ApolloServer, before attaching it to your web framework and starting to accept requests. This assertStarted() method will assert if the server is started. For more info, see comments of source code

There are some examples, see apollo-server-micro package and this

const { ApolloServer, gql } = require('apollo-server-micro');

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

const resolvers = {
  Query: {
    sayHello(parent, args, context) {
      return 'Hello World!';
    },
  },
};

const apolloServer = new ApolloServer({ typeDefs, resolvers });
module.exports = apolloServer.start().then(() => {
  return apolloServer.createHandler({ path: '/data' });
});
about 4 years ago · Juan Pablo Isaza Report

0

i had the same problem. This is a known bug with an open issue so that for now, you can downgrade apollo-server-micro@^2.

My package.json is now as follows and it works flawlessly.

"dependencies": {
  "apollo-server-micro": "2.25.0",
  "graphql": "^15.6.1",
  "micro": "^9.3.4",
  "mongoose": "^6.0.10",
  "next": "11.1.2",
  "react": "17.0.2",
  "react-dom": "17.0.2" }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!