From c47dcf06fa5d56bf4a8b83d33c2b82f1231f67ef Mon Sep 17 00:00:00 2001 From: enisdenjo Date: Mon, 11 Oct 2021 14:13:51 +0200 Subject: [PATCH] docs(recipe): client usage supported check [skip ci] --- README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/README.md b/README.md index 40f041e4..6e689a27 100644 --- a/README.md +++ b/README.md @@ -689,6 +689,41 @@ function createPingerClient(options: ClientOptions): PingerClient { +
+🔗 Client usage supported check + +```ts +import { createClient } from 'graphql-ws'; + +function supportsGraphQLTransportWS(url: string): Promise { + 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?) +} +``` + +
+
🔗 Client usage in browser