Running GraphQL Server Using Node.JS & Express on Linux Hosting Server

  • 2019-02-10 02:17 AM
  • 120

The simplest way to run an GraphQL API server is to use Express, a popular web application framework for Node.js.

Introduction

Here I am going to show you how to setup GraphQL server and run it from a Linux hosting server. Connect your Linux server using SSH Shell command. As I am using an A2Hosting server, I am going to use this SSH command in my terminal windows.

Prerequisites

Before starting, make sure you have Node.JS and NPM is installed on your server environment. If you haven’t installed on your server, you check below link as well.

Once you have Node.JSand NPM installed on your server, let’s get started with SSH command in your terminal. Keep your SSH credentials in hand. Open your terminal window and type this command.

ssh -p port [email protected]

It will ask your server password, and allow you to enter your server path.

Install Express and GraphQL

Install Express and GraphQL on your server using this command.

npm install express express-graphql graphql --save

I have installed GraphQL in my subdomain services.zeptobook.com.

Create Server File

Let’s create graphserver.js file and upload it to the your server folder.

var express = require('express');
var graphqlHTTP = require('express-graphql');
var { buildSchema } = require('graphql');

// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
  type Query {
    hello: String
  }
`);

// The root provides a resolver function for each API endpoint
var root = {
  hello: () => {
    return 'Hello world!';
  },
};

var app = express();
app.use('/graphql', graphqlHTTP({
  schema: schema,
  rootValue: root,
  graphiql: true,
}));
app.listen(49500);
console.log('Running a GraphQL API server at serverurl/graphql');

In order to handle GraphQL queries, we need to first create a schema that has a Query type, and we need an API root with a function called a resolver for each API endpoint. So, create this server file and deploy it on your server folder using either an FTP tool or cPanel window.

Run Your GraphQL Server

Now, once you deploy your server file on the server, run this command on your server using ssh command.

node graphserver.js

It will show this console message.

Running a GraphQL API server at serverurl/graphql

Running GraphQL Server Using Node.JS & Express on Linux Hosting Server

Now, we are all set. It is time to test our GraphQL server. Run your server service url in the browser like this:

Running GraphQL Server Using Node.JS & Express on Linux Hosting Server

Fire Query

Now, our GraphQL server is up, let’s query our data using this command in the left side of the window, and click on the Run arrow button.

{
   hello
}

The output will be in the right side of the window.

{
   "data": {
     "hello": "Hello world!"
   }
}

Running GraphQL Server Using Node.JS & Express on Linux Hosting Server

Using Chrome POSTMAN

Running GraphQL Server Using Node.JS & Express on Linux Hosting Server

Using Curl Command

curl -X POST \
-H "Content-Type: application/json" \
-d '{"query": "{ hello }"}' \
services.zeptobook.com:49500/graphql

Congratulations — Your GraphQL server is up.

Summary

So, in this post, we see how to setup GraphQL on our Linux hosting server. I will create posts on GraphQL in the upcoming month.

Learn More

The Complete Node.js Developer Course (2nd Edition)

Learn and Understand NodeJS

Node JS: Advanced Concepts

GraphQL: Learning GraphQL with Node.Js

Angular (Angular 2+) & NodeJS - The MEAN Stack Guide

GraphQL with React: The Complete Developers Guide

GraphQL with Angular & Apollo - The Full-stack Guide

GraphQL: Learning GraphQL with Node.Js

Complete guide to building a GraphQL API

GraphQL: Introduction to GraphQL for beginners

Original source: https://www.zeptobook.com

Suggest