UNPKG

@graphql-tools/jest-transform

Version:

Jest Plugin to load and parse imported GraphQL files

36 lines (35 loc) • 1.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.process = process; const tslib_1 = require("tslib"); const fs_1 = tslib_1.__importDefault(require("fs")); const webpack_loader_1 = tslib_1.__importDefault(require("@graphql-tools/webpack-loader")); class GraphQLTransformer { process(input, filePath, jestConfig) { const config = jestConfig.config.globals?.['graphql'] || {}; // call directly the webpack loader with a mocked context // as the loader leverages `this.cacheable()` const code = webpack_loader_1.default.call({ cacheable() { }, query: config, resourcePath: filePath, fs: fs_1.default, addDependency() { }, }, input); if (typeof code !== 'string') { throw new Error('@graphql-tools/jest-transform requires the webpack loader to return source synchronously'); } return { code }; } } let transformer; function defaultTransformer() { return transformer || (transformer = new GraphQLTransformer()); } function process(...args) { return defaultTransformer().process(...args); } const transformerFactory = { process, }; exports.default = transformerFactory;