UNPKG

@graphql-tools/jest-transform

Version:

Jest Plugin to load and parse imported GraphQL files

32 lines (31 loc) • 1.02 kB
import fs from 'fs'; import loader from '@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 = loader.call({ cacheable() { }, query: config, resourcePath: filePath, fs, 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()); } export function process(...args) { return defaultTransformer().process(...args); } const transformerFactory = { process, }; export default transformerFactory;