UNPKG

@newmo/graphql-fake-server

Version:

GraphQL fake server for testing

39 lines 1.34 kB
import vm from "node:vm"; import { generateCode, getTypeInfos, normalizeConfig } from "@newmo/graphql-fake-core"; import { createLogger } from "./logger.js"; const cloneAsJSON = (obj) => { return JSON.parse(JSON.stringify(obj)); }; /** * Create mock object from schema * It supports @example directive * @param options */ export const createMock = async (options) => { const logger = createLogger(options.logLevel); try { const normalizedConfig = normalizeConfig({ maxFieldRecursionDepth: options.maxFieldRecursionDepth ?? 3, }); const typeInfos = getTypeInfos(normalizedConfig, options.schema); const code = generateCode({ ...normalizedConfig, outputType: "commonjs", }, typeInfos); logger.debug("Generated code:"); logger.debug(code); // execute code in vm and get all exports const exports = {}; vm.runInNewContext(code, { exports }); // Apollo Server does not support Function type in mock object const plainObject = cloneAsJSON(exports); logger.debug("Exports:"); logger.debug(JSON.stringify(plainObject, null, 2)); return plainObject; } catch (error) { logger.error(error); process.exit(1); } }; //# sourceMappingURL=createMock.js.map