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

0.0.15 • Public • Published

@graphql-see/express

This package provides a GraphQL subscription server over Express.

Installation

npm install @graphql-sse/express

Basic usage

Express Example

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

const app = express();

app.use(express.json());
const subscriptionServer = SubscriptionServer.create({
    schema,
    app   
});

Apollo Server example

The snippet below is a sample from the Apollo Server Example App.

import { createServer } from 'http';
import { SubscriptionServer } from '@graphql-sse/server';
import { makeExecutableSchema } from '@graphql-tools/schema';
import * as express from 'express';
import { ApolloServer } from 'apollo-server-express';
import { typeDefs, resolvers } from './graphql-api';
import { ApolloServerPluginLandingPageGraphQLPlayground } from 'apollo-server-core';

export async function startApp() {
  const app = express();

  const httpServer = createServer(app);

  const schema = makeExecutableSchema({
    typeDefs,
    resolvers,
  });

  const subscriptionServer = SubscriptionServer.create({
    schema,
    app   
  });

  const server = new ApolloServer({
    schema,
    introspection: true,
    plugins: [
      {
        async serverWillStart() {
          return {
            async drainServer() {
              subscriptionServer.close();
            },
          };
        },
      },
    ],
  });
  await server.start();
  server.applyMiddleware({ app });

  const PORT = 4000;
  httpServer.listen(PORT, () =>
    console.log(`Server is now running on http://localhost:${PORT}/graphql`)
  );
}

Package Sidebar

Install

npm i @graphql-sse/express

Weekly Downloads

1

Version

0.0.15

License

MIT

Unpacked Size

17 kB

Total Files

13

Last publish

Collaborators

  • faboulaws