2021-04-26 05:09:53 -04:00
|
|
|
import { ApolloLink, Observable } from 'apollo-link';
|
|
|
|
import { print } from 'graphql';
|
|
|
|
import cable from '~/actioncable_consumer';
|
2021-05-10 08:10:26 -04:00
|
|
|
import { uuids } from '~/lib/utils/uuids';
|
2021-04-26 05:09:53 -04:00
|
|
|
|
|
|
|
export default class ActionCableLink extends ApolloLink {
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
request(operation) {
|
|
|
|
return new Observable((observer) => {
|
|
|
|
const subscription = cable.subscriptions.create(
|
|
|
|
{
|
|
|
|
channel: 'GraphqlChannel',
|
|
|
|
query: operation.query ? print(operation.query) : null,
|
|
|
|
variables: operation.variables,
|
|
|
|
operationName: operation.operationName,
|
|
|
|
nonce: uuids()[0],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
received(data) {
|
|
|
|
if (data.errors) {
|
|
|
|
observer.error(data.errors);
|
|
|
|
} else if (data.result) {
|
|
|
|
observer.next(data.result);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data.more) {
|
|
|
|
observer.complete();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
unsubscribe() {
|
|
|
|
subscription.unsubscribe();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|