Skip to content

Commit

Permalink
docs(recipes): client usage with TypedDocumentNode
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Nov 29, 2023
1 parent 690b202 commit b2f4966
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions website/src/pages/recipes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,58 @@ export const link = new SSELink({
});
```

### With [TypedDocumentNode](https://github.com/dotansimha/graphql-typed-document-node)

```ts
import {
createClient,
Client,
ClientOptions,
ExecutionResult,
Sink,
} from 'graphql-sse';
import { TypedDocumentNode } from '@graphql-typed-document-node/core'; // yarn add @graphql-typed-document-node/core
import { print } from 'graphql';

export interface TypedClient
extends Omit<
Client,
'iterate' | 'subscribe' // we're replacing the `iterate` and `subscribe` implementations
> {
iterate<
Result extends ExecutionResult,
Variables extends Record<string, unknown>,
>(
document: TypedDocumentNode<Result, Variables>,
variables: Variables,
): AsyncIterableIterator<Result>;
subscribe<
Result extends ExecutionResult,
Variables extends Record<string, unknown>,
>(
document: TypedDocumentNode<Result, Variables>,
variables: Variables,
): Sink<Result>;
}

export function createTypedClient(options: ClientOptions): TypedClient {
const client = createClient(options);
return {
...client,
iterate: (document, variables) =>
client.iterate.bind(client)({
query: print(document),
variables,
}),
subscribe: (document, variables) =>
client.subscribe.bind(client)({
query: print(document),
variables,
}),
};
}
```

### For HTTP/1 (aka. [single connection mode](https://github.com/enisdenjo/graphql-sse/blob/master/PROTOCOL.md#single-connection-mode))

```js
Expand Down

0 comments on commit b2f4966

Please sign in to comment.