Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

169
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda