UNPKG

wgc

Version:

The official CLI tool to manage the GraphQL Federation Platform Cosmo

152 lines 5.65 kB
import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb'; import { getBaseHeaders } from '../../core/config.js'; /** * Retrieve user information [email] and [organization name] */ export async function fetchUserInfo(client) { var _a, _b, _c; try { const response = await client.platform.whoAmI({}, { headers: getBaseHeaders(), }); switch ((_a = response.response) === null || _a === void 0 ? void 0 : _a.code) { case EnumStatusCode.OK: { return { userInfo: response, error: null, }; } default: { return { userInfo: null, error: new Error((_c = (_b = response.response) === null || _b === void 0 ? void 0 : _b.details) !== null && _c !== void 0 ? _c : 'An unknown error occurred.'), }; } } } catch (err) { return { userInfo: null, error: err instanceof Error ? err : new Error('An unknown error occurred.'), }; } } /** * Retrieve onboarding record. Provides information about allowed [status]: * [error] | [not-allowed] | [ok] * If record exists, returns [onboarding] metadata. */ export async function checkExistingOnboarding(client) { var _a; const { response, finishedAt, enabled } = await client.platform.getOnboarding({}, { headers: getBaseHeaders(), }); if ((response === null || response === void 0 ? void 0 : response.code) !== EnumStatusCode.OK) { return { error: new Error((_a = response === null || response === void 0 ? void 0 : response.details) !== null && _a !== void 0 ? _a : 'Failed to fetch onboarding metadata.'), status: 'error', }; } if (!enabled) { return { status: 'not-allowed', }; } return { onboarding: { finishedAt, }, status: 'ok', }; } /** * Retrieves federated graph by [name] *demo*. Missing federated graph * is a valid state. */ export async function fetchFederatedGraphByName(client, { name, namespace }) { var _a; const { response, graph, subgraphs } = await client.platform.getFederatedGraphByName({ name, namespace, }, { headers: getBaseHeaders(), }); switch (response === null || response === void 0 ? void 0 : response.code) { case EnumStatusCode.OK: { return { data: { graph, subgraphs }, error: null }; } case EnumStatusCode.ERR_NOT_FOUND: { return { data: null, error: null }; } default: { return { data: null, error: new Error((_a = response === null || response === void 0 ? void 0 : response.details) !== null && _a !== void 0 ? _a : 'An unknown error occured'), }; } } } /** * Cleans up the federated graph by [name] _demo_ and its related * subgraphs. */ export async function cleanUpFederatedGraph(client, graphData) { var _a, _b, _c; const subgraphDeleteResponses = await Promise.all(graphData.subgraphs.map(({ name, namespace }) => client.platform.deleteFederatedSubgraph({ namespace, subgraphName: name, disableResolvabilityValidation: false, }, { headers: getBaseHeaders(), }))); const failedSubgraphDeleteResponses = subgraphDeleteResponses.filter(({ response }) => (response === null || response === void 0 ? void 0 : response.code) !== EnumStatusCode.OK); if (failedSubgraphDeleteResponses.length > 0) { return { error: new Error(failedSubgraphDeleteResponses.map(({ response }) => { var _a; return (_a = response === null || response === void 0 ? void 0 : response.details) !== null && _a !== void 0 ? _a : 'Unknown error occurred.'; }).join('. ')), }; } const federatedGraphDeleteResponse = await client.platform.deleteFederatedGraph({ name: graphData.graph.name, namespace: graphData.graph.namespace, }, { headers: getBaseHeaders(), }); switch ((_a = federatedGraphDeleteResponse.response) === null || _a === void 0 ? void 0 : _a.code) { case EnumStatusCode.OK: { return { error: null, }; } default: { return { error: new Error((_c = (_b = federatedGraphDeleteResponse.response) === null || _b === void 0 ? void 0 : _b.details) !== null && _c !== void 0 ? _c : 'Unknown error occurred.'), }; } } } /** * Creates federated graph using default [name] and [namespace], with pre-defined * [labelMatcher] which identify the graph as _demo_. */ export async function createFederatedGraph(client, options) { var _a, _b, _c; const createFedGraphResponse = await client.platform.createFederatedGraph({ name: options.name, namespace: options.namespace, routingUrl: options.routingUrl.toString(), labelMatchers: [options.labelMatcher], }, { headers: getBaseHeaders(), }); switch ((_a = createFedGraphResponse.response) === null || _a === void 0 ? void 0 : _a.code) { case EnumStatusCode.OK: { return { error: null }; } default: { return { error: new Error((_c = (_b = createFedGraphResponse.response) === null || _b === void 0 ? void 0 : _b.details) !== null && _c !== void 0 ? _c : 'An unknown error occured'), }; } } } //# sourceMappingURL=api.js.map