# @graphql-sse/client

> GraphQL Subscription client

Latest version **0.0.16** (published 2021-10-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install @graphql-sse/client
pnpm add @graphql-sse/client
yarn add @graphql-sse/client
bun add @graphql-sse/client
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.16 |
| Published | 2021-10-10 |
| First published | 2021-09-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 12 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Lod Lawson |
| Maintainers | faboulaws |
| Keywords | Graphql Subscription Client, Server Sent Event |

## Links

- npm: https://www.npmjs.com/package/@graphql-sse/client
- npm.io page: https://npm.io/package/@graphql-sse/client

## Dependencies (1)

- [@microsoft/fetch-event-source](https://npm.io/package/@microsoft/fetch-event-source.md) ^2.0.1

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 0.0.16 (latest) — 2021-10-10
- 0.0.15 — 2021-10-10
- 0.0.13 — 2021-09-22
- 0.0.12 — 2021-09-22
- 0.0.10 — 2021-09-22
- 0.0.7 — 2021-09-22
- 0.0.5 — 2021-09-21
- 0.0.2 — 2021-09-21
- 0.0.1 — 2021-09-21

## README

# @graphql-sse/client

A client for GraphQL subscription using Server Sent Events.

## Installation

```shell
npm install @graphql-sse/client
```

## Usage

### Basic Example

```typescript
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.

```typescript
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.

```typescript
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`](https://github.com/faboulaws/graphql-sse/tree/main/libs/apollo-client) that wraps this `@graphql-sse/client`. See the  [Example React App](https://github.com/faboulaws/graphql-sse/tree/main/apps/react-app-example)

---
_Source: https://npm.io/package/@graphql-sse/client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
