var myHeaders = new Headers();
myHeaders.append("Accept", "multipart/mixed");
var raw = JSON.stringify({
"query": "subscription test {\n currentTime {unixTime}\n}",
"operationName": "test"
});
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
const response = await fetch("https://demo-router.fly.dev/graphql", requestOptions)
// To recieve data as a string we use TextDecoderStream class in pipethrough
const reader = response.body.pipeThrough(new TextDecoderStream()).getReader()
// Consume unless you reload the page. You can also use the AbortController API to close the connection.
while (true) {
const {value, done} = await reader.read();
if (done) break;
// Remove the data: prefix and parse the message
console.log('Received', JSON.parse(value.replace("data: ", "")));
}