UNPKG

@graphql-hive/cli

Version:

A CLI util to manage and control your GraphQL Hive

57 lines 2.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadOperations = loadOperations; const fs_1 = require("fs"); const path_1 = require("path"); const graphql_1 = require("graphql"); const core_1 = require("@graphql-hive/core"); const code_file_loader_1 = require("@graphql-tools/code-file-loader"); const graphql_file_loader_1 = require("@graphql-tools/graphql-file-loader"); const load_1 = require("@graphql-tools/load"); async function loadOperations(file, options) { var _a; const shouldNormalize = (_a = options === null || options === void 0 ? void 0 : options.normalize) !== null && _a !== void 0 ? _a : true; const pluckModules = options === null || options === void 0 ? void 0 : options.pluckModules; const pluckGlobalGqlIdentifierName = options === null || options === void 0 ? void 0 : options.pluckGlobalGqlIdentifierName; if (file.toLowerCase().endsWith('.json')) { const output = JSON.parse(await fs_1.promises.readFile(file, 'utf8')); const operations = []; for (const operationHash in output) { const content = output[operationHash]; const doc = (0, graphql_1.parse)(content); operations.push({ operationHash, content: shouldNormalize ? (0, core_1.normalizeOperation)({ document: doc, hideLiterals: true, removeAliases: true, }) : content, }); } return operations; } const cwd = process.cwd(); const sources = await (0, load_1.loadDocuments)(file, { cwd, loaders: [ new code_file_loader_1.CodeFileLoader({ pluckConfig: { modules: pluckModules, globalGqlIdentifierName: pluckGlobalGqlIdentifierName, }, }), new graphql_file_loader_1.GraphQLFileLoader(), ], }); return sources.map(source => ({ content: (0, core_1.normalizeOperation)({ document: source.document, hideLiterals: false, removeAliases: false, }), location: source.location ? (0, path_1.relative)(cwd, source.location) : undefined, })); } //# sourceMappingURL=operations.js.map