[Note]GraphQL – Quick Guide (tutorialspoint.com) In this chapter, we’ll learn about GraphQL’s environment Settings. To execute the examples in this tutorial, you will need the following:

  • A computer running Linux, macOS, or Windows.
  • A web browser, preferably the latest version of Google Chrome.
  • The latest version of Node.js is installed. You are advised to use the latest LTS version.
  • Visual Studio Code is installed for VSCode’s extension GraphQL or any Code editor you choose.

How to build GraphQL server using Nodejs

We’ll go through the steps to build the GraphQL server using Nodejs, as follows:

Step 1 – Verify the node and Npm version

After installing NodeJs, run the following command on the terminal to verify the version of Node and NPM:

C:\Users\Admin>node -v v8.11.3 C:\Users\Admin> NPM -v 5.6.0Copy the code

Step 2 – create the project folder and open the root folder of the project in VSCode, which can be named test-app.

Follow these instructions to open the folder using the Visual Studio code editor:

C:\Users\Admin>mkdir test-app
C:\Users\Admin>cd test-app
C:\Users\Admin\test-app>code.
Copy the code

Step 3 – Create package.json and install dependencies

Create a package.json file that will contain all the dependencies of the GraphQL server application.

{
   "name": "hello-world-server"."private": true."scripts": {
      "start": "nodemon --ignore data/ server.js"
   },
   "dependencies": {
      "apollo-server-express": "^ 1.4.0." "."body-parser": "^ 1.18.3"."cors": "^ 2.8.4"."express": "^ 4.16.3"."graphql": "^ 0.13.2"."graphql-tools": "^ 3.1.1." "
   },
   "devDependencies": {
      "nodemon": "1.17.1"}}Copy the code

Install the dependencies using the commands given below:

C:\Users\Admin\test-app>npm install
Copy the code

Step 4 – Create the flat file database in the data folder

In this step, we use flat files to store and retrieve data. Create folder data and add two files student.json and colleges.json. Here is the colleges.json file:

[{"id": "col-101"."name": "AMU"."location": "Uttar Pradesh"."rating":5.0
    },
    
    {
       "id": "col-102"."name": "CUSAT"."location": "Kerala"."rating":4.5}]Copy the code

Here is the student.json file:

[{"id": "S1001"."firstName":"Mohtashim"."lastName":"Mohammad"."email": "[email protected]"."password": "pass123"."collegeId": "col-102"
    },
    
    {
       "id": "S1002"."email": "[email protected]"."firstName":"Kannan"."lastName":"Sudhakaran"."password": "pass123"."collegeId": "col-101"
    },
    
    {
       "id": "S1003"."email": "[email protected]"."firstName":"Kiran"."lastName":"Panigrahi"."password": "pass123"."collegeId": "col-101"}]Copy the code

Step 5 – Create a data access layer

We need to create a data store that loads the contents of the data folder. In this case, we need set variables, students, and universities. Whenever an application needs data, it uses these collection variables. Create the file db.js in the project folder as follows:

const { DataStore } = require('notarealdb');

const store = new DataStore('./data');

module.exports = {
   students:store.collection('students'),
   colleges:store.collection('colleges')};Copy the code

Step 6 – Create the schema file, schema.graphQL

Create a pattern file in the current project folder and add the following:

type Query  {
   test: String
}
Copy the code

Step 7 – Create the parser file, resolvers.js

Create a parser file in the current project folder and add the following:

const Query = {
    test: () = > 'Test Success, GraphQL server is up & running !! '
 }
 module.exports = {Query}
Copy the code

Step 8 – Create server.js and configure GraphQL

Create the server file and configure the GraphQL as follows:

const bodyParser = require('body-parser');
const cors = require('cors');
const express = require('express');
const db = require('./db');

const port = process.env.PORT || 9000;
const app = express();

const fs = require('fs')
const typeDefs = fs.readFileSync('./schema.graphql', {encoding:'utf-8'})
const resolvers = require('./resolvers')

const {makeExecutableSchema} = require('graphql-tools')
const schema = makeExecutableSchema({typeDefs, resolvers})

app.use(cors(), bodyParser.json());

const  {graphiqlExpress,graphqlExpress} = require('apollo-server-express')
app.use('/graphql',graphqlExpress({schema}))
app.use('/graphiql',graphiqlExpress({endpointURL:'/graphql'}))

app.listen(
   port, () = > console.info(
      `Server started on port ${port}`));Copy the code

Step 9 – Run the application and test it using GraphiQL

The folder structure of the verification project test-app is as follows:

test-app /
   -->package.json
   -->db.js
   -->data
      students.json
      colleges.json
   -->resolvers.js
   -->schema.graphql
   -->server.js
Copy the code

Run the command NPM start as follows:

C:\Users\Admin\test-app>npm start
Copy the code

The server runs on port 9000, so we can use the GraphiQL tool to test the application. Open a browser and enter the URL http://localhost:9000/graphiql. Enter the following query in the editor:

{
  test
}
Copy the code

The response from the server is as follows:

{
    "data": {
        "test": "Test Success, GraphQL server is running !!"}}Copy the code