UNPKG

wgc

Version:

The official CLI tool to manage the GraphQL Federation Platform Cosmo

100 lines 5.03 kB
import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb'; import { z } from 'zod'; import { getBaseHeaders } from '../../../core/config.js'; export const registerGetSubgraphsTool = ({ server, opts }) => { server.registerTool('get_subgraphs', { title: 'Get Subgraphs', description: 'Get details for one or more subgraphs, including the SDL/GraphQL Schema for each.', inputSchema: { names: z.array(z.string()).describe('The names of the subgraphs'), namespace: z.string().optional().describe('The namespace of the subgraphs'), }, }, async (params) => { var _a, _b, _c, _d, _e; const results = []; const errors = []; // Fetch details for all subgraphs first to potentially leverage batching if the API supports it later. // Currently, it iterates and fetches one by one. const subgraphDetailsPromises = params.names.map((name) => opts.client.platform .getSubgraphs({ query: name, namespace: params.namespace, limit: 1, offset: 0, }, { headers: getBaseHeaders(), }) .then((resp) => ({ name, resp }))); const subgraphDetailsResponses = await Promise.allSettled(subgraphDetailsPromises); const foundSubgraphs = {}; for (const result of subgraphDetailsResponses) { if (result.status === 'rejected') { // Handle potential network or client errors errors.push(`Failed to initiate fetch for a subgraph: ${result.reason}`); continue; } const { name, resp } = result.value; if (((_a = resp.response) === null || _a === void 0 ? void 0 : _a.code) !== EnumStatusCode.OK) { errors.push(`Could not fetch subgraph '${name}': ${((_b = resp.response) === null || _b === void 0 ? void 0 : _b.details) || 'Unknown error'}`); continue; } if (resp.graphs.length === 0) { errors.push(`No subgraph found with name '${name}'${params.namespace ? ` in namespace '${params.namespace}'` : ''}.`); continue; } foundSubgraphs[name] = resp.graphs[0]; } // Fetch SDLs for the found subgraphs const sdlPromises = Object.entries(foundSubgraphs).map(([name, graph]) => opts.client.platform .getLatestSubgraphSDL({ name, namespace: params.namespace, // Use the common namespace }, { headers: getBaseHeaders(), }) .then((sdlResp) => ({ name, sdlResp, graph }))); const sdlResponses = await Promise.allSettled(sdlPromises); for (const result of sdlResponses) { if (result.status === 'rejected') { // Find the original name associated with this failed promise if possible // This is tricky as the error might not contain the name directly. // We might need a different approach if precise error mapping per subgraph is critical here. errors.push(`Failed to fetch SDL for a subgraph: ${result.reason}`); continue; // Skip adding this subgraph to results } const { name, sdlResp, graph } = result.value; const out = { id: graph.id, name: graph.name, labels: graph.labels, routingURL: graph.routingURL, lastUpdate: graph.lastUpdatedAt, sdl: ((_c = sdlResp.response) === null || _c === void 0 ? void 0 : _c.code) === EnumStatusCode.OK ? sdlResp.sdl : undefined, }; results.push(out); if (((_d = sdlResp.response) === null || _d === void 0 ? void 0 : _d.code) !== EnumStatusCode.OK) { errors.push(`Could not fetch SDL for subgraph '${name}': ${((_e = sdlResp.response) === null || _e === void 0 ? void 0 : _e.details) || 'Unknown error'}`); // Keep the subgraph in results, but SDL will be undefined } } // Construct the final output message let outputText = ''; if (results.length > 0) { outputText += `Found details for ${results.length} subgraph(s):\n${JSON.stringify(results, null, 2)}`; } if (errors.length > 0) { if (outputText.length > 0) { outputText += '\n\n'; // Add separation if there were results } outputText += `Encountered errors:\n- ${errors.join('\n- ')}`; } if (results.length === 0 && errors.length === 0) { outputText = 'No subgraphs found matching the provided names or an unexpected error occurred.'; } // Ensure the return type matches the expected ToolOutput structure return { content: [{ type: 'text', text: outputText }], }; }); }; //# sourceMappingURL=get-subgraphs.js.map