-
Good morning As mentioned in the title, the recipe for graphql-ws client with apollo is not working with Vanilla JS. This example was made for TypeScript. Could you tell me how to adapt this example to work with Vanilla JS please ? Thank you in advance for your help. |
Beta Was this translation helpful? Give feedback.
Answered by
enisdenjo
Jan 24, 2022
Replies: 1 comment 1 reply
-
A simple Google search with "typescript to javascript" gives you the tools. TS Playground from the results would give you a JavaScript only version like this: // for Apollo Client v3:
import { ApolloLink, Observable, } from '@apollo/client/core';
// or for Apollo Client v2:
// import { ApolloLink, Operation, FetchResult, Observable } from 'apollo-link'; // yarn add apollo-link
import { print } from 'graphql';
import { createClient } from 'graphql-ws';
class WebSocketLink extends ApolloLink {
constructor(options) {
super();
this.client = createClient(options);
}
request(operation) {
return new Observable((sink) => {
return this.client.subscribe(Object.assign(Object.assign({}, operation), { query: print(operation.query) }), {
next: sink.next.bind(sink),
complete: sink.complete.bind(sink),
error: sink.error.bind(sink),
});
});
}
}
const link = new WebSocketLink({
url: 'ws://where.is:4000/graphql',
connectionParams: () => {
const session = getSession();
if (!session) {
return {};
}
return {
Authorization: `Bearer ${session.token}`,
};
},
}); P.S. you should learn TypeScript ;) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
NicoSan20
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A simple Google search with "typescript to javascript" gives you the tools. TS Playground from the results would give you a JavaScript only version like this: