overmind-graphql
Version:
Functional actions
83 lines • 2.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.absintheSocket = void 0;
exports.absintheSocket = {
/**
* Create an Absinthe socket wrapper around a Phoenix socket
*/
create(phoenixSocket) {
phoenixSocket.connect();
const channel = phoenixSocket.channel('__absinthe__:control');
channel
.join()
.receive('ok', () => {
// Channel joined successfully
})
.receive('error', (reason) => {
console.error('Failed to join Absinthe control channel:', reason);
});
return {
phoenixSocket,
channel,
subscriptions: new Map(),
};
},
/**
* Send a GraphQL subscription request
*/
send(absintheSocket, request) {
const subscriptionId = `subscription:${Date.now()}:${Math.random()
.toString(36)
.substr(2, 9)}`;
absintheSocket.channel
.push('doc', {
query: request.operation,
variables: request.variables || {},
})
.receive('ok', (response) => {
if (response.subscriptionId) {
absintheSocket.subscriptions.set(response.subscriptionId, subscriptionId);
}
})
.receive('error', (error) => {
console.error('Subscription error:', error);
});
return {
subscriptionId,
absintheSocket,
};
},
/**
* Observe subscription results
*/
observe(absintheSocket, notifier, callbacks) {
const ref = absintheSocket.channel.on('subscription:data', (payload) => {
const subscriptionId = absintheSocket.subscriptions.get(payload.subscriptionId);
if (subscriptionId === notifier.subscriptionId) {
callbacks.onResult({ data: payload.result });
}
});
return {
subscriptionId: notifier.subscriptionId,
ref,
};
},
/**
* Unsubscribe and clean up
*/
unobserve(absintheSocket, notifier, observer) {
absintheSocket.channel.off('subscription:data', observer.ref);
// Clean up subscription
absintheSocket.channel.push('unsubscribe', {
subscriptionId: notifier.subscriptionId,
});
// Remove from subscriptions map
for (const [key, value] of absintheSocket.subscriptions.entries()) {
if (value === notifier.subscriptionId) {
absintheSocket.subscriptions.delete(key);
break;
}
}
},
};
//# sourceMappingURL=absinthe-socket.js.map