GraphQL JavaScript Library (Node.js): graphql-http
GraphQL is a query language for APIs developed by Facebook that allows clients to request only the data they need. In the Node.js ecosystem, one of the key libraries for implementing GraphQL servers is graphql-http. It is a lightweight yet powerful tool that enables you to build high-performance APIs with minimal overhead. Unlike heavier solutions such as Apollo Server, graphql-http offers flexibility and full control over HTTP handlers.
What is graphql-http?
graphql-http is an official library from the GraphQL community that provides a simple way to connect GraphQL to any Node.js HTTP server. It is framework-agnostic (Express, Koa, Fastify, etc.), making it a universal choice. The library's main task is to take an incoming HTTP request, parse it, execute the GraphQL query, and return a structured response.
Installation and Setup
To get started, install the library via npm:
npm install graphql-http graphqlA minimal server example using plain Node.js (without Express):
const http = require('http');const { createHandler } = require('graphql-http');const { buildSchema } = require('graphql');
// Define the schemaconst schema = buildSchema(` type Query { hello: String user(id: Int!): User } type User { id: Int name: String email: String }`);
// Resolversconst rootValue = { hello: () => 'Hello, world!', user: ({ id }) => ({ id, name: `User ${id}`, email: `user${id}@example.com` })};
// Create the handlerconst handler = createHandler({ schema, rootValue });
// HTTP serverconst server = http.createServer((req, res) => { if (req.url === '/graphql') { handler(req, res); } else { res.writeHead(404); res.end('Not Found'); }});
server.listen(4000, () => { console.log('Server running at http://localhost:4000/graphql');});Integration with Express
Most developers use Express. Integrating graphql-http with Express is straightforward:
const express = require('express');const { createHandler } = require('graphql-http');const { buildSchema } = require('graphql');
const schema = buildSchema(` type Query { books: [Book] } type Book { title: String author: String }`);
const books = [ { title: 'War and Peace', author: 'Leo Tolstoy' }, { title: 'Crime and Punishment', author: 'Fyodor Dostoevsky' }];
const rootValue = { books: () => books};
const app = express();
app.all('/graphql', createHandler({ schema, rootValue }));
app.listen(3000, () => { console.log('Express + GraphQL at http://localhost:3000/graphql');});Key Features and Benefits
- Routing flexibility: you decide which endpoint GraphQL will be available on.
- Support for all HTTP methods: GET and POST requests are handled correctly.
- Built-in validation: the library automatically checks requests against the schema.
- Performance: minimal memory consumption and high processing speed.
- Extensibility: you can add middleware for authentication, logging, and caching.
Working with Mutations and Subscriptions
In addition to queries, graphql-http supports mutations for modifying data. Example schema with a mutation:
const schema = buildSchema(` type Query { message: String } type Mutation { setMessage(newMessage: String!): String }`);
let message = 'Sta Also in library
Log4j 2 / SLF4J for Java: Complete Guide with Examples
Protocol Buffers for C++: Complete Guide with Examples
Generating PDF Reports in JavaScript (Node.js): Library Overview and Code Examples