UNPKG

@chmoyle-conga/graphql-cart

Version:

Cart module for realtime subscriptions to conga cart

47 lines (46 loc) 2.01 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@apollo/client/core"); const graphql_sse_1 = require("graphql-sse"); const environment_1 = __importDefault(require("./environment")); class ClientManager { constructor() { } initialize() { // HTTP connection to the GraphQL API this.httpLink = (0, core_1.createHttpLink)({ uri: `${environment_1.default.getEndpoint()}/api/cart/graphql/v1`, }); this.sseClient = (0, graphql_sse_1.createClient)({ // singleConnection: true, use "single connection mode" instead of the default "distinct connection mode" url: `${environment_1.default.getEndpoint()}/api/cart/graphql/v1`, headers: () => { return { "Authorization": "Bearer " + environment_1.default.getToken() }; }, retryAttempts: 3 }); // Middleware for adding the authorization token to requests this.authLink = new core_1.ApolloLink((operation, forward) => { // Retrieve the authentication token from local storage or another source const token = environment_1.default.getToken(); // Use the setContext method to set the HTTP headers. operation.setContext({ headers: { Authorization: token ? `Bearer ${token}` : '', } }); // Call the next link in the middleware chain. return forward(operation); }); this.client = new core_1.ApolloClient({ link: this.authLink.concat(this.httpLink), cache: new core_1.InMemoryCache(), }); } } const clientManager = new ClientManager(); exports.default = clientManager;