Skip to content

Commit

Permalink
docs(recipe): client usage supported check [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Oct 11, 2021
1 parent 9445a81 commit c47dcf0
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,41 @@ function createPingerClient(options: ClientOptions): PingerClient {

</details>

<details id="supported-check">
<summary><a href="#supported-check">🔗</a> Client usage supported check</summary>

```ts
import { createClient } from 'graphql-ws';

function supportsGraphQLTransportWS(url: string): Promise<boolean> {
return new Promise((resolve) => {
const client = createClient({
url,
retryAttempts: 0, // fail immediately
lazy: false, // connect as soon as the client is created
on: {
closed: () => resolve(false), // connection rejected, probably not supported
connected: () => {
resolve(true); // connected = supported
client.dispose(); // dispose after check
},
},
});
});
}

const supported = await supportsGraphQLTransportWS(
'ws://some.unknown:4000/enpoint',
);
if (supported) {
// use graphql-ws
} else {
// fallback (use subscriptions-transport-ws?)
}
```

</details>

<details id="browser">
<summary><a href="#browser">🔗</a> Client usage in browser</summary>

Expand Down

0 comments on commit c47dcf0

Please sign in to comment.