🚀 DAY 5 OF LAUNCH WEEK: Introducing Socket Firewall Enterprise.Learn more →
Socket
Book a DemoInstallSign in
Socket

@enisdenjo/graphql-transport-ws

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@enisdenjo/graphql-transport-ws

A WebSocket client for GraphQL subscriptions

latest
Source
npmnpm
Version
1.1.1
Version published
Maintainers
1
Created
Source

graphql-transport-ws

đź”— A coherent, zero-dependency, lazy, simple and easy to use server and client implementation of the GraphQL over WebSocket Protocol.

Getting started

Install

yarn add @enisdenjo/graphql-transport-ws
# or
npm install @enisdenjo/graphql-transport-ws

Examples

Client usage with Relay

import { createClient } from '@enisdenjo/graphql-transport-ws';
import { Network, Observable } from 'relay-runtime';

const subscriptionsClient = createClient({
  url: 'wss://some.url/graphql',
  connectionParams: () => {
    const session = getSession();
    if (!session) {
      return null;
    }
    return {
      Authorization: `Bearer ${session.token}`,
    };
  },
});

export const network = Network.create(
  // fetch
  (operation, variables, cacheConfig) => {
    return Observable.create((sink) => {
      fetchQuery(operation, variables, cacheConfig, sink);
    });
  },
  // subscribe
  (operation, variables) => {
    return Observable.create((sink) => {
      if (!operation.text) {
        return sink.error(new Error('Operation text cannot be empty'));
      }
      return subscriptionsClient.subscribe(
        {
          operationName: operation.name,
          query: operation.text,
          variables,
        },
        sink,
      );
    });
  },
);

Client usage with Apollo

import { print } from 'graphql';
import { ApolloLink, Operation, FetchResult, Observable } from '@apollo/client';
import { createClient, Config, Client } from '@enisdenjo/graphql-transport-ws';

class WebSocketLink extends ApolloLink {
  private client: Client;

  constructor(config: Config) {
    super();
    this.client = createClient(config);
  }

  public request({
    operationName,
    query,
    variables,
  }: Operation): Observable<FetchResult> {
    return new Observable((sink) => {
      return this.client.subscribe<FetchResult>(
        { operationName, query: print(query), variables },
        sink,
      );
    });
  }
}

const link = new WebSocketLink({
  url: 'wss://some.url/graphql',
  connectionParams: () => {
    const session = getSession();
    if (!session) {
      return null;
    }
    return {
      Authorization: `Bearer ${session.token}`,
    };
  },
});

Documentation

TypeDoc generated documentation is located in the docs folder.

Protocol

Read about the exact transport protocol used by the library in the PROTOCOL.md document.

Want to help?

File a bug, contribute with code, or improve documentation? Welcome đź‘‹! Read up on our guidelines for contributing.

FAQs

Package last updated on 28 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts