Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda