diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-26 12:09:53 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-26 12:09:53 +0300 |
commit | 0ccabeb3f62c5fbc81f52cc16fa654404bb87874 (patch) | |
tree | 27c81cfa9d498fa0b604acaa9c4f5400743f83fd /app/assets/javascripts/actioncable_link.js | |
parent | 6819cb95c9c0aa63fce1d246026978df5cac9e44 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/assets/javascripts/actioncable_link.js')
-rw-r--r-- | app/assets/javascripts/actioncable_link.js | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/app/assets/javascripts/actioncable_link.js b/app/assets/javascripts/actioncable_link.js new file mode 100644 index 00000000000..4c642db2f3c --- /dev/null +++ b/app/assets/javascripts/actioncable_link.js @@ -0,0 +1,40 @@ +import { ApolloLink, Observable } from 'apollo-link'; +import { print } from 'graphql'; +import cable from '~/actioncable_consumer'; +import { uuids } from '~/diffs/utils/uuids'; + +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(); + }, + }; + }); + } +} |