@graphql-sse/server
TypeScript icon, indicating that this package has built-in type declarations

0.0.13 • Public • Published

@graphql-sse/server

This package provide utility function for implementing GraphQL Subscriptions transport over Server Send Events. This package is inspired by the awesome GraphQL Helix.

Installation

npm install @graphql-sse/server

Basic usage

Here is a simple example of how to use it with express. Alternatively you can use the @graphql-sse/express package.

import express, { RequestHandler } from "express";
import {
  getGraphQLParameters,
  processSubscription,
} from "@graphql-sse/server";
import { schema } from "./schema";

const app = express();

app.use(express.json());

app.post(path, async (req, res, next) => {
    const request = {
        body: req.body,
        headers: req.headers,
        method: req.method,
        query: req.query,
    };

    const { operationName, query, variables } = getGraphQLParameters(request);
    if (!query) {
        return next();
    }
    const result = await processSubscription({
        operationName,
        query,
        variables,
        request: req,
        schema,
    });

    if (result.type === RESULT_TYPE.NOT_SUBSCRIPTION) {
        return next();
    } else if (result.type === RESULT_TYPE.ERROR) {
        result.headers.forEach(({ name, value }) => res.setHeader(name, value));
        res.status(result.status);
        res.json(result.payload);
    } else if (result.type === RESULT_TYPE.EVENT_STREAM) {
        res.writeHead(200, {
            'Content-Type': 'text/event-stream',
            Connection: 'keep-alive',
            'Cache-Control': 'no-cache',
        });

        result.subscribe((data) => {
            res.write(`data: ${JSON.stringify(data)}\n\n`);
        });

        req.on('close', () => {
            result.unsubscribe();
        });
    }
});

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.0.13
    79
    • latest

Version History

Package Sidebar

Install

npm i @graphql-sse/server

Weekly Downloads

79

Version

0.0.13

License

MIT

Unpacked Size

19.5 kB

Total Files

16

Last publish

Collaborators

  • faboulaws