Running Apollo Server

  • 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
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}`);});
node index.js
🚀 Server ready at http://localhost:4000/

--

--

--

Learner. Observer. Interested in software development.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

JavaScript: Tracking Key Value Pairs Using Hashmaps

D3 and React — similarities and differences

A Byte of Advice for New Programmers

Eight drill bits sitting in a drill bit container

Why is my z-index not working? Let’s fix it

Angular Component Libraries You Should Know In 2020 to Build Cool Web Solutions

Using props.children With React Function Components

Make Your Life Easy With Dependency Injection in Your TypeScript Projects

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akanksha Pardeshi

Akanksha Pardeshi

Learner. Observer. Interested in software development.

More from Medium

Implement Enterprise Recaptcha In Next JS (with backend)

Car Wash Pattern: Parallelizing Non-Thread Safe and/or CPU-intensive Processes with Future Based…

Apollo Federation v2 by example

Role Based Access Control (RBAC) with firebase