@dillonkearns/elm-graphql
Version:
<img src="https://cdn.jsdelivr.net/gh/martimatix/logo-graphqelm/logo.svg" alt="dillonearns/elm-graphql logo" width="40%" align="right">
60 lines (49 loc) • 1.66 kB
JavaScript
import * as AbsintheSocket from "@absinthe/socket";
import { Socket as PhoenixSocket } from "phoenix";
import { Elm } from "./Main.elm";
let notifiers = [];
document.addEventListener("DOMContentLoaded", function() {
const absintheSocket = AbsintheSocket.create(
new PhoenixSocket("wss://elm-graphql.herokuapp.com/socket")
);
const app = Elm.Main.init({ node: document.getElementById("main") });
app.ports.createSubscriptions.subscribe(function(subscription) {
console.log("createSubscriptions called with", [subscription]);
// Remove existing notifiers
notifiers.map(notifier => AbsintheSocket.cancel(absintheSocket, notifier));
// Create new notifiers for each subscription sent
notifiers = [subscription].map(operation =>
AbsintheSocket.send(absintheSocket, {
operation,
variables: {}
})
);
function onStart(data) {
console.log(">>> Start", JSON.stringify(data));
app.ports.socketStatusConnected.send(null);
}
function onAbort(data) {
console.log(">>> Abort", JSON.stringify(data));
}
function onCancel(data) {
console.log(">>> Cancel", JSON.stringify(data));
}
function onError(data) {
console.log(">>> Error", JSON.stringify(data));
app.ports.socketStatusReconnecting.send(null);
}
function onResult(res) {
console.log(">>> Result", JSON.stringify(res));
app.ports.gotSubscriptionData.send(res);
}
notifiers.map(notifier =>
AbsintheSocket.observe(absintheSocket, notifier, {
onAbort,
onError,
onCancel,
onStart,
onResult
})
);
});
});