API Service Using GraphQL With Node And Express

 

Introduction

 
GraphQL is a query language(“QL” stands for) that describes how exactly a client should requested information thorough 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 interactive, graphical, IDE in browser for GraphQL which is created by Prisma based on GraphQL. In development mode, GraphQL Playground enables 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 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  
  2. cd graphql-express-demo  
  3. npm init -y  
  4. 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')  
  2. const cors = require('cors')  
  3. const graphqlHTTP = require('express-graphql')  
  4. const gql = require('graphql-tag')  
  5. const { buildASTSchema } = require('graphql')  
  6.   
  7. const app = express()  
  8. app.use(cors())  
  9.   
  10. const schema = buildASTSchema(gql`  
  11.   type Query {  
  12.     hello: String  
  13.   }  
  14. `)  
  15.   
  16. const rootValue = {  
  17.   hello: () => 'Hello, world'  
  18. }  
  19.   
  20. app.use('/graphql', graphqlHTTP({ schema, rootValue }))  
  21.   
  22. const port = process.env.PORT || 4000  
  23. app.listen(port)  
  24. 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 port. 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 {  
  2.   hello  

 

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. 


Similar Articles