GraphQL Server With Node.js

Prachi Thacker
The Startup
Published in
4 min readOct 20, 2020

Over the past decade, REST has become the standard for designing web APIs. It offers some great ideas, such as stateless servers and structured access to resources. However, REST APIs have shown to be too inflexible to keep up with the rapidly changing requirements of the clients that access them.

BTS: Before RESTful APIs, we had RPC, SOAP, CORBA, and other less open protocols. Many pre-REST APIs required complex client libraries to serialize/deserialize the payload over the wire. A fundamental difference compared to today’s RESTful APIs, is that SOAP is strongly typed using formal contracts via WSDL (Web Services Description Language).

The RESTful architecture was introduced in 2000 as a much simpler way to enable the machine to machine communication using only the ubiquitous HTTP protocol without additional layers in a stateless and type freeway.

Now: REST uses the standard CRUD HTTP Verbs (GET, POST, PUT, DELETE) and leverages HTTP conventions and centered around data resources (HTTP URIs) rather than attempting to fight HTTP.

3rd party developers to a new API need to only reason about the data model and leave the rest to HTTP convention rather than digging deep into thousands of operations. In other words, REST is much tighter coupled to HTTP and CRUD compared to SOAP but provides loose data contracts.

  1. A client requests data from a separated server, often over a network, A Client-Server Model
  2. A client doesn’t have to be connected to the end server, a Layered system
  3. All clients and servers interact with the API in the same way, a Uniform interface
  4. The client holds the state between requests and responses, Statelessness
  5. a client can cache a server’s response, Cacheability

As more variety of APIs are placed in production use and scaled to extreme levels, certain problems in RESTful architecture transpired. You could even say GraphQL is between SOAP and REST taking pieces from each.

GraphQL: GraphQL was developed to cope with the need for more flexibility and efficiency! It solves many of the shortcomings and inefficiencies that developers experience when interacting with REST APIs.

GraphQL follows the same set of constraints as REST APIs, but it organizes data into a graph using one interface. Objects are represented by nodes (defined using the GraphQL schema), and the relationship between nodes is represented by edges in the graph. Each object is then backed by a resolver that accesses the server’s data.

For each node, an object type is defined in the GraphQL schema. GraphQL defines some object types out of the box. For example, query type and scalar types (e.g. integer, string, and boolean). But, the API developer is responsible for defining the rest of the queryable objects using the GraphQL Schema Definition Language (SDL)

Important Terms:

Query: A query is what a client sends to a server in order to specify the necessary data.

Schema: The blueprint for communication between client and server. It specifies what queries clients can make, the types of data retrievable, and relationships between types.

Resolvers: A function applied to every field. It specifies how that field is connected to the backend and fetches data for that field from a database.

  • If you have multiple clients, because they simply write their own queries in the language of their choice (GraphQL supports them all);
  • If you work on different platforms: web, mobile, apps, etc.;
  • If your API is highly customizable.

With GraphQL, there is a learning curve, which isn’t nearly as established as REST APIs, but that learning curve is worth it. When creating automation, your life can become so much easier if you receive only the data you need and nothing else. As an added bonus, the performance of your automation increases because you’re not having to process a large amount of data. At scale, any performance improvements you can include are big wins.

Node.js Express & GraphQL

Express is a fast, unopinionated, minimalist web framework for Node.js, It’s a simple framework that adds key web application features on top of Node.js. It was one of the first out there and is widely used by lots of companies that work with Node.js.

There’s a ton of modules you can add on top of it to handle most use cases, including mine here. Node.js Express is without a doubt your best bet for starting a new server with GraphQL, as the express-graphql module provides a shortcut to create one. This module will be part of the tutorial below.

Although there are other Node.js alternatives such as Koa and Strapi, I decided to go with the classic and stick to what I know best here.

  1. Setting up the project with GraphQL, create a new directory

$ mkdir gql-server

2. Change into that directory and initiate a new package.json file by executing the following NPM command:

$ npm init

3. Furthermore, create a new server.js file in the project directory. That will be the file where the code required to implement the Node.js GraphQL server will be inserted in the next section:

$ touch server.js

4. Install GraphQL dependencies:

Need to install express-graphql, an HTTP middleware that will be leveraged by Express to rapidly create your API and the official graphql implementation.

$ npm install graphql express express-graphql —save

5. Run GraphQL server with:

$ node server.js

This screenshot shows the GraphQL query { hello } being issued and giving a result of { data: { hello: 'Hello GraphQL!' } }.

References:

https://graphql.org/

--

--