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 = require('express-graphql')   

  1. const gql = require('graphql-tag')   

  1. const { buildASTSchema } = require('graphql')   

  1.    

  1. const app = express()   

  1. app.use(cors())   

  1.    

  1. const schema = buildASTSchema(gql`   

  1.   type Query {   

  1.     hello: String   

  1.   }   

  1. `)   

  1.    

  1. const rootValue = {   

  1.   hello: () => 'Hello, world'   

  1. }   

  1.    

  1. app.use('/graphql', graphqlHTTP({ schema, rootValue }))   

  1.    

  1. const port = process.env.PORT || 4000   

  1. app.listen(port)   

  1. console.log(`Running a GraphQL API server at localhost:${port}/graphql`)  

 
 
Step 3: Run index.js or node project by node index.js command in command-line 

 

After running the command, your application will be hosted on localhost with 4000 ports. Currently, nothing is implemented in the project so nothing will happen by opening http://localhost:4000  link, rather this is the GraphQL API you can need to open GraphQL Playground, and then enter http://localhost:4000/graphql in that page. 

 

 

 

The GraphQL Playground will help with your schema and test out queries. GraphQL playground also creates documentation with API information automatically. 

 

 

  

Now try querying for Hello, using Query 

  1. query {   

  1.   hello   

  1. }  

  1.  

 

 

 

 

 

Summary 

  

Here we've learned about GraphQL API integration in NodeJS & Express. This is just a simple sample Hello World example developed for beginner levels to understand. In my next article we will learn GraphQL APIs with lists, create, update, and delete operations being performed.  

Share this


Back

Posts by Category