1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const NodeCache = require("node-cache");
|
4 | const RequestProcessor_1 = require("../internal/transport/RequestProcessor");
|
5 | const logger_1 = require("../internal/util/logger");
|
6 | const ApolloGraphClient_1 = require("./ApolloGraphClient");
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | class ApolloGraphClientFactory {
|
13 | constructor(configuration, authCallback) {
|
14 | this.configuration = configuration;
|
15 | this.authCallback = authCallback;
|
16 | this.graphClients = new NodeCache({ stdTTL: 1 * 60, checkperiod: 1 * 30, useClones: false });
|
17 | }
|
18 | createGraphClient(event) {
|
19 | let workspaceId;
|
20 | if (RequestProcessor_1.isCommandIncoming(event)) {
|
21 | workspaceId = event.team.id;
|
22 | }
|
23 | else if (RequestProcessor_1.isEventIncoming(event)) {
|
24 | workspaceId = event.extensions.team_id;
|
25 | }
|
26 | let graphClient = this.graphClients.get(workspaceId);
|
27 | if (graphClient) {
|
28 | logger_1.logger.debug("Re-using cached graph client for team '%s'", workspaceId);
|
29 | return graphClient;
|
30 | }
|
31 | else if (this.authCallback) {
|
32 | logger_1.logger.debug("Creating new graph client for team '%s'", workspaceId);
|
33 | graphClient = new ApolloGraphClient_1.ApolloGraphClient(`${this.configuration.endpoints.graphql}/${workspaceId}`, { Authorization: this.authCallback() });
|
34 | this.graphClients.set(workspaceId, graphClient);
|
35 | return graphClient;
|
36 | }
|
37 | logger_1.logger.debug("Unable to create graph client for team '%s'", workspaceId);
|
38 | return null;
|
39 | }
|
40 | }
|
41 | exports.ApolloGraphClientFactory = ApolloGraphClientFactory;
|
42 |
|
\ | No newline at end of file |