Implementing GraphQL

Learn to build a GraphQL query server in Node JS. Enable dynamic/high speed API endpoints with GraphQL and Express.

To get started with GraphQl in Node Js first you will need to install the graphql.js library by the GraphQL foundation. GraphQL.js helps with building queries and allows you to build APIs with Express.


npm install graphql --save

Building Schema's

import { buildSchema }  from 'graphql';

// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
  type Query {
    hello: String

Building a schema with the javascript SDK is shown above, use the buildSchema method imported from the graphql package. Then pass a schema using the GraphQL language (more here), note that the schema passed is in a template literal.

Creating Resolvers

// The rootValue provides a resolver function for each API endpoint
var rootValue = {
  hello: () => {
    return 'Hello world!';

After the schema is created, you can create a resolver to fill in the information asked in the query. A resolver is an object of functions with the keys corresponding to the values queried. In the resolver above the parameter hello will be filled by "Hello World".

Running Queries

import { graphql } from 'graphql';
// Run the GraphQL query '{ hello }' and print out the response
  source: '{ hello }',
}).then((response) => {

Using the graphql function from the module of the same name, you are able to execute queries on the server against the schema you created. For the first argument pass in an object with the schema, the query (as the source), and the resolver. The source in the query you are trying to execute, in this case, we are asking for the resolver that is named hello from the rootValue resolver.

Running Queries with Express

Getting express running with GraphQL is done by installing the adapter express-graphql.

npm install express-graphql --save

For this example, we will use the same code above just adding in an express server along with it.

import express from "express";
import { graphqlHTTP } from 'express-graphql';

// Code from above goes here

var app = express();
app.use('/graphql', graphqlHTTP({
  schema: schema,
  rootValue: root,
  graphiql: true,
console.log('Running a GraphQL API server at http://localhost:4000/graphql');

This will start a sever on port 4000 running your GraphQL server on it. The graphqlHTTP function is passed into express as middleware so you are still able to keep any traditional REST API endpoints you have created previously. The expected response will be a JSON object with the response data inside of it like below.

            "hello": "Hello World"		

The graphiql: true argument passed in the graphqlHTTP middleware will create an interface using the GraphiQL tool that lets you experiment with your new API. When using the API make sure to disable this feature so it works properly.

If you want to add authentication using JSON web tokens you can pass in the middleware from this tutorial and your routes will be protected.



Be the first to comment!

Read More

Making sticky elements in HTML and CSS.

Sticky elements are a useful feature in modern web design, as they allow certain elements on a webpage to remain visible even when the user scrolls. This can be useful for elements such as the n...

Node JS Logo
Node JS Logo

Setting up Node.JS for production

Node.js is a popular JavaScript runtime that allows developers to build server-side applications with JavaScript. PM2 is a process manager for Node.js applications that helps to keep your applications running smoothly and automatically restart th...

Svelte + Env
Svelte + Env

Svelte Environment Varibles

Environment variables are values that can be passed to an application at runtime, that influence the behavior or configuration of the application. They are stored in key-value pairs and can be used to store sensitive information, such as passwords, API keys, or configuration settings. This information is made available to the application through the oper...

Node JS HTTP Webserver

Node JS comes out of the box with a built in HTTP module that can handle Hypertext Transfer Protocol requests. You can use this to build a web server without installing anything.