UNPKG

apollo-angular

Version:

Use your GraphQL data in your Angular app, with the Apollo Client

84 lines (83 loc) 3.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.factory = factory; exports.createDependenciesMap = createDependenciesMap; 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 index_cjs_1 = require("../utils/index.cjs"); function factory(options) { return (0, schematics_1.chain)([ assertStandaloneApp(options), addDependencies(options), addProvideHttpClient(options), addProvideApollo(options), ]); } function createDependenciesMap(options) { return { 'apollo-angular': '^9.0.0', '@apollo/client': '^4.0.1', graphql: `^${options.graphql ?? '16.0.0'}`, }; } function assertStandaloneApp(options) { return async (host) => { const mainPath = await (0, util_1.getMainFilePath)(host, options.project); if (!(0, ng_ast_utils_1.isStandaloneApp)(host, mainPath)) { throw new Error('Only standalone application are supported'); } return host; }; } /** * 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 addProvideApollo(options) { return async () => { 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')}(), }; })`; }); }; } function addProvideHttpClient(options) { return async () => { return (0, utility_1.addRootProvider)(options.project, ({ code, external }) => { return code `${external('provideHttpClient', '@angular/common/http')}()`; }); }; } //# sourceMappingURL=index.cjs.map