Setup & Initialization for - GraphQL using Node.js

The title here is getting started with GraphQL so this is just a primer for using GraphQL in Node.js in the cloud. I first created an Ubuntu cloud server, installed Node.js and GraphQL. Then I was ready to load up a server file and fire up a node.

For this introductory example I am going with a classic "Hello World!" program which just shows how to setup and initialize a basic API request. GraphQL does get more interesting and I will show more complex queries in later posts. The goal here is just to show how to get up and running with querying API's using GraphQL.

First I created a new directory using the CLI(command line interface) that I am connecting to via SSH from the terminal on my home work machine. Once I went through all the server setup and installations it was time to test out the system. For this I created a server.js file and added the contents seen below. Here a schema is defined with a query that will return a string with the value stored in  '{ hello }' within var root. Then I pass the response and log it to the console in the terminal.


To clarify a bit further, the code above is doing a few things. A schema is built to define the type of query being called from the API endpoint. Then our response is returned as a string that is saved in a variable that can be queried with GraphQL. This is a very simple server starting point but it shows how to get up and running with GraphQL. 

And the initial response from the first GraphQL query below confirms that string of data was successfully returned from my query.

Hello World! from GraphQL using Node.js



Comments