@graphql-codegen/typescript-apollo-angular
Version:
GraphQL Code Generator plugin for generating ready-to-use Angular Components based on GraphQL operations
45 lines (44 loc) • 2.03 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApolloAngularVisitor = exports.validate = exports.addToSchema = exports.plugin = void 0;
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
const graphql_1 = require("graphql");
const visitor_js_1 = require("./visitor.js");
Object.defineProperty(exports, "ApolloAngularVisitor", { enumerable: true, get: function () { return visitor_js_1.ApolloAngularVisitor; } });
const path_1 = require("path");
const plugin = (schema, documents, config) => {
const allAst = (0, graphql_1.concatAST)(documents.map(v => v.document));
const operations = allAst.definitions.filter(d => d.kind === graphql_1.Kind.OPERATION_DEFINITION);
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.ApolloAngularVisitor(schema, allFragments, operations, config, documents);
const visitorResult = (0, plugin_helpers_1.oldVisit)(allAst, { leave: visitor });
return {
prepend: visitor.getImports(),
content: [
visitor.fragments,
...visitorResult.definitions.filter(t => typeof t === 'string'),
config.sdkClass ? visitor.sdkClass : null,
]
.filter(a => a)
.join('\n'),
};
};
exports.plugin = plugin;
exports.addToSchema = (0, graphql_1.parse)(`
directive @NgModule(module: String!) on OBJECT | FIELD
directive @namedClient(name: String!) on OBJECT | FIELD
`);
const validate = async (schema, documents, config, outputFile) => {
if ((0, path_1.extname)(outputFile) !== '.ts') {
throw new Error(`Plugin "apollo-angular" requires extension to be ".ts"!`);
}
};
exports.validate = validate;
;