@graphql-codegen/c-sharp-operations
Version:
GraphQL Code Generator plugin for generating CSharp code based on GraphQL operations
49 lines (48 loc) • 2.17 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.CSharpOperationsVisitor = exports.validate = exports.addToSchema = exports.plugin = void 0;
const tslib_1 = require("tslib");
const path_1 = require("path");
const graphql_1 = require("graphql");
const graphql_tag_1 = tslib_1.__importDefault(require("graphql-tag"));
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
const visitor_js_1 = require("./visitor.js");
Object.defineProperty(exports, "CSharpOperationsVisitor", { enumerable: true, get: function () { return visitor_js_1.CSharpOperationsVisitor; } });
const plugin = (schema, documents, config) => {
const schemaAST = (0, plugin_helpers_1.getCachedDocumentNodeFromSchema)(schema);
const allAst = (0, graphql_1.concatAST)(documents.map(v => v.document).concat(schemaAST));
const allFragments = [
...allAst.definitions.filter(d => d.kind === graphql_1.Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({
node: fragmentDef,
name: fragmentDef.name.value,
onType: fragmentDef.typeCondition.name.value,
isExternal: false,
})),
...(config.externalFragments || []),
];
const visitor = new visitor_js_1.CSharpOperationsVisitor(schema, allFragments, config, documents);
const visitorResult = (0, plugin_helpers_1.oldVisit)(allAst, { leave: visitor });
const imports = visitor.getCSharpImports();
const openNameSpace = `namespace ${visitor.config.namespaceName} {`;
return {
prepend: [],
content: [
imports,
openNameSpace,
...visitorResult.definitions.filter(t => typeof t === 'string'),
'}',
]
.filter(a => a)
.join('\n'),
};
};
exports.plugin = plugin;
exports.addToSchema = (0, graphql_tag_1.default) `
directive @namedClient(name: String!) on OBJECT | FIELD
`;
const validate = async (schema, documents, config, outputFile) => {
if ((0, path_1.extname)(outputFile) !== '.cs') {
throw new Error(`Plugin "c-sharp-operations" requires extension to be ".cs"!`);
}
};
exports.validate = validate;
;