apollo-server/docs/source/getting-started.md
2018-04-05 09:53:52 -04:00

1.8 KiB

title description
Quick start Copy and paste this code to have a GraphQL server running in 30 seconds.

Here's a complete example that sets up a GraphQL server with apollo-server-express. First, make sure to install the necessary modules:

npm install apollo-server graphql

Then, write this code in a file called index.js with the following contents:

const { ApolloServer } = require('apollo-server');

// Some fake data
const books = [
  {
    title: "Harry Potter and the Sorcerer's stone",
    author: 'J.K. Rowling',
  },
  {
    title: 'Jurassic Park',
    author: 'Michael Crichton',
  },
];

// The GraphQL schema
const typeDefs = `
  type Query { books: [Book] }
  type Book { title: String, author: String }
`;

// The resolvers to load data
const resolvers = {
  Query: { books: () => books },
};

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

// run the server!
server.listen(({ url }) =>
  console.log(`Go to ${url}/graphiql to run some queries!`),
);

Now you can start up your brand new Apollo Server by running this command:

node index.js

This is a simple example but it shows the power of Apollo Server already. Instead of writing middleware, managing the request and response, setting up routing, and handling errors, you can focus on what your data looks like and how to load it.

Apollo Server provides two easy ways to get up and running quickly. For GraphQL first apps, the batteries included server setup ApolloServer is the best way to get started. If you have a node app already, Apollo Server provides easy to use middleware to plug into your current app right away.

Creating the server

Adding GraphQL

Running your first query