Skip to content

Commit

Permalink
fix: Sink's next callback always receives an ExecutionResult
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Aug 26, 2021
1 parent ad5aea2 commit 045b402
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
7 changes: 4 additions & 3 deletions docs/interfaces/client.Client.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ ___

### subscribe

**subscribe**<`T`\>(`payload`, `sink`): () => `void`
**subscribe**<`Data`, `Extensions`\>(`payload`, `sink`): () => `void`

Subscribes through the WebSocket following the config parameters. It
uses the `sink` to emit received data or errors. Returns a _cleanup_
Expand All @@ -81,14 +81,15 @@ function used for dropping the subscription and cleaning stuff up.

| Name | Type |
| :------ | :------ |
| `T` | `unknown` |
| `Data` | `Record`<`string`, `unknown`\> |
| `Extensions` | `unknown` |

#### Parameters

| Name | Type |
| :------ | :------ |
| `payload` | [`SubscribePayload`](common.SubscribePayload.md) |
| `sink` | [`Sink`](common.Sink.md)<`T`\> |
| `sink` | [`Sink`](common.Sink.md)<`ExecutionResult`<`Data`, `Extensions`\>\> |

#### Returns

Expand Down
8 changes: 6 additions & 2 deletions src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
SubscribePayload,
} from '../common';
import { startWSTServer as startTServer } from './utils';
import { ExecutionResult } from 'graphql';

// simulate browser environment for easier client testing
beforeEach(() => {
Expand All @@ -27,7 +28,10 @@ function noop(): void {
}

interface TSubscribe<T> {
waitForNext: (test?: (value: T) => void, expire?: number) => Promise<void>;
waitForNext: (
test?: (value: ExecutionResult<T, unknown>) => void,
expire?: number,
) => Promise<void>;
waitForError: (
test?: (error: unknown) => void,
expire?: number,
Expand All @@ -41,7 +45,7 @@ function tsubscribe<T = unknown>(
payload: SubscribePayload,
): TSubscribe<T> {
const emitter = new EventEmitter();
const results: T[] = [];
const results: ExecutionResult<T, unknown>[] = [];
let error: unknown,
completed = false;
const dispose = client.subscribe<T>(payload, {
Expand Down
6 changes: 5 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
*/

import { ExecutionResult } from 'graphql';
import {
GRAPHQL_TRANSPORT_WS_PROTOCOL,
CloseCode,
Expand Down Expand Up @@ -402,7 +403,10 @@ export interface Client extends Disposable {
* uses the `sink` to emit received data or errors. Returns a _cleanup_
* function used for dropping the subscription and cleaning stuff up.
*/
subscribe<T = unknown>(payload: SubscribePayload, sink: Sink<T>): () => void;
subscribe<Data = Record<string, unknown>, Extensions = unknown>(
payload: SubscribePayload,
sink: Sink<ExecutionResult<Data, Extensions>>,
): () => void;
}

/**
Expand Down

0 comments on commit 045b402

Please sign in to comment.