@via-profit-services/core
TypeScript icon, indicating that this package has built-in type declarations

2.4.1 • Public • Published

Via Profit Services / Core

via-profit-services-cover

Via Profit services / Core - GraphQL server

Documentation here

The MIT License.

Peer dependencies

  • GraphQL - The JavaScript reference implementation for GraphQL
  • Busboy - A streaming parser for HTML form data for Node. Used for the files upload

Installation

$ npm install busboy graphql @via-profit-services/core

Usage

import http from 'node:http';
import { Readable } from 'node:stream';
import { graphqlHTTPFactory } from '@via-profit-services/core';

import schema from './my-schema';

// Create the simple NodeJS HTTP server
const server = http.createServer();

// Create The graphqlHTTP listener
const graphqlHTTP = graphqlHTTPFactory({
  schema,
  debug: true, // For display geaphql extensions response
});

// Now you can use graphqlHTTP in your http request
server.on("request", async (req, res) => {
  // Allow only POST/GET request on `/graphql`
  if (
    ["POST", "GET"].includes(req.method) &&
    req.url.match(/^\/graphql(\?|$)/)
  ) {
    const { data, errors, extensions } = await graphqlHTTP(req, res);
    const response = JSON.stringify({ data, errors, extensions });
    const stream = Readable.from([response]);

    res.statusCode = 200;
    res.setHeader("content-type", "application/json");

    stream.pipe(res);
  }
});

server.listen(8080, "localhost", () => {
  console.debug("started at http://localhost:8080/graphql");
});

Documentation here

Readme

Keywords

Package Sidebar

Install

npm i @via-profit-services/core

Weekly Downloads

6

Version

2.4.1

License

MIT

Unpacked Size

484 kB

Total Files

8

Last publish

Collaborators

  • vasily-novosad
  • pashtet422