Building GraphQL Server with NodeJS

How to Build a GraphQL Server with Nodejs

Most of the applications today need to fetch data from the server where data is stored in a database. GraphQL is a new API standard and building a GraphQL server with NodeJS provides a more efficient, robust and flexible alternative to REST. It allows a client to fetch only the data it needs from a server. GraphQL is often confused with being a database technology, but this is a Misconception, GraphQL is a query language for APIs and not databases. It enables you to provides a complete and understandable description of the data in your API. Before going in-depth on building a GraphQL server with NodeJS, let’s see what REST is? And what are its disadvantages?

What is REST?

REST referees as Representational State Transfer which is just a pattern for making APIs. Using API, you can access resources like images, videos, text, etc. It is independent of platform and languages. RESTful is an example. A service built on the REST architecture is called a RESTful service. RESTful enables web applications, built on various programming languages, to communicate through data.

graphql5

What are the drawbacks of RESTful?

  • Restful API always need multiple API calls.
  • The data will get more complex; the routed get longer
  • This one of the primary concern of this is that it never allows for rapid iterations on the frontend. 

graphql1

What is GraphQL?

GrapphQL is a new API Standard and super flexible that can be implemented in a million different ways. Thus there will be no much confusion. This was invented and open-sourced by Facebook. It is an execution engine and data query language. It was used as an alternative to the existing REST standard for structuring server queries. GraphQL lets for requesting specific data that a client needs, departing from the fixed data structure approach. 

Why use GraphQL?

  • When you send a GraphQL query to your API, you will get exact Renault that you want.
  • By using GraphQL, you will always return predictable results very fastly.
  • GraphQL is a strongly typed query language. Let’s see with an example:
  • The client application or GraphQL browser application makes a request to the Apollo GraphQL server.
  • If the request schema passes validation, the server will parse and authenticate the query against a defined schema.

Graphql2

  • Then the associated resolver functions will be performed. The resolver will contain code to fetch data from an API or database. 

Build a GraphQL server with Node.js

 Apollo server is a flexible, community-driven, production-ready HTTP GraphQL. Apollo server is a library that assists you to connect a GraphQL schema to an HTTP server in Node. It works with any GrapQL schema built with GraphQL.js; with this, you can develop your schema directly or with a convenience library such as graphql-tools. Even you can use Apollo server with all accessible JavaScript HTTP servers which include Express, connect, etc. 

This server can be queried from any GraphQL client, since it supports all of the standard semantics for sending GraphQL over HTTP, as documentation on graphql.org. Apollo server also holds some small extensions to the protocol such as sending multiple GraphQL operations in one request.

Mean Stack Developer

Features of building GraphQL server using Node.js

To build GraphQL server Apollo server is the best option as it has many features such as:

  • Attach GrapQL schema to your HTTP server to server requests
  • Attach GraphQL and GraphiQL via separate middlewares on different routes
  • Accept queries via getting or POST
  • Support Apollo tracing to get performance information about the server
  • Support HTTP query batching 
  • Support Apollo cache control to inform gateways such as Apollo engine

What is the principle that Apollo server follows to build GraphQL server using Node.js?

The following principles guide the construction of Apollo Server:

  • The community drives Apollo Server development to meet the needs of developers using the library.
  • Simplicity: Keep things simple, for instance, supports a limited set of transports, makes Apollo server more comfortable to use, contribute to and more secure.
  • Apollo Server ensures performance by being well-tested and production-ready.

How to Build a GraphQL server with Nodejs

1 ) Verify Node and Npm Versions

Graphql6

2 ) Create a Project Folder and Open in VSCode

Graphql7

3 ) Create package.json and Install the Dependencies

Graphql8

4 ) Install GraphQL and Apollo Server

Graphql9

We can verify if these dependencies are installed successfully by checking the package.json file that we created previously.

Graphql10

5 ) Create Flat File Database in Data Folder

Graphql11
Following is the students.json file −

Graphql12

6 ) Create a Data Access Layer

Create file db.js within the project folder as follows −

Graphql13

7 ) Create Schema File, schema.graphql

Create a schema file in the current project folder and add the following contents −

Graphql14

8 ) Create Resolver File, resolvers.js

graphql15

9 ) Create Server.js and Configure GraphQL

Create a server file and configure GraphQL as follows −

Graphql16
Graphql18

10 ) Run the Application and Test with GraphiQL

Graphql19

Graphql20

What is JWT?

JSON Web Token(JWT) is an open standard that defines a compact and self-contained way for securing transmitting information between parties as a JSON object. 

Compact – You can send JWT via URL, Post request, or HTTP header for fast transmission.

JWT Structure

Graphql3

Graphql4

Why JWT?

JWT has a very high authorization capacity. It can effectively exchange information. 

Step 1 − Install the JWT module.

Graphql21

Step 2 − Require the JWT module.

Graphql22

Graphql23

Krify is the leading mobile and website development company having a vast knowledge of developing a website and mobile app for any domain. We even have competency in developing applications using Node.js.

For more information, please contact us and get a free quote.

Scroll to Top