@boostercloud/application-tester
Version:
Contains Booster types related to the information extracted from the user project
125 lines (124 loc) • 5.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DisconnectableApolloClient = exports.GraphQLHelper = void 0;
const client_1 = require("@apollo/client");
const ws_1 = require("@apollo/client/link/ws");
const cross_fetch_1 = require("cross-fetch");
const WebSocket = require("ws");
const subscriptions_transport_ws_1 = require("subscriptions-transport-ws");
const utilities_1 = require("@apollo/client/utilities");
class GraphQLHelper {
constructor(providerTestHelper) {
this.providerTestHelper = providerTestHelper;
}
client(authToken) {
return new client_1.ApolloClient({
cache: new client_1.InMemoryCache(),
link: this.getAuthLink(authToken).concat(this.getApolloHTTPLink()),
defaultOptions: {
query: {
fetchPolicy: 'no-cache',
},
},
});
}
/**
* IMPORTANT: After using this "DisconnectableApolloClient", you must call ".disconnect()" to close the socket. Otherwise
* it will keep waiting for messages forever
*/
async clientWithSubscriptions(authToken) {
const subscriptionClient = await this.subscriptionsClient(authToken);
const link = (0, client_1.split)(({ query }) => {
const definition = (0, utilities_1.getMainDefinition)(query);
return definition.kind === 'OperationDefinition' && definition.operation === 'subscription';
}, new ws_1.WebSocketLink(subscriptionClient), this.getAuthLink(authToken).concat(this.getApolloHTTPLink()));
return new DisconnectableApolloClient(subscriptionClient, {
cache: new client_1.InMemoryCache(),
link: link,
defaultOptions: {
query: {
fetchPolicy: 'no-cache',
},
},
});
}
getApolloHTTPLink() {
return new client_1.HttpLink({
uri: this.providerTestHelper.outputs.graphqlURL,
fetch: cross_fetch_1.default,
});
}
getAuthLink(authToken) {
return new client_1.ApolloLink((operation, forward) => {
if (authToken) {
const token = typeof authToken == 'function' ? authToken() : authToken;
operation.setContext({ headers: { Authorization: 'Bearer ' + token } });
}
return forward(operation);
});
}
async subscriptionsClient(authToken) {
return new Promise((resolve, reject) => {
const subscriptionClient = new subscriptions_transport_ws_1.SubscriptionClient(this.providerTestHelper.outputs.websocketURL, {
reconnect: true,
connectionParams: () => {
if (authToken) {
const token = typeof authToken == 'function' ? authToken() : authToken;
return {
Authorization: 'Bearer ' + token,
};
}
return {};
},
connectionCallback: (err) => {
if (err) {
reject(err);
return;
}
resolve(subscriptionClient);
},
}, class MyWebSocket extends WebSocket {
constructor(url, protocols) {
super(url, protocols);
this.addListener('open', () => {
console.debug('[GraphQL socket] on open');
});
this.addListener('ping', () => {
console.debug('[GraphQL socket] on "ping"');
});
this.addListener('pong', () => {
console.debug('[GraphQL socket] on "pong"');
});
this.addListener('message', (data) => {
console.debug('[GraphQL socket] on message: ', data);
});
this.addListener('close', (code, message) => {
console.debug('[GraphQL socket] on close: ', code, message);
});
this.addListener('error', (err) => {
console.debug('[GraphQL socket] on error: ', err.message);
});
}
});
});
}
}
exports.GraphQLHelper = GraphQLHelper;
class DisconnectableApolloClient extends client_1.ApolloClient {
constructor(subscriptionClient, options) {
super(options);
this.subscriptionClient = subscriptionClient;
}
reconnect() {
const reconnectPromise = new Promise((resolve) => {
this.subscriptionClient.onReconnected(resolve);
});
this.subscriptionClient.close(false);
return reconnectPromise;
}
disconnect() {
this.subscriptionClient.close();
this.stop();
}
}
exports.DisconnectableApolloClient = DisconnectableApolloClient;