UNPKG

3.54 kBJavaScriptView Raw
1'use strict';
2
3function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
5function _interopNamespace(e) {
6 if (e && e.__esModule) { return e; } else {
7 var n = {};
8 if (e) {
9 Object.keys(e).forEach(function (k) {
10 var d = Object.getOwnPropertyDescriptor(e, k);
11 Object.defineProperty(n, k, d.get ? d : {
12 enumerable: true,
13 get: function () {
14 return e[k];
15 }
16 });
17 });
18 }
19 n['default'] = e;
20 return n;
21 }
22}
23
24const schema = require('@graphql-tools/schema');
25const resolversComposition = require('@graphql-tools/resolvers-composition');
26const path = require('path');
27const fsExtra = require('fs-extra');
28const objectHash = _interopDefault(require('object-hash'));
29const utils = require('@graphql-mesh/utils');
30
31function computeSnapshotFilePath(options) {
32 const argsHash = objectHash(options.args, { ignoreUnknown: true });
33 const fileName = [options.typeName, options.fieldName, argsHash].join('_') + '.json';
34 const absoluteOutputDir = path.isAbsolute(options.outputDir) ? options.outputDir : path.join(process.cwd(), options.outputDir);
35 return path.join(absoluteOutputDir, fileName);
36}
37
38const writeFile = async (path, json) => {
39 try {
40 await fsExtra.ensureFile(path);
41 await fsExtra.writeJSON(path, json, {
42 spaces: 2,
43 });
44 }
45 catch (e) {
46 console.error(`Snapshot cannot saved to ${path}: ${e.message}`);
47 }
48};
49class SnapshotTransform {
50 constructor(options) {
51 this.options = options;
52 this.noWrap = true;
53 }
54 transformSchema(schema$1) {
55 const { config } = this.options;
56 // TODO: Needs to be changed!
57 const configIf = 'if' in config ? (typeof config.if === 'boolean' ? config.if : config.if && eval(config.if)) : true;
58 if (configIf) {
59 const resolvers = utils.extractResolvers(schema$1);
60 const resolversComposition$1 = {};
61 const outputDir = path.isAbsolute(config.outputDir) ? config.outputDir : path.join(process.cwd(), config.outputDir);
62 const snapshotComposition = next => async (root, args, context, info) => {
63 const snapshotFilePath = computeSnapshotFilePath({
64 typeName: info.parentType.name,
65 fieldName: info.fieldName,
66 args,
67 outputDir,
68 });
69 if (snapshotFilePath in require.cache || (await fsExtra.pathExists(snapshotFilePath))) {
70 return new Promise(function (resolve) { resolve(_interopNamespace(require(snapshotFilePath))); });
71 }
72 const result = await next(root, args, context, info);
73 await writeFile(snapshotFilePath, result);
74 return result;
75 };
76 for (const field of config.apply) {
77 resolversComposition$1[field] = snapshotComposition;
78 }
79 const composedResolvers = resolversComposition.composeResolvers(resolvers, resolversComposition$1);
80 return schema.addResolversToSchema({
81 schema: schema$1,
82 resolvers: composedResolvers,
83 updateResolversInPlace: true,
84 });
85 }
86 return schema$1;
87 }
88}
89
90module.exports = SnapshotTransform;
91//# sourceMappingURL=index.cjs.js.map