-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Drop Snapshot transform in favor of a new Snapshot plugin (#4638)
* Drop Snapshot transform and introduce a new Snapshot plugin * Respect if
- Loading branch information
Showing
17 changed files
with
199 additions
and
2,129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@graphql-mesh/plugin-snapshot': patch | ||
'@graphql-mesh/types': patch | ||
--- | ||
|
||
Drop Snapshot transform in favor of Snapshot plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { hashObject } from '@graphql-mesh/string-interpolation'; | ||
import { MeshPlugin, MeshPluginOptions, YamlConfig } from '@graphql-mesh/types'; | ||
import { getHeadersObj, pathExists, writeJSON } from '@graphql-mesh/utils'; | ||
import minimatch from 'minimatch'; | ||
import { fs, path, process } from '@graphql-mesh/cross-helpers'; | ||
import { Response } from '@whatwg-node/fetch'; | ||
|
||
function calculateCacheKey(url: string, options: RequestInit) { | ||
return hashObject({ | ||
url, | ||
options, | ||
}); | ||
} | ||
|
||
interface SnapshotEntry { | ||
text: string; | ||
headersObj: Record<string, string>; | ||
status: number; | ||
statusText: string; | ||
} | ||
|
||
export default function useSnapshot( | ||
pluginOptions: MeshPluginOptions<YamlConfig.SnapshotPluginConfig> | ||
): MeshPlugin<any> { | ||
if (typeof pluginOptions.if === 'boolean') { | ||
if (!pluginOptions.if) { | ||
return {}; | ||
} | ||
} | ||
if (typeof pluginOptions.if === 'string') { | ||
// eslint-disable-next-line no-new-func | ||
if (new Function('return ' + pluginOptions.if, 'env')(process.env)) { | ||
return {}; | ||
} | ||
} | ||
const matches = pluginOptions.apply.map(glob => new minimatch.Minimatch(glob)); | ||
const snapshotsDir = pluginOptions.outputDir || '__snapshots__'; | ||
return { | ||
async onFetch({ url, options, setFetchFn }) { | ||
if (matches.some(matcher => matcher.match(url))) { | ||
const snapshotFileName = calculateCacheKey(url, options); | ||
const snapshotPath = path.join(pluginOptions.baseDir, snapshotsDir, `${snapshotFileName}.json`); | ||
if (await pathExists(snapshotPath)) { | ||
setFetchFn(async () => { | ||
const snapshotFile = await fs.promises.readFile(snapshotPath, 'utf-8'); | ||
const snapshot: SnapshotEntry = JSON.parse(snapshotFile); | ||
return new Response(snapshot.text, { | ||
headers: snapshot.headersObj, | ||
status: snapshot.status, | ||
statusText: snapshot.statusText, | ||
}); | ||
}); | ||
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, | ||
}) | ||
); | ||
}; | ||
} | ||
return () => {}; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
extend type Plugin { | ||
""" | ||
Configuration for Snapshot extension | ||
""" | ||
snapshot: SnapshotPluginConfig | ||
} | ||
|
||
type SnapshotPluginConfig @md { | ||
""" | ||
Expression for when to activate this extension. | ||
Value can be a valid JS expression string or a boolean | ||
""" | ||
if: If | ||
""" | ||
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! | ||
} | ||
|
||
union If = String | Boolean |
Oops, something went wrong.