Pre-requisites:

  • Node js
  • Npm
  • VS Code
  1. Create a new project. In my case it’s apollo-testing.
  2. Open the code in VS Code.
  3. Create a file name “index.js” inside the folder
  4. Open terminal in VS code and execute below commands. npm init -y converts your folder into Node js project with package.json file inside it. npm install apollo-server graphql installs apollo-server and graphql dependencies.
npm init -y
npm install apollo-server graphql

After installing the npm package, the folder structure should look like this:

5. Copy the content and paste it inside index.js file. The content has been copied from official doc of Apollo Server.

const { ApolloServer, gql } = require('apollo-server');
// A schema is a collection of type definitions (hence "typeDefs")
// that together define the "shape" of queries that are executed against
const typeDefs = gql`# This "Book" type defines the queryable fields for every book in our data source.type Book {
title: String
author: String
}
# The "Query" type is special: it lists all of the available queries that# clients can execute, along with the return type for each. In this# case, the "books" query returns an array of zero or more Books (defined above).type Query {books: [Book]}`;const books = [{title: 'The Awakening',author: 'Kate Chopin',},{title: 'City of Glass',author: 'Paul Auster',},];// Resolvers define the technique for fetching the types defined in the
// schema. This resolver retrieves books from the "books" array above.
const resolvers = {
Query: {
books: () => books,
},
};
// The ApolloServer constructor requires two parameters: your schema
// definition and your set of resolvers.
const server = new ApolloServer({ typeDefs, resolvers });// The `listen` method launches a web server.server.listen().then(({ url }) => {console.log(`🚀 Server ready at ${url}`);});

6. Now we are good to go. To run start and run the apollo server, we need to execute this command in the terminal.

node index.js

Following output should be visible:

🚀 Server ready at http://localhost:4000/

Congrats, the server is running now.

Learner. Observer. Interested in software development.