Skip to content

Commit

Permalink
Fixes on snapshot plugin and types
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Oct 8, 2022
1 parent a5a4919 commit 6fb57d3
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 80 deletions.
6 changes: 6 additions & 0 deletions .changeset/afraid-tables-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-mesh/plugin-snapshot': patch
'@graphql-mesh/types': patch
---

Fixes
2 changes: 1 addition & 1 deletion examples/soap-country-info/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
snapshots
__snapshots__
2 changes: 1 addition & 1 deletion examples/soap-country-info/.meshrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins:
if: "process.env.NODE_ENV != 'production'"
apply:
- http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso
outputDir: snapshots
outputDir: __snapshots__

documents:
- list-of-languages-by-name.graphql
33 changes: 18 additions & 15 deletions packages/plugins/snapshot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function useSnapshot(
}
if (typeof pluginOptions.if === 'string') {
// eslint-disable-next-line no-new-func
if (new Function('return ' + pluginOptions.if, 'env')(process.env)) {
if (!new Function('env', 'return ' + pluginOptions.if)(process.env)) {
return {};
}
}
Expand All @@ -53,20 +53,23 @@ export default function useSnapshot(
return () => {};
}
return async ({ response, setResponse }) => {
const snapshot: SnapshotEntry = {
text: await response.text(),
headersObj: getHeadersObj(response.headers),
status: response.status,
statusText: response.statusText,
};
await writeJSON(snapshotPath, snapshot);
setResponse(
new Response(snapshot.text, {
headers: snapshot.headersObj,
status: snapshot.status,
statusText: snapshot.statusText,
})
);
const contentType = response.headers.get('content-type');
if (contentType.includes('json') || contentType.includes('text')) {
const snapshot: SnapshotEntry = {
text: await response.text(),
headersObj: getHeadersObj(response.headers),
status: response.status,
statusText: response.statusText,
};
await writeJSON(snapshotPath, snapshot, null, 2);
setResponse(
new Response(snapshot.text, {
headers: snapshot.headersObj,
status: snapshot.status,
statusText: snapshot.statusText,
})
);
}
};
}
return () => {};
Expand Down
70 changes: 35 additions & 35 deletions packages/types/src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -524,12 +524,12 @@
"responseCache": {
"$ref": "#/definitions/ResponseCacheConfig"
},
"statsd": {
"$ref": "#/definitions/StatsdPlugin"
},
"snapshot": {
"$ref": "#/definitions/SnapshotPluginConfig",
"description": "Configuration for Snapshot extension"
},
"statsd": {
"$ref": "#/definitions/StatsdPlugin"
}
}
},
Expand Down Expand Up @@ -2332,6 +2332,37 @@
},
"required": ["coordinate", "ttl"]
},
"SnapshotPluginConfig": {
"additionalProperties": false,
"type": "object",
"title": "SnapshotPluginConfig",
"properties": {
"if": {
"description": "Expression for when to activate this extension.\nValue can be a valid JS expression string or a boolean (Any of: String, Boolean)",
"anyOf": [
{
"type": "string"
},
{
"type": "boolean"
}
]
},
"apply": {
"type": "array",
"items": {
"type": "string"
},
"additionalItems": false,
"description": "HTTP URL pattern to be applied\nFor example;\n apply:\n - http://my-remote-api.com/* \\<- * will apply this extension to all paths of remote API"
},
"outputDir": {
"type": "string",
"description": "Path to the directory of the generated snapshot files"
}
},
"required": ["apply", "outputDir"]
},
"StatsdPlugin": {
"additionalProperties": false,
"type": "object",
Expand Down Expand Up @@ -2966,7 +2997,7 @@
"args": {
"type": "object",
"properties": {},
"description": "You need configure the arguments for that field;\n```yml\nargs:\n someArg: \"{root.someKeyValue}\"\n```"
"description": "You need configure the arguments for that field;\n```yaml\nargs:\n someArg: \"{root.someKeyValue}\"\n```"
}
},
"required": ["queryFieldName"]
Expand Down Expand Up @@ -3658,37 +3689,6 @@
"description": "Path to the SQL Dump file if you want to build a in-memory database"
}
}
},
"SnapshotPluginConfig": {
"additionalProperties": false,
"type": "object",
"title": "SnapshotPluginConfig",
"properties": {
"if": {
"description": "Expression for when to activate this extension.\nValue can be a valid JS expression string or a boolean (Any of: String, Boolean)",
"anyOf": [
{
"type": "string"
},
{
"type": "boolean"
}
]
},
"apply": {
"type": "array",
"items": {
"type": "string"
},
"additionalItems": false,
"description": "HTTP URL pattern to be applied\nFor example;\n apply:\n - http://my-remote-api.com/* \\<- * will apply this extension to all paths of remote API"
},
"outputDir": {
"type": "string",
"description": "Path to the directory of the generated snapshot files"
}
},
"required": ["apply", "outputDir"]
}
},
"title": "Config",
Expand Down
46 changes: 23 additions & 23 deletions packages/types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,7 @@ export interface ResolveReferenceObject {
queryFieldName: string;
/**
* You need configure the arguments for that field;
* ```yml
* ```yaml
* args:
* someArg: "{root.someKeyValue}"
* ```
Expand Down Expand Up @@ -1734,8 +1734,8 @@ export interface Plugin {
prometheus?: PrometheusConfig;
rateLimit?: RateLimitPluginConfig;
responseCache?: ResponseCacheConfig;
statsd?: StatsdPlugin;
snapshot?: SnapshotPluginConfig;
statsd?: StatsdPlugin;
[k: string]: any;
}
export interface MaskedErrorsPluginConfig {
Expand Down Expand Up @@ -1998,6 +1998,27 @@ export interface ResponseCacheTTLConfig {
coordinate: string;
ttl: number;
}
/**
* Configuration for Snapshot extension
*/
export interface SnapshotPluginConfig {
/**
* Expression for when to activate this extension.
* Value can be a valid JS expression string or a boolean (Any of: String, Boolean)
*/
if?: string | boolean;
/**
* HTTP URL pattern to be applied
* For example;
* apply:
* - http://my-remote-api.com/* \<- * will apply this extension to all paths of remote API
*/
apply: string[];
/**
* Path to the directory of the generated snapshot files
*/
outputDir: string;
}
export interface StatsdPlugin {
/**
* If you wish to disable introspection for logging (default: false)
Expand Down Expand Up @@ -2046,24 +2067,3 @@ export interface StatsdClientConfiguration {
export interface StatsdClientBufferHolder {
buffer: string;
}
/**
* Configuration for Snapshot extension
*/
export interface SnapshotPluginConfig {
/**
* Expression for when to activate this extension.
* Value can be a valid JS expression string or a boolean (Any of: String, Boolean)
*/
if?: string | boolean;
/**
* HTTP URL pattern to be applied
* For example;
* apply:
* - http://my-remote-api.com/* \<- * will apply this extension to all paths of remote API
*/
apply: string[];
/**
* Path to the directory of the generated snapshot files
*/
outputDir: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* `object`:
* `queryFieldName` (type: `String`, required) - Name of root field name that resolves the reference
* `args` (type: `JSON`) - You need configure the arguments for that field;
```yml
```yaml
args:
someArg: "{root.someKeyValue}"
```
14 changes: 10 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4161,12 +4161,13 @@
resolved "https://registry.yarnpkg.com/@testing-library/user-event/-/user-event-14.4.3.tgz#af975e367743fa91989cd666666aec31a8f50591"
integrity sha512-kCUc5MEwaEMakkO5x7aoD+DLi02ehmEM2QCGWvNqAS1dV/fAvORWEjnjsEIvml59M7Y5kCkWN6fCCyPOe8OL6Q==

"@theguild/algolia@0.1.0":
version "0.1.0"
resolved "https://registry.yarnpkg.com/@theguild/algolia/-/algolia-0.1.0.tgz#3937ad35df42cde7972137369b2f23f2118eb1f3"
integrity sha512-bIC4qBUj1U/EOkNx7ncFP9uX3cc8u+rRx3duNQv3eA3NUIAn4VmQsAwPx8kKJwSsUSQ0H2vJAh3RtaG5kSVvHA==
"@theguild/[email protected].1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@theguild/algolia/-/algolia-1.0.1.tgz#002f638dbcb5be9c4c5ecb827c9f48d1f7c48210"
integrity sha512-9T57HvRGpylJAwFdLfIoEBDqfNTG7Z0u60j7RLQ1s5wyKcMpMpgnRmw96y7c+50pCeok/NmgsKZyDhUgsyzNDg==
dependencies:
algoliasearch "^4.13.1"
commander "^9.4.1"
github-slugger "^1.4.0"
glob "^8.0.3"
gray-matter "^4.0.3"
Expand Down Expand Up @@ -7224,6 +7225,11 @@ commander@^9.3.0:
resolved "https://registry.yarnpkg.com/commander/-/commander-9.3.0.tgz#f619114a5a2d2054e0d9ff1b31d5ccf89255e26b"
integrity sha512-hv95iU5uXPbK83mjrJKuZyFM/LBAoCV/XhVGkS5Je6tl7sxr6A0ITMw5WoRV46/UaJ46Nllm3Xt7IaJhXTIkzw==

commander@^9.4.1:
version "9.4.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-9.4.1.tgz#d1dd8f2ce6faf93147295c0df13c7c21141cfbdd"
integrity sha512-5EEkTNyHNGFPD2H+c/dXXfQZYa/scCKasxWcXJaWnNJ99pnQN9Vnmqow+p+PlFPE63Q6mThaZws1T+HxfpgtPw==

common-path-prefix@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/common-path-prefix/-/common-path-prefix-3.0.0.tgz#7d007a7e07c58c4b4d5f433131a19141b29f11e0"
Expand Down

0 comments on commit 6fb57d3

Please sign in to comment.