Apollo Server Express GraphQL API using Node.js with TypeScript

In this article, we are going to see how to build a Nodejs GraphQL API with TypeScript using Apollo Server Express.

I conducted a workshop for beginners who want to write GraphQL APIs using Typescript and Nodejs.

If it feels boring reading lots of words one by one and you can understand Hindi a bit. You can watch video tutorials for this.
Video tutorials are available on my YouTube Channel as Playlsit.

Let's understand a few terminologies before actually diving into the code.


  • Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast and scalable network applications.

Apollo Server

You can use Apollo Server as:

  • A stand-alone GraphQL server, including in a serverless environment

Apollo Server provides:

  • Straightforward setup, so your client developers can start fetching data quickly


Express is a routing and middleware web framework that has minimal functionality of its own: An Express application is essentially a series of middleware function calls.

Read more about express here


  • TypeScript is JavaScript that scales.

Why use TypeScript over JavaScript?

If an experienced developer is working on relatively small coding projects, then JavaScript is ideal. However, if you have the knowledge and expertise development team, then Typescript is the most preferred option. Typescript has the following advantages over Javascript.

  • The powerful type system, including generics & JS features

Let's write code for what we are here….

Creating a directory and working on

mkdir apollo-server-demo-projectcd apollo-server-demo-project

Setting up the npm project

npm init — yes

This command will create a package.json file in the project.

Install and initializing TypeScript for our project

Add TypeScript to our project’s npm devDependencies

npm install — save-dev typescript

Generate a tsconfig.json file using npx.

npx tsc --init --rootDir src --outDir dist --lib dom,es6 --module commonjs –removeComments

Running above command will create tsconfig.json file in the project and it will look like this.

We will also need nodemon to compile our code on change,

and ts-node to exec TypeScript files.

Let's install these 2 dependencies as well now.

npm i nodemon ts-node --save-dev

Writing actual code now

Create a/src directory with a server.ts file and print hello world from server.ts file

console.log(“Hello World”);

Run below command to see the result on your terminal of project.

nodemon 'src/server.ts' --exec 'ts-node' src/server.ts

You will see Hello World printed on your terminal as below.

Lets now create an actual GraphQL API

Setting up Apollo-Server-Express

Installing dependencies for apollo-server-express, cors, express and graphql

npm install apollo-server-express cors express graphql

Installing dependencies for @types/express, @types/node, graphql-tools and graphql-import-node

npm install @types/express @types/node graphql-tools graphql-import-node --save-dev

Creating a simple schema and resolver

In the /src folder, create a /schema folder and create a file schema.graphql

Then let’s quickly create a sresolver.ts file in the /src folder

Create a schema file in the /src folder that will be in charge of making an Executable GraphQL Schema:

Finally creating apollo-server

Run your server again open http://localhost:8000/graphql on browsers of any other graphql client software.

Let's test your helloWolrd query from the client

Add scripts in your package.json file as below for running commands in an easier way from next time.

"scripts": {"start": "node 'dist/server.js'","build": "tsc -p . && ncp src/schema dist/schema","start:dev": "npm run build:dev","build:dev": "nodemon 'src/server.ts' --exec 'ts-node' src/server.ts -e ts,graphql"},
  • Running npm run build:dev in your terminal will compile your code. You can run the compiled code with npm start:dev

Lets now create a Mutation GraphQL API

Add mutation object in src/schem/schema.graphql file

Add some logic for resolving mutation in src/resolvers.ts file as shown below

Run the command and test mutation from client

Happy learning and happy coding !!!

The code for this workshop is available as open source on GitHub repo.

I have tried to share some of the lessons that I have learned from various sources. I will continue to keep learning more and sharing more. I hope it helps someone and makes their life a bit easier.

If you like this article, do give it a thumbs up, please don’t forget to click 👏👏👏 , comment on it, and share it with your friends.

Also, to be notified about my new articles and stories, follow me on Medium, Github, and Twitter. You can find me on LinkedIn as well. I am quite active on Dev Community as well and write small topics over there.