Skip to content

Commit dd831a7

Browse files
authored
Drop Snapshot transform in favor of a new Snapshot plugin (#4638)
* Drop Snapshot transform and introduce a new Snapshot plugin * Respect if
1 parent 6d40631 commit dd831a7

File tree

17 files changed

+199
-2129
lines changed

17 files changed

+199
-2129
lines changed

.changeset/little-plants-admire.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-mesh/plugin-snapshot': patch
3+
'@graphql-mesh/types': patch
4+
---
5+
6+
Drop Snapshot transform in favor of Snapshot plugin

examples/soap-country-info/.meshrc.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ sources:
33
handler:
44
soap:
55
wsdl: http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL
6-
transforms:
7-
- snapshot:
8-
if: "process.env.NODE_ENV != 'production'"
9-
apply:
10-
- Query.*
11-
- Mutation.*
12-
outputDir: snapshots
6+
7+
plugins:
8+
- snapshot:
9+
if: "process.env.NODE_ENV != 'production'"
10+
apply:
11+
- http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso
12+
outputDir: snapshots
1313

1414
documents:
1515
- list-of-languages-by-name.graphql

examples/soap-country-info/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"dependencies": {
1010
"@graphql-mesh/cli": "0.78.29",
1111
"@graphql-mesh/soap": "0.14.19",
12-
"@graphql-mesh/transform-snapshot": "0.14.65",
12+
"@graphql-mesh/plugin-snapshot": "0.0.0",
1313
"graphql": "16.6.0"
1414
}
1515
}

packages/transforms/snapshot/package.json renamed to packages/plugins/snapshot/package.json

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "@graphql-mesh/transform-snapshot",
3-
"version": "0.14.65",
2+
"name": "@graphql-mesh/plugin-snapshot",
3+
"version": "0.0.0",
44
"sideEffects": false,
55
"main": "dist/index.js",
66
"module": "dist/index.mjs",
@@ -22,22 +22,18 @@
2222
"repository": {
2323
"type": "git",
2424
"url": "Urigo/graphql-mesh",
25-
"directory": "packages/transforms/snapshot"
25+
"directory": "packages/plugins/snapshot"
2626
},
2727
"peerDependencies": {
2828
"graphql": "*"
2929
},
30-
"devDependencies": {
31-
"graphql-fields": "2.0.3"
32-
},
3330
"dependencies": {
31+
"@graphql-mesh/string-interpolation": "0.3.2",
32+
"@graphql-mesh/utils": "0.41.19",
3433
"@graphql-mesh/cross-helpers": "0.2.6",
3534
"@graphql-mesh/types": "0.84.8",
36-
"@graphql-mesh/utils": "0.41.19",
37-
"@graphql-tools/resolvers-composition": "6.5.6",
38-
"@graphql-tools/schema": "9.0.4",
39-
"@graphql-tools/utils": "8.12.0",
40-
"object-hash": "3.0.0",
35+
"@whatwg-node/fetch": "0.4.7",
36+
"minimatch": "5.1.0",
4137
"tslib": "^2.4.0"
4238
},
4339
"publishConfig": {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { hashObject } from '@graphql-mesh/string-interpolation';
2+
import { MeshPlugin, MeshPluginOptions, YamlConfig } from '@graphql-mesh/types';
3+
import { getHeadersObj, pathExists, writeJSON } from '@graphql-mesh/utils';
4+
import minimatch from 'minimatch';
5+
import { fs, path, process } from '@graphql-mesh/cross-helpers';
6+
import { Response } from '@whatwg-node/fetch';
7+
8+
function calculateCacheKey(url: string, options: RequestInit) {
9+
return hashObject({
10+
url,
11+
options,
12+
});
13+
}
14+
15+
interface SnapshotEntry {
16+
text: string;
17+
headersObj: Record<string, string>;
18+
status: number;
19+
statusText: string;
20+
}
21+
22+
export default function useSnapshot(
23+
pluginOptions: MeshPluginOptions<YamlConfig.SnapshotPluginConfig>
24+
): MeshPlugin<any> {
25+
if (typeof pluginOptions.if === 'boolean') {
26+
if (!pluginOptions.if) {
27+
return {};
28+
}
29+
}
30+
if (typeof pluginOptions.if === 'string') {
31+
// eslint-disable-next-line no-new-func
32+
if (new Function('return ' + pluginOptions.if, 'env')(process.env)) {
33+
return {};
34+
}
35+
}
36+
const matches = pluginOptions.apply.map(glob => new minimatch.Minimatch(glob));
37+
const snapshotsDir = pluginOptions.outputDir || '__snapshots__';
38+
return {
39+
async onFetch({ url, options, setFetchFn }) {
40+
if (matches.some(matcher => matcher.match(url))) {
41+
const snapshotFileName = calculateCacheKey(url, options);
42+
const snapshotPath = path.join(pluginOptions.baseDir, snapshotsDir, `${snapshotFileName}.json`);
43+
if (await pathExists(snapshotPath)) {
44+
setFetchFn(async () => {
45+
const snapshotFile = await fs.promises.readFile(snapshotPath, 'utf-8');
46+
const snapshot: SnapshotEntry = JSON.parse(snapshotFile);
47+
return new Response(snapshot.text, {
48+
headers: snapshot.headersObj,
49+
status: snapshot.status,
50+
statusText: snapshot.statusText,
51+
});
52+
});
53+
return () => {};
54+
}
55+
return async ({ response, setResponse }) => {
56+
const snapshot: SnapshotEntry = {
57+
text: await response.text(),
58+
headersObj: getHeadersObj(response.headers),
59+
status: response.status,
60+
statusText: response.statusText,
61+
};
62+
await writeJSON(snapshotPath, snapshot);
63+
setResponse(
64+
new Response(snapshot.text, {
65+
headers: snapshot.headersObj,
66+
status: snapshot.status,
67+
statusText: snapshot.statusText,
68+
})
69+
);
70+
};
71+
}
72+
return () => {};
73+
},
74+
};
75+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
extend type Plugin {
2+
"""
3+
Configuration for Snapshot extension
4+
"""
5+
snapshot: SnapshotPluginConfig
6+
}
7+
8+
type SnapshotPluginConfig @md {
9+
"""
10+
Expression for when to activate this extension.
11+
Value can be a valid JS expression string or a boolean
12+
"""
13+
if: If
14+
"""
15+
HTTP URL pattern to be applied
16+
For example;
17+
apply:
18+
- http://my-remote-api.com/* \<- * will apply this extension to all paths of remote API
19+
"""
20+
apply: [String!]!
21+
"""
22+
Path to the directory of the generated snapshot files
23+
"""
24+
outputDir: String!
25+
}
26+
27+
union If = String | Boolean

0 commit comments

Comments
 (0)