wgc
Version:
The official CLI tool to manage the GraphQL Federation Platform Cosmo
153 lines • 7.06 kB
JavaScript
import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb';
import Table from 'cli-table3';
import pc from 'picocolors';
export const handleCompositionResult = ({ responseCode, responseDetails, compositionErrors, compositionWarnings, deploymentErrors, spinner, successMessage, subgraphCompositionBaseErrorMessage, subgraphCompositionDetailedErrorMessage, deploymentErrorMessage, defaultErrorMessage, shouldOutputJson, suppressWarnings, failOnCompositionError, failOnCompositionErrorMessage, failOnAdmissionWebhookError, failOnAdmissionWebhookErrorMessage, }) => {
switch (responseCode) {
case EnumStatusCode.OK: {
if (shouldOutputJson) {
const successMessageJson = {
status: 'success',
message: successMessage,
compositionErrors,
deploymentErrors,
};
if (!suppressWarnings) {
successMessageJson.compositionWarnings = compositionWarnings;
}
console.log(JSON.stringify(successMessageJson));
}
else {
spinner.succeed(successMessage);
}
break;
}
case EnumStatusCode.ERR_SUBGRAPH_COMPOSITION_FAILED: {
if (shouldOutputJson) {
const compositionFailedMessageJson = {
status: 'error',
message: subgraphCompositionBaseErrorMessage,
compositionErrors,
deploymentErrors,
};
if (!suppressWarnings) {
compositionFailedMessageJson.compositionWarnings = compositionWarnings;
}
console.log(JSON.stringify(compositionFailedMessageJson));
}
else {
spinner.fail(subgraphCompositionBaseErrorMessage);
const compositionErrorsTable = new Table({
head: [
pc.bold(pc.white('FEDERATED_GRAPH_NAME')),
pc.bold(pc.white('NAMESPACE')),
pc.bold(pc.white('FEATURE_FLAG')),
pc.bold(pc.white('ERROR_MESSAGE')),
],
colWidths: [30, 30, 30, 120],
wordWrap: true,
});
console.log(pc.yellow(subgraphCompositionDetailedErrorMessage));
for (const compositionError of compositionErrors) {
compositionErrorsTable.push([
compositionError.federatedGraphName,
compositionError.namespace,
compositionError.featureFlag || '-',
compositionError.message,
]);
}
// Don't exit here with 1 because the change was still applied
console.log(compositionErrorsTable.toString());
}
if (failOnCompositionError) {
console.log(pc.red(pc.bold(failOnCompositionErrorMessage || 'The command failed due to composition errors.')));
throw new Error(failOnCompositionErrorMessage || 'The command failed due to composition errors.');
}
break;
}
case EnumStatusCode.ERR_DEPLOYMENT_FAILED: {
if (shouldOutputJson) {
const deploymentFailedMessageJson = {
status: 'error',
message: deploymentErrorMessage,
compositionErrors,
deploymentErrors,
};
if (!suppressWarnings) {
deploymentFailedMessageJson.compositionWarnings = compositionWarnings;
}
console.log(JSON.stringify(deploymentFailedMessageJson));
}
else {
spinner.warn(deploymentErrorMessage);
const deploymentErrorsTable = new Table({
head: [
pc.bold(pc.white('FEDERATED_GRAPH_NAME')),
pc.bold(pc.white('NAMESPACE')),
pc.bold(pc.white('ERROR_MESSAGE')),
],
colWidths: [30, 30, 120],
wordWrap: true,
});
for (const deploymentError of deploymentErrors) {
deploymentErrorsTable.push([
deploymentError.federatedGraphName,
deploymentError.namespace,
deploymentError.message,
]);
}
// Don't exit here with 1 because the change was still applied
console.log(deploymentErrorsTable.toString());
}
if (failOnAdmissionWebhookError) {
console.log(pc.red(pc.bold(failOnAdmissionWebhookErrorMessage || 'The command failed due to admission webhook errors.')));
throw new Error(failOnAdmissionWebhookErrorMessage || 'The command failed due to admission webhook errors.');
}
break;
}
default: {
if (shouldOutputJson) {
const defaultErrorMessageJson = {
status: 'error',
message: defaultErrorMessage,
compositionErrors,
deploymentErrors,
details: responseDetails,
};
if (!suppressWarnings) {
defaultErrorMessageJson.compositionWarnings = compositionWarnings;
}
console.log(JSON.stringify(defaultErrorMessageJson));
}
else {
spinner.fail(defaultErrorMessage);
if (responseDetails) {
console.log(pc.red(pc.bold(responseDetails)));
}
}
throw new Error(defaultErrorMessage);
}
}
if (!shouldOutputJson && !suppressWarnings && compositionWarnings.length > 0) {
const compositionWarningsTable = new Table({
head: [
pc.bold(pc.white('FEDERATED_GRAPH_NAME')),
pc.bold(pc.white('NAMESPACE')),
pc.bold(pc.white('FEATURE_FLAG')),
pc.bold(pc.white('WARNING_MESSAGE')),
],
colWidths: [30, 30, 30, 120],
wordWrap: true,
});
console.log(pc.yellow(`The following warnings were produced while composing the federated graph:`));
for (const compositionWarning of compositionWarnings) {
compositionWarningsTable.push([
compositionWarning.federatedGraphName,
compositionWarning.namespace,
compositionWarning.featureFlag || '-',
compositionWarning.message,
]);
}
console.log(compositionWarningsTable.toString());
}
};
//# sourceMappingURL=handle-composition-result.js.map