Skip to content

Docs: Apollo Server example throws error #1078

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

Closed
hyposlasher opened this issue Aug 8, 2021 · 9 comments · Fixed by #1969
Closed

Docs: Apollo Server example throws error #1078

hyposlasher opened this issue Aug 8, 2021 · 9 comments · Fixed by #1969
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

@ghost
Copy link

ghost commented Aug 22, 2021

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

@ghost
Copy link

ghost commented Aug 22, 2021

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?

@novwhisky
Copy link

typeDefs was renamed to schema at some point. Didn't feel like going through the contributor sign-up process, but here's my closed PR addressing it.

@glasser
Copy link
Contributor

glasser commented Feb 18, 2025

The current code is very close to the block in the @apollo/server README except for the issues around typeDefs vs schema (and an unnecessary gql parsing, and reformatting to remove semicolons for some reason) — I'd just take the README code exactly:

import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';

// The GraphQL schema
const typeDefs = `#graphql
  type Query {
    hello: String
  }
`;

// A map of functions which return data for the schema.
const resolvers = {
  Query: {
    hello: () => 'world',
  },
};

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

const { url } = await startStandaloneServer(server);
console.log(`🚀 Server ready at ${url}`);

bignimbus added a commit to bignimbus/graphql.github.io that referenced this issue Feb 19, 2025
@bignimbus
Copy link
Contributor

Posted this change here - thanks all!

saihaj pushed a commit that referenced this issue Mar 3, 2025
* Update Apollo Server code snippet

Resolves #1078

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