@benzene/http
TypeScript icon, indicating that this package has built-in type declarations

0.4.2 • Public • Published

@benzene/http

npm CI codecov PRs Welcome

Fast and simple framework-agnostic GraphQL HTTP Server

Installation

npm i graphql @benzene/http

Usage

import { createServer } from "http";
import { Benzene, makeHandler, parseGraphQLBody } from "@benzene/http";

function readBody(request) {
  return new Promise((resolve) => {
    let body = "";
    request.on("data", (chunk) => (body += chunk));
    request.on("end", () => resolve(body));
  });
}

const GQL = new Benzene({ schema });

const graphqlHTTP = makeHandler(GQL, options);

createServer(async (req, res) => {
  const rawBody = await readBody(req);
  const result = await graphqlHTTP({
    method: req.method,
    headers: req.headers,
    body: parseGraphQLBody(rawBody, req.headers["content-type"]),
  });
  res.writeHead(result.status, result.headers);
  res.end(JSON.stringify(result.payload));
}).listen(3000);

Documentation

Documentation is available at benzene.vercel.app. Check out examples for integrations with different server libraries and tools.

Package Sidebar

Install

npm i @benzene/http

Weekly Downloads

3,881

Version

0.4.2

License

MIT

Unpacked Size

13.8 kB

Total Files

8

Last publish

Collaborators

  • hoangvvo