@graphql-mesh/incontext-sdk-codegen
Version:
75 lines (73 loc) • 3.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.codegenPlugin = void 0;
const graphql_scalars_1 = require("graphql-scalars");
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
const cross_helpers_1 = require("@graphql-mesh/cross-helpers");
const fusion_runtime_1 = require("@graphql-mesh/fusion-runtime");
const transport_common_1 = require("@graphql-mesh/transport-common");
const utils_1 = require("@graphql-mesh/utils");
const utils_2 = require("@graphql-tools/utils");
const wrap_1 = require("@graphql-tools/wrap");
const generateIncontextSDKTypes_js_1 = require("./generateIncontextSDKTypes.js");
exports.codegenPlugin = {
async plugin(unifiedGraph, _documents, config, info) {
const outputFileDirName = info.outputFile ? cross_helpers_1.path.dirname(info.outputFile) : process.cwd();
const { inContextSDK, getSubschema } = (0, fusion_runtime_1.handleFederationSupergraph)({
unifiedGraph,
getUnifiedGraphSDL() {
return (0, transport_common_1.defaultPrintFn)((0, plugin_helpers_1.getCachedDocumentNodeFromSchema)(unifiedGraph));
},
onSubgraphExecute(subgraphName, executionRequest) {
return {};
},
});
const imports = new Set();
const identifiers = new Set();
for (const sourceName in inContextSDK) {
const subschemaConfig = getSubschema(sourceName);
const transformedSchema = (0, utils_2.mapSchema)((0, wrap_1.wrapSchema)(subschemaConfig), {
[utils_2.MapperKind.ROOT_FIELD](_, fieldName) {
if (fieldName === '_entities') {
return null;
}
},
});
const scalarTypes = {};
for (const scalarName in graphql_scalars_1.resolvers) {
if (transformedSchema.getType(scalarName)) {
const codegenScalarType = graphql_scalars_1.resolvers[scalarName]?.extensions?.codegenScalarType;
if (typeof codegenScalarType === 'string') {
scalarTypes[scalarName] = codegenScalarType;
}
}
}
const typesResult = await (0, generateIncontextSDKTypes_js_1.generateIncontextSDKTypes)({
schema: transformedSchema,
name: sourceName,
contextVariables: {},
flattenTypes: false,
codegenConfig: {
scalars: scalarTypes,
skipTypename: false,
nonOptionalTypename: true,
useTypeImports: true,
...config,
},
unifiedContextIdentifier: '{}',
});
identifiers.add(typesResult.identifier);
const targetPath = `./subgraphs/${sourceName}`;
imports.add(`import type { ${typesResult.identifier} } from '${targetPath}';`);
await (0, utils_1.writeFile)(cross_helpers_1.path.join(outputFileDirName, targetPath + '.ts'), typesResult.codeAst);
}
const unifiedContextType = (0, generateIncontextSDKTypes_js_1.generateUnifiedContextTypeFromIdentifiers)([...identifiers]);
const content = `
${[...imports].join('\n')}
${unifiedContextType}
`;
return {
content,
};
},
};