Skip to content

Commit

Permalink
fix: Define graphql execution results
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Sep 8, 2021
1 parent b1fb883 commit a64c91b
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 16 deletions.
59 changes: 59 additions & 0 deletions docs/interfaces/common.ExecutionPatchResult.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[graphql-ws](../README.md) / [common](../modules/common.md) / ExecutionPatchResult

# Interface: ExecutionPatchResult<Data, Extensions\>

[common](../modules/common.md).ExecutionPatchResult

## Type parameters

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

## Table of contents

### Properties

- [data](common.ExecutionPatchResult.md#data)
- [errors](common.ExecutionPatchResult.md#errors)
- [extensions](common.ExecutionPatchResult.md#extensions)
- [hasNext](common.ExecutionPatchResult.md#hasnext)
- [label](common.ExecutionPatchResult.md#label)
- [path](common.ExecutionPatchResult.md#path)

## Properties

### data

`Optional` **data**: ``null`` \| `Data`

___

### errors

`Optional` **errors**: readonly `GraphQLError`[]

___

### extensions

`Optional` **extensions**: `Extensions`

___

### hasNext

**hasNext**: `boolean`

___

### label

`Optional` **label**: `string`

___

### path

`Optional` **path**: readonly (`string` \| `number`)[]
45 changes: 45 additions & 0 deletions docs/interfaces/common.ExecutionResult.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[graphql-ws](../README.md) / [common](../modules/common.md) / ExecutionResult

# Interface: ExecutionResult<Data, Extensions\>

[common](../modules/common.md).ExecutionResult

## Type parameters

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

## Table of contents

### Properties

- [data](common.ExecutionResult.md#data)
- [errors](common.ExecutionResult.md#errors)
- [extensions](common.ExecutionResult.md#extensions)
- [hasNext](common.ExecutionResult.md#hasnext)

## Properties

### data

`Optional` **data**: ``null`` \| `Data`

___

### errors

`Optional` **errors**: readonly `GraphQLError`[]

___

### extensions

`Optional` **extensions**: `Extensions`

___

### hasNext

`Optional` **hasNext**: `boolean`
2 changes: 1 addition & 1 deletion docs/interfaces/common.NextMessage.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ___

### payload

`Readonly` **payload**: `ExecutionResult`<`ObjMap`<`unknown`\>, `ObjMap`<`unknown`\>\>
`Readonly` **payload**: [`ExecutionResult`](common.ExecutionResult.md)<`Record`<`string`, `unknown`\>, `Record`<`string`, `unknown`\>\> \| [`ExecutionPatchResult`](common.ExecutionPatchResult.md)<`unknown`, `Record`<`string`, `unknown`\>\>

___

Expand Down
2 changes: 1 addition & 1 deletion docs/interfaces/server.Context.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ ___

### subscriptions

`Readonly` **subscriptions**: `Record`<`string`, ``null`` \| `AsyncIterator`<`unknown`, `any`, `undefined`\>\>
`Readonly` **subscriptions**: `Record`<`string`, ``null`` \| `AsyncGenerator`<`unknown`, `any`, `unknown`\> \| `AsyncIterable`<`unknown`\>\>

Holds the active subscriptions for this context. **All operations**
that are taking place are aggregated here. The user is _subscribed_
Expand Down
6 changes: 3 additions & 3 deletions docs/interfaces/server.ServerOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ ___

### onNext

`Optional` **onNext**(`ctx`, `message`, `args`, `result`): `void` \| `ExecutionResult`<`ObjMap`<`unknown`\>, `ObjMap`<`unknown`\>\> \| `Promise`<`void` \| `ExecutionResult`<`ObjMap`<`unknown`\>, `ObjMap`<`unknown`\>\>\>
`Optional` **onNext**(`ctx`, `message`, `args`, `result`): `void` \| [`ExecutionResult`](common.ExecutionResult.md)<`Record`<`string`, `unknown`\>, `Record`<`string`, `unknown`\>\> \| [`ExecutionPatchResult`](common.ExecutionPatchResult.md)<`unknown`, `Record`<`string`, `unknown`\>\> \| `Promise`<`void` \| [`ExecutionResult`](common.ExecutionResult.md)<`Record`<`string`, `unknown`\>, `Record`<`string`, `unknown`\>\> \| [`ExecutionPatchResult`](common.ExecutionPatchResult.md)<`unknown`, `Record`<`string`, `unknown`\>\>\>

Executed after an operation has emitted a result right before
that result has been sent to the client. Results from both
Expand All @@ -402,11 +402,11 @@ in the close event reason.
| `ctx` | [`Context`](server.Context.md)<`E`\> |
| `message` | [`NextMessage`](common.NextMessage.md) |
| `args` | `ExecutionArgs` |
| `result` | `ExecutionResult`<`ObjMap`<`unknown`\>, `ObjMap`<`unknown`\>\> |
| `result` | [`ExecutionResult`](common.ExecutionResult.md)<`Record`<`string`, `unknown`\>, `Record`<`string`, `unknown`\>\> \| [`ExecutionPatchResult`](common.ExecutionPatchResult.md)<`unknown`, `Record`<`string`, `unknown`\>\> |

#### Returns

`void` \| `ExecutionResult`<`ObjMap`<`unknown`\>, `ObjMap`<`unknown`\>\> \| `Promise`<`void` \| `ExecutionResult`<`ObjMap`<`unknown`\>, `ObjMap`<`unknown`\>\>\>
`void` \| [`ExecutionResult`](common.ExecutionResult.md)<`Record`<`string`, `unknown`\>, `Record`<`string`, `unknown`\>\> \| [`ExecutionPatchResult`](common.ExecutionPatchResult.md)<`unknown`, `Record`<`string`, `unknown`\>\> \| `Promise`<`void` \| [`ExecutionResult`](common.ExecutionResult.md)<`Record`<`string`, `unknown`\>, `Record`<`string`, `unknown`\>\> \| [`ExecutionPatchResult`](common.ExecutionPatchResult.md)<`unknown`, `Record`<`string`, `unknown`\>\>\>

___

Expand Down
14 changes: 14 additions & 0 deletions docs/modules/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
- [ConnectionInitMessage](client.md#connectioninitmessage)
- [Disposable](client.md#disposable)
- [ErrorMessage](client.md#errormessage)
- [ExecutionPatchResult](client.md#executionpatchresult)
- [ExecutionResult](client.md#executionresult)
- [GRAPHQL\_TRANSPORT\_WS\_PROTOCOL](client.md#graphql_transport_ws_protocol)
- [ID](client.md#id)
- [JSONMessageReplacer](client.md#jsonmessagereplacer)
Expand Down Expand Up @@ -384,6 +386,18 @@ Re-exports: [ErrorMessage](../interfaces/common.ErrorMessage.md)

___

### ExecutionPatchResult

Re-exports: [ExecutionPatchResult](../interfaces/common.ExecutionPatchResult.md)

___

### ExecutionResult

Re-exports: [ExecutionResult](../interfaces/common.ExecutionResult.md)

___

### GRAPHQL\_TRANSPORT\_WS\_PROTOCOL

Re-exports: [GRAPHQL\_TRANSPORT\_WS\_PROTOCOL](common.md#graphql_transport_ws_protocol)
Expand Down
2 changes: 2 additions & 0 deletions docs/modules/common.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
- [ConnectionInitMessage](../interfaces/common.ConnectionInitMessage.md)
- [Disposable](../interfaces/common.Disposable.md)
- [ErrorMessage](../interfaces/common.ErrorMessage.md)
- [ExecutionPatchResult](../interfaces/common.ExecutionPatchResult.md)
- [ExecutionResult](../interfaces/common.ExecutionResult.md)
- [NextMessage](../interfaces/common.NextMessage.md)
- [PingMessage](../interfaces/common.PingMessage.md)
- [PongMessage](../interfaces/common.PongMessage.md)
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ ___

### OperationResult

Ƭ **OperationResult**: `Promise`<`AsyncIterableIterator`<`ExecutionResult`\> \| `ExecutionResult`\> \| `AsyncIterableIterator`<`ExecutionResult`\> \| `ExecutionResult`
Ƭ **OperationResult**: `Promise`<`AsyncGenerator`<[`ExecutionResult`](../interfaces/common.ExecutionResult.md) \| [`ExecutionPatchResult`](../interfaces/common.ExecutionPatchResult.md)\> \| `AsyncIterable`<[`ExecutionResult`](../interfaces/common.ExecutionResult.md) \| [`ExecutionPatchResult`](../interfaces/common.ExecutionPatchResult.md)\> \| [`ExecutionResult`](../interfaces/common.ExecutionResult.md)\> \| `AsyncGenerator`<[`ExecutionResult`](../interfaces/common.ExecutionResult.md) \| [`ExecutionPatchResult`](../interfaces/common.ExecutionPatchResult.md)\> \| `AsyncIterable`<[`ExecutionResult`](../interfaces/common.ExecutionResult.md) \| [`ExecutionPatchResult`](../interfaces/common.ExecutionPatchResult.md)\> \| [`ExecutionResult`](../interfaces/common.ExecutionResult.md)

___

Expand Down
28 changes: 26 additions & 2 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
*/

import { GraphQLError, ExecutionResult } from 'graphql';
import { GraphQLError } from 'graphql';
import {
isObject,
areGraphQLErrors,
Expand Down Expand Up @@ -131,11 +131,35 @@ export interface SubscribePayload {
readonly extensions?: Record<string, unknown> | null;
}

/** @category Common */
export interface ExecutionResult<
Data = Record<string, unknown>,
Extensions = Record<string, unknown>,
> {
errors?: ReadonlyArray<GraphQLError>;
data?: Data | null;
hasNext?: boolean;
extensions?: Extensions;
}

/** @category Common */
export interface ExecutionPatchResult<
Data = unknown,
Extensions = Record<string, unknown>,
> {
errors?: ReadonlyArray<GraphQLError>;
data?: Data | null;
path?: ReadonlyArray<string | number>;
label?: string;
hasNext: boolean;
extensions?: Extensions;
}

/** @category Common */
export interface NextMessage {
readonly id: ID;
readonly type: MessageType.Next;
readonly payload: ExecutionResult;
readonly payload: ExecutionResult | ExecutionPatchResult;
}

/** @category Common */
Expand Down
24 changes: 16 additions & 8 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
getOperationAST,
GraphQLError,
SubscriptionArgs,
ExecutionResult,
} from 'graphql';
import {
GRAPHQL_TRANSPORT_WS_PROTOCOL,
Expand All @@ -33,6 +32,8 @@ import {
JSONMessageReviver,
PingMessage,
PongMessage,
ExecutionResult,
ExecutionPatchResult,
} from './common';
import {
isObject,
Expand All @@ -44,12 +45,12 @@ import {
/** @category Server */
export type OperationResult =
| Promise<
| AsyncGenerator<ExecutionResult>
| AsyncIterable<ExecutionResult>
| AsyncGenerator<ExecutionResult | ExecutionPatchResult>
| AsyncIterable<ExecutionResult | ExecutionPatchResult>
| ExecutionResult
>
| AsyncGenerator<ExecutionResult>
| AsyncIterable<ExecutionResult>
| AsyncGenerator<ExecutionResult | ExecutionPatchResult>
| AsyncIterable<ExecutionResult | ExecutionPatchResult>
| ExecutionResult;

/**
Expand Down Expand Up @@ -356,8 +357,12 @@ export interface ServerOptions<E = unknown> {
ctx: Context<E>,
message: NextMessage,
args: ExecutionArgs,
result: ExecutionResult,
) => Promise<ExecutionResult | void> | ExecutionResult | void;
result: ExecutionResult | ExecutionPatchResult,
) =>
| Promise<ExecutionResult | ExecutionPatchResult | void>
| ExecutionResult
| ExecutionPatchResult
| void;
/**
* The complete callback is executed after the
* operation has completed right before sending
Expand Down Expand Up @@ -652,7 +657,10 @@ export function makeServer<E = unknown>(options: ServerOptions<E>): Server<E> {
ctx.subscriptions[id] = null;

const emit = {
next: async (result: ExecutionResult, args: ExecutionArgs) => {
next: async (
result: ExecutionResult | ExecutionPatchResult,
args: ExecutionArgs,
) => {
let nextMessage: NextMessage = {
id,
type: MessageType.Next,
Expand Down

0 comments on commit a64c91b

Please sign in to comment.