wgc
Version:
The official CLI tool to manage the GraphQL Federation Platform Cosmo
54 lines • 3.4 kB
JavaScript
import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb';
import { z } from 'zod';
import { getBaseHeaders } from '../../../core/config.js';
export const registerSubgraphVerifySchemaChangesTool = ({ server, opts }) => {
server.tool('verify_subgraph_schema_changes', 'When making changes to a Subgraph Schema, this command can validate if the schema is valid GraphQL SDL, if it composes with all other subgraphs into a valid supergraph, and if there are any breaking changes.', {
name: z.string().describe('The name of the subgraph'),
namespace: z.string().optional().describe('The namespace of the subgraph'),
schema: z.string().describe('The new schema SDL to check'),
delete: z.boolean().optional().describe('Run checks in case the subgraph should be deleted'),
skipTrafficCheck: z.boolean().optional().describe('Skip checking for client traffic'),
}, async (params) => {
var _a, _b, _c, _d, _e, _f, _g;
const resp = await opts.client.platform.checkSubgraphSchema({
subgraphName: params.name,
namespace: params.namespace,
schema: new Uint8Array(Buffer.from(params.schema)),
delete: params.delete,
skipTrafficCheck: params.skipTrafficCheck,
}, {
headers: getBaseHeaders(),
});
return {
content: [
{
type: 'text',
text: JSON.stringify({
...resp,
isCheckSuccessful: isCheckSuccessful({
isComposable: resp.compositionErrors.length === 0,
isBreaking: resp.breakingChanges.length > 0,
hasClientTraffic: ((_b = (_a = resp.operationUsageStats) === null || _a === void 0 ? void 0 : _a.totalOperations) !== null && _b !== void 0 ? _b : 0) > 0 &&
((_d = (_c = resp.operationUsageStats) === null || _c === void 0 ? void 0 : _c.totalOperations) !== null && _d !== void 0 ? _d : 0) !==
((_f = (_e = resp.operationUsageStats) === null || _e === void 0 ? void 0 : _e.safeOperations) !== null && _f !== void 0 ? _f : 0),
hasLintErrors: resp.lintErrors.length > 0,
hasGraphPruningErrors: resp.graphPruneErrors.length > 0,
clientTrafficCheckSkipped: resp.clientTrafficCheckSkipped === true,
hasProposalMatchError: ((_g = resp.response) === null || _g === void 0 ? void 0 : _g.code) === EnumStatusCode.ERR_SCHEMA_MISMATCH_WITH_APPROVED_PROPOSAL,
}),
}, null, 2),
},
],
};
});
};
const isCheckSuccessful = ({ isComposable, isBreaking, hasClientTraffic, hasLintErrors, hasGraphPruningErrors, clientTrafficCheckSkipped, hasProposalMatchError, }) => {
return (isComposable &&
// If no breaking changes found
// OR Breaking changes are found, but no client traffic is found and traffic check is not skipped
(!isBreaking || (isBreaking && !hasClientTraffic && !clientTrafficCheckSkipped)) &&
!hasLintErrors &&
!hasGraphPruningErrors &&
!hasProposalMatchError);
};
//# sourceMappingURL=subgraph-verify-schema-changes.js.map