graphql.github.io icon indicating copy to clipboard operation
graphql.github.io copied to clipboard

Docs: Apollo Server example throws error

Open hyposlasher opened this issue 4 years ago • 6 comments

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

hyposlasher avatar Aug 08 '21 09:08 hyposlasher

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

saihaj avatar Aug 08 '21 12:08 saihaj

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();

sellareddy18 avatar Aug 22 '21 15:08 sellareddy18

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

hyposlasher avatar Aug 22 '21 16:08 hyposlasher

I will open pr and fix this issue.

sellareddy18 avatar Aug 22 '21 16:08 sellareddy18

As discussed in https://github.com/graphql/graphql.github.io/pull/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 avatar Nov 13 '22 00:11 hwillson

@hwillson is this still the case?

Urigo avatar Apr 09 '23 18:04 Urigo