Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Apollo Server example throws error #1078

Open
hyposlasher opened this issue Aug 8, 2021 · 6 comments
Open

Docs: Apollo Server example throws error #1078

hyposlasher opened this issue Aug 8, 2021 · 6 comments
Assignees

Comments

@hyposlasher
Copy link

hyposlasher commented Aug 8, 2021

Description

When trying run Apollo Server code example from https://graphql.org/code/ I got an error:

   throw new Error('You must `await server.start()` before calling `server.' +
            ^

Error: You must `await server.start()` before calling `server.applyMiddleware()`

Steps to Reproduce

  1. npm install apollo-server-express express
  2. run node server.js with this code in server.js:
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');

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

const resolvers = {
  Query: {
    hello: () => 'Hello world!',
  },
};

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

const app = express();
server.applyMiddleware({ app });

app.listen({ port: 4000 }, () =>
  console.log('Now browse to http://localhost:4000' + server.graphqlPath)
);

Expected Result

code runs with no errors

Actual Result

code runs with error

@saihaj
Copy link
Member

saihaj commented Aug 8, 2021

server.start() is a promise so you need to resolve it. Checkout apollo docs on how to use it https://github.com/apollographql/apollo-server/tree/main/packages/apollo-server-express#express

@listentothefrog
Copy link

Hey it is pretty simple to solve this issue. Here is what I did

const express = require("express");
const { ApolloServer, gql } = require("apollo-server-express");

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

const resolvers = {
  Query: {
    hello: () => "Hello world!",
  },
};

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

const main = async () => {
  const app = express();
  await server.start();
  server.applyMiddleware({ app });

  app.listen({ port: 4000 }, () =>
    console.log("Now browse to http://localhost:4000" + server.graphqlPath)
  );
};

main();

Basically create a main function and make it an async function and await server.start();

@hyposlasher
Copy link
Author

Hey it is pretty simple to solve this issue. Here is what I did

const express = require("express");
const { ApolloServer, gql } = require("apollo-server-express");

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

const resolvers = {
  Query: {
    hello: () => "Hello world!",
  },
};

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

const main = async () => {
  const app = express();
  await server.start();
  server.applyMiddleware({ app });

  app.listen({ port: 4000 }, () =>
    console.log("Now browse to http://localhost:4000" + server.graphqlPath)
  );
};

main();

Basically create a main function and make it an async function and await server.start();

Thank you but I know how to solve it. I am just pointing that there is an error in the docs

@listentothefrog
Copy link

I will open pr and fix this issue.

@hwillson
Copy link
Member

As discussed in #1082 this was fixed, but the new instructions are not entirely accurate now that Apollo Server 4 has shipped. We'll get this updated - thanks!

@hwillson hwillson self-assigned this Nov 13, 2022
@Urigo
Copy link
Contributor

Urigo commented Apr 9, 2023

@hwillson is this still the case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants