DEV Community

Cover image for How to build a GraphQL server with Nodejs and Express
Martin Wangondu
Martin Wangondu

Posted on • Edited on

How to build a GraphQL server with Nodejs and Express


Being able to work with APIs and write queries is critical to any developer today. As more developers and teams opt to use GraphQL when working with APIs.

In this article, we shall learn how to build a GraphQL server with Nodejs and Express and write a query to check its working. The code in this project is on Github.

Some of the advantages of GraphQL are:-

  • Responses are decided by the client rather than the server itself. This returns exactly what the client needs.

  • Facilitates the cut down of your request by allowing you to send specific requests making it to be fast.

  • Each level relates to a specific type where each type describes a bunch of available fields. Allows descriptive error messages prior to executing a query.

How about we begin

Let's start by creating an empty folder.

mkdir graphqlserver
Enter fullscreen mode Exit fullscreen mode

then

cd graphqlserver
Enter fullscreen mode Exit fullscreen mode

Change directory to graphqlserver and initiate the folder by the following npm code

npm init
Enter fullscreen mode Exit fullscreen mode

Create a new file app.js and finally install the following packages express, graphql, and express-graphql

touch app.js

npm i express graphql express-graphql
Enter fullscreen mode Exit fullscreen mode

The project is now set up and it's time to add our code to app.js file.

require('dotenv').config();
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');

// GraphQL schema
const schema = buildSchema(`
  type Query {
      message: String
  }`);

//Root resolver
const root = {
  message: () => 'Welcome to GraphQL server with Nodejs and Express',
};

// Create an express server and a GraphQL endpoint

const app = express();

const Port = process.env.Port || 8080;

app.use(
  '/graphql',
  graphqlHTTP({
    schema: schema,
    rootValue: root,
    graphiql: true,
  })
);

app.listen(Port, (req, res) => {
  console.log(`Graphql Express Server is up on localhost:${Port}/graphql`);
});
Enter fullscreen mode Exit fullscreen mode

At first, is to make sure all dependencies needed are imported express, express-graphql, and buildSchema function from graphql. Next is to create an express server with express instance stored in the app variable.

We create a GraphQL schema that is describing the API system. The API system includes data and how a client can access that data. Each time the API is called it is validated against the schema. It's only executed when the action proves valid else an error is returned.

Finally app.listen is called to start the server process on port 8000 as included on the .env file else on port 8080.

The Node server is ready and can be started using the below code

node start

or

node app.js

If you don't need to keep restating your app nodemon will watch for any changes in the file, make sure is installed as a global dependency.

nodemon

Graphql Express Server is up on localhost:8000/graphql

Image description

Top comments (0)