@graphql-tools/executor-legacy-ws
Advanced tools
Comparing version 1.0.1-rc-20230612101309-a46efd88 to 1.0.1-rc-20230612102029-16c7ce78
@@ -23,27 +23,36 @@ "use strict"; | ||
let websocket = null; | ||
const ensureWebsocket = () => { | ||
websocket = new WebSocketImpl(subscriptionsEndpoint, 'graphql-ws', { | ||
followRedirects: true, | ||
headers: options?.headers, | ||
rejectUnauthorized: false, | ||
skipUTF8Validation: true, | ||
}); | ||
websocket.onopen = () => { | ||
let payload = {}; | ||
switch (typeof options?.connectionParams) { | ||
case 'function': | ||
payload = options?.connectionParams(); | ||
break; | ||
case 'object': | ||
payload = options?.connectionParams; | ||
break; | ||
} | ||
websocket.send(JSON.stringify({ | ||
type: LEGACY_WS.CONNECTION_INIT, | ||
payload, | ||
})); | ||
}; | ||
websocket.onclose = () => { | ||
websocket = null; | ||
}; | ||
const ensureWebsocket = (errorHandler = err => console.error(err)) => { | ||
if (websocket == null || websocket.readyState !== isomorphic_ws_1.default.OPEN) { | ||
websocket = new WebSocketImpl(subscriptionsEndpoint, 'graphql-ws', { | ||
followRedirects: true, | ||
headers: options?.headers, | ||
rejectUnauthorized: false, | ||
skipUTF8Validation: true, | ||
}); | ||
websocket.onopen = () => { | ||
let payload = {}; | ||
switch (typeof options?.connectionParams) { | ||
case 'function': | ||
payload = options?.connectionParams(); | ||
break; | ||
case 'object': | ||
payload = options?.connectionParams; | ||
break; | ||
} | ||
websocket.send(JSON.stringify({ | ||
type: LEGACY_WS.CONNECTION_INIT, | ||
payload, | ||
}), (error) => { | ||
if (error) { | ||
errorHandler(error); | ||
} | ||
}); | ||
}; | ||
websocket.onerror = event => { | ||
errorHandler(event.error); | ||
}; | ||
websocket.onclose = () => { | ||
websocket = null; | ||
}; | ||
} | ||
}; | ||
@@ -63,2 +72,5 @@ const cleanupWebsocket = () => { | ||
subscribe(observer) { | ||
function errorHandler(err) { | ||
observer.error(err); | ||
} | ||
ensureWebsocket(); | ||
@@ -83,3 +95,7 @@ if (websocket == null) { | ||
}, | ||
})); | ||
}), (error) => { | ||
if (error) { | ||
errorHandler(error); | ||
} | ||
}); | ||
break; | ||
@@ -99,8 +115,11 @@ } | ||
case LEGACY_WS.COMPLETE: { | ||
if (websocket == null) { | ||
throw new Error(`WebSocket connection is not found!`); | ||
if (websocket != null) { | ||
websocket.send(JSON.stringify({ | ||
type: LEGACY_WS.CONNECTION_TERMINATE, | ||
}), (error) => { | ||
if (error) { | ||
errorHandler(error); | ||
} | ||
}); | ||
} | ||
websocket.send(JSON.stringify({ | ||
type: LEGACY_WS.CONNECTION_TERMINATE, | ||
})); | ||
observer.complete(); | ||
@@ -107,0 +126,0 @@ cleanupWebsocket(); |
@@ -19,27 +19,36 @@ import { observableToAsyncIterable } from '@graphql-tools/utils'; | ||
let websocket = null; | ||
const ensureWebsocket = () => { | ||
websocket = new WebSocketImpl(subscriptionsEndpoint, 'graphql-ws', { | ||
followRedirects: true, | ||
headers: options?.headers, | ||
rejectUnauthorized: false, | ||
skipUTF8Validation: true, | ||
}); | ||
websocket.onopen = () => { | ||
let payload = {}; | ||
switch (typeof options?.connectionParams) { | ||
case 'function': | ||
payload = options?.connectionParams(); | ||
break; | ||
case 'object': | ||
payload = options?.connectionParams; | ||
break; | ||
} | ||
websocket.send(JSON.stringify({ | ||
type: LEGACY_WS.CONNECTION_INIT, | ||
payload, | ||
})); | ||
}; | ||
websocket.onclose = () => { | ||
websocket = null; | ||
}; | ||
const ensureWebsocket = (errorHandler = err => console.error(err)) => { | ||
if (websocket == null || websocket.readyState !== WebSocket.OPEN) { | ||
websocket = new WebSocketImpl(subscriptionsEndpoint, 'graphql-ws', { | ||
followRedirects: true, | ||
headers: options?.headers, | ||
rejectUnauthorized: false, | ||
skipUTF8Validation: true, | ||
}); | ||
websocket.onopen = () => { | ||
let payload = {}; | ||
switch (typeof options?.connectionParams) { | ||
case 'function': | ||
payload = options?.connectionParams(); | ||
break; | ||
case 'object': | ||
payload = options?.connectionParams; | ||
break; | ||
} | ||
websocket.send(JSON.stringify({ | ||
type: LEGACY_WS.CONNECTION_INIT, | ||
payload, | ||
}), (error) => { | ||
if (error) { | ||
errorHandler(error); | ||
} | ||
}); | ||
}; | ||
websocket.onerror = event => { | ||
errorHandler(event.error); | ||
}; | ||
websocket.onclose = () => { | ||
websocket = null; | ||
}; | ||
} | ||
}; | ||
@@ -59,2 +68,5 @@ const cleanupWebsocket = () => { | ||
subscribe(observer) { | ||
function errorHandler(err) { | ||
observer.error(err); | ||
} | ||
ensureWebsocket(); | ||
@@ -79,3 +91,7 @@ if (websocket == null) { | ||
}, | ||
})); | ||
}), (error) => { | ||
if (error) { | ||
errorHandler(error); | ||
} | ||
}); | ||
break; | ||
@@ -95,8 +111,11 @@ } | ||
case LEGACY_WS.COMPLETE: { | ||
if (websocket == null) { | ||
throw new Error(`WebSocket connection is not found!`); | ||
if (websocket != null) { | ||
websocket.send(JSON.stringify({ | ||
type: LEGACY_WS.CONNECTION_TERMINATE, | ||
}), (error) => { | ||
if (error) { | ||
errorHandler(error); | ||
} | ||
}); | ||
} | ||
websocket.send(JSON.stringify({ | ||
type: LEGACY_WS.CONNECTION_TERMINATE, | ||
})); | ||
observer.complete(); | ||
@@ -103,0 +122,0 @@ cleanupWebsocket(); |
{ | ||
"name": "@graphql-tools/executor-legacy-ws", | ||
"version": "1.0.1-rc-20230612101309-a46efd88", | ||
"version": "1.0.1-rc-20230612102029-16c7ce78", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
14487
299