UNPKG

wgc

Version:

The official CLI tool to manage the GraphQL Federation Platform Cosmo

96 lines 3.88 kB
import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb'; import { program } from 'commander'; import jwtDecode from 'jwt-decode'; import pc from 'picocolors'; import { config, getBaseHeaders } from '../../../core/config.js'; export const fetchRouterConfig = async ({ client, name, namespace, }) => { var _a, _b; const resp = await client.platform.generateRouterToken({ fedGraphName: name, namespace, }, { headers: getBaseHeaders(), }); if (((_a = resp.response) === null || _a === void 0 ? void 0 : _a.code) !== EnumStatusCode.OK) { throw new Error(`${pc.red(`Could not fetch the router config for the graph ${pc.bold(name)}`)} \n${pc.red(pc.bold(((_b = resp.response) === null || _b === void 0 ? void 0 : _b.details) || ''))}`); } let decoded; try { decoded = jwtDecode(resp.token); } catch { program.error('Could not fetch the router config. Please try again'); } const requestBody = JSON.stringify({ Version: '', }); const headers = new Headers(); headers.append('Content-Type', 'application/json; charset=UTF-8'); headers.append('Authorization', 'Bearer ' + resp.token); headers.append('Accept-Encoding', 'gzip'); const url = new URL(`/${decoded.organization_id}/${decoded.federated_graph_id}/routerconfigs/latest.json`, config.cdnURL); const response = await fetch(url, { method: 'POST', headers, body: requestBody, }); const routerConfig = await response.text(); return routerConfig; }; export const getSubgraphsOfFedGraph = async ({ client, name, namespace, }) => { var _a, _b; const resp = await client.platform.getFederatedGraphByName({ name, namespace, includeMetrics: false, }, { headers: getBaseHeaders(), }); if (((_a = resp.response) === null || _a === void 0 ? void 0 : _a.code) !== EnumStatusCode.OK) { throw new Error(`${pc.red(`Could not fetch the federated graph ${pc.bold(name)}`)} \n${pc.red(pc.bold(((_b = resp.response) === null || _b === void 0 ? void 0 : _b.details) || ''))}`); } const subgraphs = await resp.subgraphs; return subgraphs.map((s) => { return { name: s.name, routingURL: s.routingURL, subscriptionURL: s.subscriptionUrl, subscriptionProtocol: s.subscriptionProtocol, isEventDrivenGraph: s.isEventDrivenGraph, isV2Graph: s.isV2Graph, }; }); }; export const getFederatedGraphSchemas = async ({ client, name, namespace, }) => { var _a, _b; const resp = await client.platform.getFederatedGraphSDLByName({ name, namespace, }, { headers: getBaseHeaders(), }); if (((_a = resp.response) === null || _a === void 0 ? void 0 : _a.code) !== EnumStatusCode.OK || !(resp === null || resp === void 0 ? void 0 : resp.sdl)) { throw new Error(`${pc.red(`Could not fetch the SDL of the federated graph ${pc.bold(name)}`)} \n${pc.red(pc.bold(((_b = resp.response) === null || _b === void 0 ? void 0 : _b.details) || ''))}`); } return { sdl: resp.sdl, clientSchema: resp.clientSchema, }; }; // Returns the latest valid schema version of a subgraph that was composed to form the provided federated graph. export const getSubgraphSDL = async ({ client, fedGraphName, subgraphName, namespace, }) => { var _a; const resp = await client.platform.getSubgraphSDLFromLatestComposition({ name: subgraphName, namespace, fedGraphName, }, { headers: getBaseHeaders(), }); if (((_a = resp.response) === null || _a === void 0 ? void 0 : _a.code) !== EnumStatusCode.OK) { return undefined; } const sdl = await resp.sdl; return sdl; }; //# sourceMappingURL=utils.js.map