Part 2: Building an API with GraphQL, Node and Express

2 min read ⋅ 544 views

This article is part 2 of 2 in a series. Make sure that you're familiar with 
Part 1: Building an API with GraphQL, Node and Express before continuing.

In the second part of this series, we'll take a deeper look to make our application more of a production-grade implementation.

We'll now look at covering the following topics:

  • GraphQL Schema
  • Data Validation
  • Integrate with Express

GraphQL Schema

If you have gone through the last article, then you can see there are few obvious things which may raise few questions, like:

Why did we create a separate type instead of using the regular JSON object?

Why do we have two separate head for Query and Mutation?

Simply put, GraphQL provides a language-neutral way to organise your data structures. We define the type, queries and mutations in a rather generic way and their implementation is followed using the language of our choice. In the current case, it is done using JavaScript. The basic structure of GraphQL schema is like this:

schema {

query: Query

mutation: Mutation

}

Where query is mandatory, but mutation is optional to define. We can define multiple types, and also reference them from one another.

With above code example, if I query the blogs I would get the following below. It returns the blog model, referencing owner model as well.

Now the obvious thing comes to mind is that what if blogs returns multiple objects with the same owner, in which case we will be getting duplicate data. Without GraphQL we would be either re-structuring our APIs to reduce the duplicates or may make multiple API calls to get the desired information.

But with GraphQL, I can just change the query structure a bit and it would remove the duplicity.

Data Validations

Because GraphQL works more like a typed language because when we interact with it, it instantly invalidates the wrong data provided in the request.

So the request must match with the query set on the backend. If there are any violations, GraphQL raises the error and as a developer, we do not need to validate the request data structure once the types and query/mutation is defined.

Integrate with Express

While the simple example contains only GraphQL specific configuration, but all the other configurations like CORS, Middleware, JWT with Express remains same and there is not much change, which makes it pretty quick and easy to integrate.

GraphQL has much more to offer than I have mentioned in this short tutorial. Here are few resources to get into more in-depth.

Out of the many services built on top of GraphQL, I really like the look of GraphCool and I will be playing around that very soon.

We are working on another serverless web app, Timeline (still work in progress), which is using AWS AppSync with AWS Amplify, which is like a full-stack to build web/mobile applications and it uses GraphQL as a data provider by default. Although it is tightly integrated with other AWS services, it's a pretty neat way to build applications on AWS.

I think GraphQL is great for certain types of applications, and it definitely makes a difference where the API is being used by a variety of clients and with varying data need. It gives you the freedom to structure the data as per the business logic and let the clients decide what data to ask for.



JavaScript Node.js GraphQL



MORE ARTICLES

Part 1: Building an API with GraphQL, Node and Express

We have been working on a mid-size application primarily using Express as an API Backend, and planning to migrate a port...

1233 views

Part 1 - Building a serverless web app with AWS Amplify and Angular

In this article, we will be using the recently launched AWS Amplify service to build a serverless web application. At it...

383 views

JAMStack - JavaScript, APIs and Markup

Last week I came across this interesting term, JAMStack, while working on a prototype using Gatsby, NextJS and Netlify, ...

75 views