apollo-angular
Version:
Use your GraphQL data in your Angular app, with the Apollo Client
115 lines (114 loc) • 4.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.factory = factory;
exports.createDependenciesMap = createDependenciesMap;
const path_1 = require("path");
const schematics_1 = require("@angular-devkit/schematics");
const tasks_1 = require("@angular-devkit/schematics/tasks");
const utility_1 = require("@schematics/angular/utility");
const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils");
const util_1 = require("@schematics/angular/utility/standalone/util");
const ast_cjs_1 = require("../utils/ast.cjs");
const index_cjs_1 = require("../utils/index.cjs");
function factory(options) {
return (0, schematics_1.chain)([
addDependencies(options),
addSetupFiles(options),
importHttpClient(options),
importSetup(options),
]);
}
function createDependenciesMap(options) {
return {
'apollo-angular': '^9.0.0',
'@apollo/client': '^4.0.1',
graphql: `^${options.graphql ?? '16.0.0'}`,
};
}
/**
* Add all necessary node packages
* as dependencies in the package.json
* and installs them by running `npm install`.
*/
function addDependencies(options) {
return (host, context) => {
const packageJsonPath = 'package.json';
const packageJson = (0, index_cjs_1.getJsonFile)(host, packageJsonPath);
packageJson.dependencies = packageJson.dependencies || {};
const dependenciesMap = createDependenciesMap(options);
for (const dependency in dependenciesMap) {
if (dependenciesMap.hasOwnProperty(dependency)) {
const version = dependenciesMap[dependency];
if (!packageJson.dependencies[dependency]) {
packageJson.dependencies[dependency] = version;
}
}
}
// save the changed file
host.overwrite(packageJsonPath, JSON.stringify(packageJson, null, 2));
// schedule `npm install`
context.addTask(new tasks_1.NodePackageInstallTask());
return host;
};
}
function addSetupFiles(options) {
return async (host) => {
const mainPath = await (0, util_1.getMainFilePath)(host, options.project);
const appModuleDirectory = (0, path_1.dirname)(mainPath) + '/app';
if ((0, ng_ast_utils_1.isStandaloneApp)(host, mainPath)) {
const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files/standalone'), [
(0, schematics_1.template)({
endpoint: options.endpoint,
}),
(0, schematics_1.move)(appModuleDirectory),
]);
return (0, schematics_1.mergeWith)(templateSource);
}
else {
const appModulePath = (0, ng_ast_utils_1.getAppModulePath)(host, mainPath);
const appModuleDirectory = (0, path_1.dirname)(appModulePath);
const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files/module'), [
(0, schematics_1.template)({
endpoint: options.endpoint,
}),
(0, schematics_1.move)(appModuleDirectory),
]);
return (0, schematics_1.mergeWith)(templateSource);
}
};
}
function importSetup(options) {
return async (host) => {
const mainPath = await (0, util_1.getMainFilePath)(host, options.project);
if ((0, ng_ast_utils_1.isStandaloneApp)(host, mainPath)) {
return (0, utility_1.addRootProvider)(options.project, ({ code, external }) => {
return code `${external('provideApollo', 'apollo-angular')}(() => {
const httpLink = ${external('inject', '@angular/core')}(${external('HttpLink', 'apollo-angular/http')});
return {
link: httpLink.create({
uri: '<%= endpoint %>',
}),
cache: new ${external('InMemoryCache', '@apollo/client')}(),
};
})`;
});
}
else {
return (0, ast_cjs_1.addModuleImportToRootModule)(host, 'GraphQLModule', './graphql.module', options.project);
}
};
}
function importHttpClient(options) {
return async (host) => {
const mainPath = await (0, util_1.getMainFilePath)(host, options.project);
if ((0, ng_ast_utils_1.isStandaloneApp)(host, mainPath)) {
return (0, utility_1.addRootProvider)(options.project, ({ code, external }) => {
return code `${external('provideHttpClient', '@angular/common/http')}()`;
});
}
else {
return (0, ast_cjs_1.addModuleImportToRootModule)(host, 'HttpClientModule', '@angular/common/http', options.project);
}
};
}
//# sourceMappingURL=index.cjs.map