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

0.0.16 • Public • Published

@graphql-sse/client

A client for GraphQL subscription using Server Sent Events.

Installation

npm install @graphql-sse/client

Usage

Basic Example

import {
  SubscriptionClient,
  SubscriptionClientOptions,
} from '@graphql-sse/client';

const subscriptionClient = SubscriptionClient.create({
    graphQlSubscriptionUrl: 'http://some.host/graphl/subscriptions'
});

subscriptionClient.subscribe(
    operation
)    

Apollo client example

In the example we will create a Apollo Client link that we we later use when initializing our apollo client.

import { ApolloLink, Operation, FetchResult, Observable } from '@apollo/client';
import {
  SubscriptionClient,
  SubscriptionClientOptions,
} from '@graphql-sse/client';

export class ServerSentEventsLink extends ApolloLink {
  private subscriptionClient: SubscriptionClient;
  constructor(
    options: SubscriptionClientOptions
  ) {
    super();
    this.subscriptionClient = SubscriptionClient.create(options);
  }

  public request(operation: Operation): Observable<FetchResult> | null {
    return this.subscriptionClient.subscribe(
      operation
    ) as Observable<FetchResult>;
  }
}

Now we can use the created Apollo Client Link for subscriptions.

import { split, HttpLink, ApolloClient, InMemoryCache } from '@apollo/client';
import { getMainDefinition } from '@apollo/client/utilities';
import { ServerSentEventsLink } from './server-sent-events-link';

const httpLink = new HttpLink({
  uri: 'http://localhost:4000/graphql',
});

const sseLink = new ServerSentEventsLink({
  graphQlSubscriptionUrl: 'http://localhost:4000/graphql',
});

const splitLink = split(
  ({ query }) => {
    const definition = getMainDefinition(query);
    return (
      definition.kind === 'OperationDefinition' &&
      definition.operation === 'subscription'
    );
  },
  sseLink,
  httpLink
);

export const client = new ApolloClient({
  link: splitLink,
  cache: new InMemoryCache(),
});

Note: Alternatively, you can use the @graphql-sse/apollo-client that wraps this @graphql-sse/client. See the Example React App

Package Sidebar

Install

npm i @graphql-sse/client

Weekly Downloads

98

Version

0.0.16

License

MIT

Unpacked Size

12.3 kB

Total Files

10

Last publish

Collaborators

  • faboulaws