Tech Hub

API Service Using GraphQL with Node and Express

October 29, 2021

Posted by: Ankit Kanojia

API Service Using GraphQL with Node and Express

GraphQL is a query language (hence the “QL) that describes exactly how a client should request information through an application programming interface called API. It is a syntax that developers can use to ask for specific data structures from multiple resources. Once a client defines the data structure in the request as needed, exactly the same structured JSON data will return from the server. Before being publicly released in 2015, GraphQL was developed internally by Facebook in 2012. 



Compatible with all popular coding languages like C#, PHP, Python, Ruby, JavaScript, etc. GraphQL is to provide developers with a comprehensive view of structured data from an API, that provides an ability to only receive relevant data and an architecture which makes APIs easier to scale with very little over time. 


What is GraphQL Playground? 


It is an interactive, graphical, IDE in the browser for GraphQL which is created by Prisma based on GraphQL. In development mode, GraphQL Playground is enabled on the same URL by Apollo Server (e.g. http://localhost:4000/graphql). Best effective tool or say IDE in-browser while can be used to test if your API is working or not. 


In this article, we will learn how to write a simple Hello World GraphQL API using Node and Express. 



Getting Started with Simple Hello world implement 


Step 1: Configure project and install all dependencies 


  1. mkdir graphql-express-demo   

  1. cd graphql-express-demo   

  1. npm init -y   

  1. npm install express  express-graphql  graphql  graphql-tag  cors 


Step 2: Create index.js file and write the below codes in that file and make it as a main entry point 


  1. const express = require('express')   

  1. const cors = require('cors')   

  1. const graphqlHTTP 

    Share this