# @pixwell/graphql-eventstore-subscriptions

> _graphql-eventstore-subscriptions_ implements the `PubSubEngine` interface from the [graphql-subscriptions](https://github.com/apollographql/graphql-subscriptions) package.

Latest version **1.1.3** (published 2020-10-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install @pixwell/graphql-eventstore-subscriptions
pnpm add @pixwell/graphql-eventstore-subscriptions
yarn add @pixwell/graphql-eventstore-subscriptions
bun add @pixwell/graphql-eventstore-subscriptions
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.3 |
| Published | 2020-10-01 |
| First published | 2020-08-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 36.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Pixwell |
| Maintainers | jozefbalun, danieljaniga |
| Keywords | graphql, eventstore, apollo, subscriptions |

## Links

- npm: https://www.npmjs.com/package/@pixwell/graphql-eventstore-subscriptions
- Repository: https://github.com/pixwell-dev/graphql-eventstore-subscriptions
- Issues: https://github.com/pixwell-dev/graphql-eventstore-subscriptions/issues
- npm.io page: https://npm.io/package/@pixwell/graphql-eventstore-subscriptions

## Dependencies (3)

- [iterall](https://npm.io/package/iterall.md) ^1.3.0
- [graphql-subscriptions](https://npm.io/package/graphql-subscriptions.md) ^1.1.0
- [node-eventstore-client](https://npm.io/package/node-eventstore-client.md) ^0.2.16

## 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

- 1.1.3 (latest) — 2020-10-01
- 1.1.2 — 2020-08-26
- 1.1.1 — 2020-08-24
- 1.1.0 — 2020-08-21
- 1.0.1 — 2020-08-20
- 1.0.0 — 2020-08-19

## README

# graphql-eventstore-subscriptions

_graphql-eventstore-subscriptions_ implements the `PubSubEngine` interface from the [graphql-subscriptions](https://github.com/apollographql/graphql-subscriptions) package.

Unlike other databases, Google's Firestore comes across with real time updates. Therefore, it is not required to publish events to a queue or a pub-sub.
However, there is still something to do to get the data to the clients. In graphql-eventstore-subscriptions those tasks are called handlers. They are subscribing a specific topic and broadcast whatever you want over an AsyncIterator which is compatible with graphql-subscriptions.

## Usage

First of all, you have to install the _graphql-eventstore-subscriptions_ package using **yarn** or **npm** by calling either `yarn add graphql-eventstore-subscriptions` or `npm i --save graphql-eventstore-subscriptions`.

### Create a new _graphql-eventstore-subscription_ instance

```typescript
import PubSub from 'graphql-eventstore-subscriptions';

const ps = new PubSub();
```

### Full example

```typescript
import PubSub from 'graphql-eventstore-subscriptions';
import db from '../path/to/firestore/connection';

enum Topic {
  NEW_COMMENT = 'NEW_COMMENT',
}
eventstore
const ps = new PubSub();

ps.registerHandler(Topic.NEW_COMMENT, broadcast =>
  // Note, that `onSnapshot` returns a unsubscribe function which
  // returns void.
  db.collection('comments').onSnapshot(snapshot => {
    snapshot
      .docChanges()
      .filter(change => change.type === 'added')
      .map(item => broadcast(item.doc.data()));
  })
);

const iterator = ps.asyncIterator(Topic.NEW_COMMENT);
const addedComment = await iterator.next();

// ...
```

### With apollo-server-graphql

Define a _GraphQL_ schema with a `Subscription` type.

```graphql
schema {
  query: Query
  mutation: Mutation
  subscription: Subscription
}

type Subscription {
  newComment: Comment
}

type Comment {
  message: String
}
```

Now, implement the resolver:

```typescript
export const resolvers = {
  Subscription: {
    newComment: {
      subscribe: () => ps.asyncIterator(Topic.NEW_COMMENT),
    },
  },
};
```

Calling `asyncIterator(topics: string | string[])` will subscribe to the given topics and will return an AsyncIterator bound to the `PubSubEngine` of **graphql-eventstore-subscriptions**.
Everytime, a handler calls the obtained `broadcast`-function, the `PubSubEngine` of **graphql-eventstore-subscriptions** will publish the event.

## API

### createFallThroughHandler

```typescript
function createFallThroughHandler(
  fs: Firestore,
  overwriteOptions: FallThroughHandlerOptions
): [string, Handler];
```

#### Options

| Name           | Type                                                  | Description                                                   |
| -------------- | ----------------------------------------------------- | ------------------------------------------------------------- |
| `topic`\*      | `string`                                              | -                                                             |
| `collection`\* | `string`                                              | The firebase collection                                       |
| `transform`    | `TransformStrategy | (change: DocumentChange) => any` | Called to transform the broadcast-payload                     |
| `filter`       | `(change: DocumentChange) => boolean`                 | Called to filter document changes before they are broadcasted |

> \* required

### createFallThroughHandlerFromMap

```typescript
function createFallThroughHandlerFromMap(
  fs: Firestore,
  options: FallThroughHandlerFromMapOptions
): [string, Handler][];
```

#### Options

| Name  | Type                      | Description                                                  |
| ----- | ------------------------- | ------------------------------------------------------------ |
| topic | `[topic: string]: Object` | See createFallThroughHandler#Options for a complete overview |

## Contribute

Something is broken? The documentation is incorrect? You're missing a feature? ...and you wanna help? That's great.

The following steps are describing the way from an idea / bug / ... to a pull-request.

1. Fork this repository
1. Apply the changes
1. Write tests (you can execute the current tests by calling `npm run test:unit` OR `npm run test:unit:watch`)
1. If necessary, update the documentation
1. Open a pull-request
1. :tada:

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