Skip to content

Coherent, zero-dependency, lazy, simple, GraphQL over WebSocket Protocol compliant server and client.

License

Notifications You must be signed in to change notification settings

ilijaNL/graphql-transport-ws

 
 

Repository files navigation

Graphql-Ws-gqless

Getting started

Install

yarn add @ilijanl/graphql-ws-graphqless

A fork of graphql-ws. This package is meant to be used for custom executions based on graphql-transport-ws protocol. If used for graphql, go with original graphql-ws package.

All credits go to @enisdenjo

Create own subscription server

import { WebSocketServer } from 'ws'; // yarn add ws
// import ws from 'ws'; yarn add ws@7
// const WebSocketServer = ws.Server;
import { useServer } from '@ilijanl/graphql-ws-graphqless/lib/use/ws';
import { schema } from './previous-step';

const server = new WebSocketServer({
  port: 4000,
  path: '/path',
});

useServer(
  {
    createSubscription: () => {
      const iter = (async function* () {
        for (const hi of ['Hi', 'Bonjour', 'Hola', 'Ciao', 'Zdravo']) {
          yield { greetings: hi };
        }
      })();

      return {
        start: async function (emit) {
          for await (const l of iter) {
            await emit({ data: { greetings: l } });
          }
        },
        stop: () => {
          iter.return(undefined);
        },
      };
    },
  },
  server,
);

console.log('Listening to port 4000');

About

Coherent, zero-dependency, lazy, simple, GraphQL over WebSocket Protocol compliant server and client.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 95.4%
  • JavaScript 4.6%