-
Notifications
You must be signed in to change notification settings - Fork 132
Adds a temporary static build preview of Lab for faster iterations #7901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1aa46ac
0fd99db
bc6a84f
4dcadfd
8277543
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@graphql-hive/laboratory': patch | ||
| '@graphql-hive/render-laboratory': patch | ||
| --- | ||
|
|
||
| Hive Laboratory renders Hive Router query plan if included in response extensions |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import { promises as fs } from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| const scriptDirectory = path.dirname(fileURLToPath(import.meta.url)); | ||
| const packageDirectory = path.resolve(scriptDirectory, '..'); | ||
| const distDirectory = path.resolve(packageDirectory, 'dist'); | ||
|
|
||
| const resolveDistPath = relativePath => path.resolve(distDirectory, relativePath); | ||
|
|
||
| const readRequiredText = relativePath => fs.readFile(resolveDistPath(relativePath), 'utf8'); | ||
|
|
||
| const readOptionalText = async relativePath => { | ||
| try { | ||
| return await fs.readFile(resolveDistPath(relativePath), 'utf8'); | ||
| } catch { | ||
| return ''; | ||
| } | ||
| }; | ||
|
|
||
| const [ | ||
| bundleSource, | ||
| cssSource, | ||
| editorWorkerSource, | ||
| graphqlWorkerSource, | ||
| jsonWorkerSource, | ||
| tsWorkerSource, | ||
| ] = await Promise.all([ | ||
| readRequiredText('hive-laboratory.umd.js'), | ||
| readOptionalText('laboratory.css'), | ||
| readRequiredText('monacoeditorwork/editor.worker.bundle.js'), | ||
| readRequiredText('monacoeditorwork/graphql.worker.bundle.js'), | ||
| readRequiredText('monacoeditorwork/json.worker.bundle.js'), | ||
| readRequiredText('monacoeditorwork/ts.worker.bundle.js'), | ||
| ]); | ||
|
|
||
| const serializeForScriptTag = value => | ||
| JSON.stringify(value) | ||
| .replace(/</g, '\\u003c') | ||
| .replace(/>/g, '\\u003e') | ||
| .replace(/&/g, '\\u0026') | ||
| .replace(/\u2028/g, '\\u2028') | ||
| .replace(/\u2029/g, '\\u2029'); | ||
|
|
||
| const payload = { | ||
| cssSource, | ||
| bundleSource, | ||
| workers: { | ||
| editorWorkerService: editorWorkerSource, | ||
| graphql: graphqlWorkerSource, | ||
| json: jsonWorkerSource, | ||
| typescript: tsWorkerSource, | ||
| }, | ||
| }; | ||
|
|
||
| const html = `<!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| <title>Hive Laboratory Static Preview</title> | ||
| <style> | ||
| html, | ||
| body, | ||
| #root { | ||
| height: 100%; | ||
| } | ||
|
|
||
| body { | ||
| margin: 0; | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <div id="root"></div> | ||
| <script> | ||
| const payload = ${serializeForScriptTag(payload)}; | ||
|
|
||
| const createWorkerUrl = workerContent => { | ||
| const blob = new Blob([workerContent], { type: 'application/javascript' }); | ||
| return URL.createObjectURL(blob); | ||
| }; | ||
|
|
||
| const workerUrls = { | ||
| editorWorkerService: createWorkerUrl(payload.workers.editorWorkerService), | ||
| typescript: createWorkerUrl(payload.workers.typescript), | ||
| json: createWorkerUrl(payload.workers.json), | ||
| graphql: createWorkerUrl(payload.workers.graphql), | ||
| }; | ||
|
|
||
| globalThis.MonacoEnvironment = { | ||
| globalAPI: false, | ||
| getWorkerUrl(_moduleId, label) { | ||
| return workerUrls[label] || workerUrls.editorWorkerService; | ||
| }, | ||
| }; | ||
|
|
||
| if (payload.cssSource) { | ||
| const style = document.createElement('style'); | ||
| style.textContent = payload.cssSource; | ||
| document.head.appendChild(style); | ||
| } | ||
|
|
||
| new Function(payload.bundleSource)(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using const script = document.createElement('script');
script.textContent = payload.bundleSource;
document.body.appendChild(script); |
||
|
|
||
| const params = new URLSearchParams(globalThis.location.search); | ||
| const endpoint = params.get('endpoint') || globalThis.localStorage.getItem('hive-laboratory:endpoint'); | ||
|
|
||
| globalThis.HiveLaboratory.renderLaboratory(document.getElementById('root'), { | ||
| defaultEndpoint: endpoint || globalThis.location.origin + '/graphql', | ||
| }); | ||
| </script> | ||
| </body> | ||
| </html> | ||
| `; | ||
|
|
||
| const outputPath = resolveDistPath('hive-laboratory.static-preview.html'); | ||
| await fs.writeFile(outputPath, html, 'utf8'); | ||
|
|
||
| console.log(`Created static preview: ${outputPath}`); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
readRequiredTextfunction is designed to fetch essential files for the static preview. However, it currently lacks error handling. If any of these files are missing from thedistdirectory, the script will fail abruptly without a clear message indicating which file caused the issue. Implementing atry-catchblock would make the script more resilient and provide better diagnostic information in case of missing build artifacts.