UNPKG

1.95 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const axios_1 = require("axios");
4const axios_fetch_1 = require("axios-fetch");
5const NodeCache = require("node-cache");
6const http_1 = require("../internal/util/http");
7const logger_1 = require("../util/logger");
8const ApolloGraphClient_1 = require("./ApolloGraphClient");
9/**
10 * Factory for creating GraphClient instances for incoming commands and events.
11 *
12 * Uses a cache to store GraphClient instances for 5 mins after which new instances will be given out.
13 */
14class ApolloGraphClientFactory {
15 create(workspaceId, configuration) {
16 this.init();
17 let graphClient = this.graphClients.get(workspaceId);
18 if (graphClient) {
19 return graphClient;
20 }
21 else {
22 const headers = {
23 "Authorization": `Bearer ${configuration.apiKey}`,
24 "apollographql-client-name": `${configuration.name}/${workspaceId}`,
25 "apollographql-client-version": configuration.version,
26 };
27 graphClient = new ApolloGraphClient_1.ApolloGraphClient(`${configuration.endpoints.graphql}/${workspaceId}`, headers, this.configure(configuration), configuration.graphql.listeners);
28 this.graphClients.set(workspaceId, graphClient);
29 return graphClient;
30 }
31 logger_1.logger.debug("Unable to create graph client for team '%s'", workspaceId);
32 return null;
33 }
34 configure(configuration) {
35 return axios_fetch_1.buildAxiosFetch(axios_1.default.create(http_1.configureProxy({})));
36 }
37 init() {
38 if (!this.graphClients) {
39 this.graphClients = new NodeCache({
40 stdTTL: 1 * 60,
41 checkperiod: 1 * 30,
42 useClones: false,
43 });
44 }
45 }
46}
47exports.ApolloGraphClientFactory = ApolloGraphClientFactory;
48//# sourceMappingURL=ApolloGraphClientFactory.js.map
\No newline at end of file