Skip to content
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

DevTools <-> VSCode integration #188

Merged
merged 30 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1e30f9c
start adding devtools
phryneas Aug 28, 2024
30e5b02
first working version
phryneas Aug 29, 2024
41541e1
set `retainContextWhenHidden`
phryneas Aug 30, 2024
f32b841
testing harness
phryneas Aug 30, 2024
a76879b
show in panel instead
phryneas Sep 2, 2024
1f14d26
add CSP
phryneas Sep 2, 2024
3bf6897
`retainContextWhenHidden` not necessary anymore
phryneas Sep 2, 2024
02a27c9
panel presence configurable and "detect" mode
phryneas Sep 2, 2024
3dcc8f5
add config option for server port
phryneas Sep 2, 2024
da18007
wording
phryneas Sep 2, 2024
751f182
import devtools from package
phryneas Sep 3, 2024
7b73f64
smaller tweaks
phryneas Sep 3, 2024
09fabbd
use lifecycle in example
phryneas Sep 3, 2024
e6951e2
use vscode font size
phryneas Sep 4, 2024
88f8287
handle CSP for fetch calls to GitHub API
phryneas Sep 4, 2024
61f7bf6
comment out
phryneas Sep 4, 2024
5cdcf8d
set default to `never`, mark as experimental feature
phryneas Sep 4, 2024
6ed3511
remove vscode default styles
phryneas Sep 5, 2024
329d5d5
disable semgrep in test client
phryneas Sep 11, 2024
49712fe
use built packages
phryneas Sep 20, 2024
f314774
Merge remote-tracking branch 'origin/main' into pr/devtools-webview
phryneas Sep 20, 2024
65a25b3
Apply suggestions from code review
phryneas Sep 20, 2024
a02f5ab
lint
phryneas Sep 20, 2024
702fa50
Merge remote-tracking branch 'origin/main' into pr/devtools-webview
phryneas Dec 6, 2024
c6c9881
use package from npm
phryneas Dec 6, 2024
096869a
Adjustments to VSCode <-> DevTools communication (#197)
phryneas Dec 11, 2024
9cd5deb
reference npm package
phryneas Dec 11, 2024
16332b8
DevTools: adjustments to logging behaviour (#244)
phryneas Dec 11, 2024
7803394
changeset
phryneas Dec 11, 2024
62c7d85
Merge remote-tracking branch 'origin/main' into pr/devtools-webview
phryneas Dec 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ vscode-apollo-*.vsix
# files generated from tests
__tmp__*
.vscode-test
.yalc
yalc.lock
3 changes: 3 additions & 0 deletions .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ sampleWorkspace
renovate.json
images/**/*.gif
images/marketplace
.yalc
yalc.lock
start-ac.mjs
16 changes: 16 additions & 0 deletions images/apollo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 17 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 52 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
},
"dependencies": {
"@apollo/client": "3.11.4",
"@apollo/client-devtools-vscode": "file:.yalc/@apollo/client-devtools-vscode",
phryneas marked this conversation as resolved.
Show resolved Hide resolved
"@apollo/subgraph": "2.8.4",
"@graphql-tools/schema": "10.0.5",
"@wry/context": "0.7.4",
Expand All @@ -60,6 +61,7 @@
"vscode-languageserver-textdocument": "1.0.12",
"vscode-uri": "3.0.8",
"which": "4.0.0",
"ws": "8.18.0",
"zod": "3.23.8",
"zod-validation-error": "3.3.1"
},
Expand Down Expand Up @@ -132,6 +134,23 @@
"default": true,
"markdownDescription": "Show a \"Run in Studio\" button to the right of Operation Signatures.",
"scope": "window"
},
"apollographql.devTools.showPanel": {
"type": "string",
"enum": [
"always",
"never",
"detect"
],
"default": "never",
"markdownDescription": "[Experimental Feature] If the Apollo Client DevTools panel should be shown. If set to `detect`, the panel will only be shown if a configuration file with a client project is found in the workspace.",
"scope": "window"
},
"apollographql.devTools.serverPort": {
"type": "number",
"default": 7095,
"markdownDescription": "The Apollo Client DevTools server port. The server will be started as soon as you start using the DevTools panels.",
"scope": "window"
}
}
},
Expand Down Expand Up @@ -246,8 +265,40 @@
"command": "apollographql/showStats",
"title": "Show Status",
"category": "Apollo"
},
{
"command": "apollographql/startDevToolsServer",
"title": "Start Apollo Client DevTools Server",
"category": "Apollo",
"when": "config.apollographql.devTools.showPanel=='always' || (config.apollographql.devTools.showPanel=='detect' && vscode-apollo.hasClientProject)"
},
{
"command": "apollographql/stopDevToolsServer",
"title": "Stop Apollo Client DevTools Server",
"category": "Apollo",
"when": "config.apollographql.devTools.showPanel=='always' || (config.apollographql.devTools.showPanel=='detect' && vscode-apollo.hasClientProject)"
}
]
],
"viewsContainers": {
"panel": [
{
"id": "client-devtools",
"title": "Apollo Client DevTools",
"icon": "images/apollo.svg"
}
]
},
"views": {
"client-devtools": [
{
"type": "webview",
"id": "vscode-apollo-client-devtools",
"name": "Apollo Client DevTools",
"icon": "images/apollo.svg",
"when": "config.apollographql.devTools.showPanel=='always' || (config.apollographql.devTools.showPanel=='detect' && vscode-apollo.hasClientProject)"
}
]
}
},
"galleryBanner": {
"color": "#1d127d",
Expand Down
5 changes: 4 additions & 1 deletion sampleWorkspace/sampleWorkspace.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@
"path": "../src/language-server/__tests__/fixtures/documents"
}
],
"settings": {}
"settings": {
"apollographql.devTools.showPanel": "detect",
"apollographql.devTools.serverPort": 7095
}
}
9 changes: 8 additions & 1 deletion src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ const watch = process.argv.includes("--watch");

async function main() {
const ctx = await esbuild.context({
entryPoints: ["src/extension.ts", "src/language-server/server.ts"],
entryPoints: [
"src/extension.ts",
"src/language-server/server.ts",
{
in: require.resolve("@apollo/client-devtools-vscode/panel"),
out: "panel",
},
],
bundle: true,
format: "cjs",
minify: production,
Expand Down
129 changes: 129 additions & 0 deletions src/devtools/DevToolsViewProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import * as vscode from "vscode";
import { devtoolsEvents } from "./server";
export class DevToolsViewProvider implements vscode.WebviewViewProvider {
public static readonly viewType = "vscode-apollo-client-devtools";

constructor(private readonly _extensionUri: vscode.Uri) {}
async resolveWebviewView(
phryneas marked this conversation as resolved.
Show resolved Hide resolved
panel: vscode.WebviewView,
context: vscode.WebviewViewResolveContext,
token: vscode.CancellationToken,
): Promise<void> {
vscode.commands.executeCommand("apollographql/startDevToolsServer");
panel.webview.options = {
enableScripts: true,
localResourceRoots: [this._extensionUri],
};
panel.webview.html = DevToolsViewProvider._getHtmlForWebview(
panel.webview,
this._extensionUri,
);

panel.webview.onDidReceiveMessage((data) => {
devtoolsEvents.emit("fromDevTools", data);
});
function forwardToDevTools(data: unknown) {
panel.webview.postMessage(data);
}
devtoolsEvents.addListener("toDevTools", forwardToDevTools);
panel.onDidDispose(() => {
devtoolsEvents.removeListener("toDevTools", forwardToDevTools);
});
}

private static _getHtmlForWebview(
webview: vscode.Webview,
extensionUri: vscode.Uri,
) {
// Get the local path to main script run in the webview, then convert it to a uri we can use in the webview.
const scriptUri = webview.asWebviewUri(
vscode.Uri.joinPath(extensionUri, "lib", "panel.js"),
);

// Use a nonce to only allow a specific script to be run.
const nonce = getNonce();

return `
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />

<!--
Use a content security policy to only allow loading images from https or from our extension directory,
and only allow scripts that have a specific nonce.
-->
<meta http-equiv="Content-Security-Policy" content="
default-src 'none';
style-src ${webview.cspSource} https://fonts.googleapis.com 'unsafe-inline';
font-src ${webview.cspSource} https://fonts.gstatic.com;
img-src ${webview.cspSource} https:;
connect-src ${webview.cspSource} https://*.github.com;
script-src 'nonce-${nonce}';
frame-src https://*.apollographql.com/;
">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Apollo Client DevTools</title>
<style>
html,
body {
overflow: hidden;
font-size: var(--vscode-font-size);
}
::-webkit-scrollbar {
display: none;
}
#devtools {
width: 100vw;
height: 100vh;
}
</style>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link
href="https://fonts.googleapis.com/css2?family=Fira+Code&family=Inter:wght@400;500;600;700&display=swap"
rel="stylesheet"
/>
</head>
<body class="text-primary dark:text-primary-dark">
<div id="devtools"></div>
<script nonce="${nonce}">
const vscode = acquireVsCodeApi();
try {
// remove VSCode default styles
_defaultStyles.remove();
} catch {}
window.originalPostMessage = window.postMessage;
window.postMessage = function wrapPostMessage (...args) {
if (args.length>1 && args[1].startsWith("vscode-webview://")) {
return window.originalPostMessage(...args);
}
return vscode.postMessage(...args);
};

window.addEventListener("message", (event) => { console.debug(event); });
phryneas marked this conversation as resolved.
Show resolved Hide resolved
</script>
<script nonce="${nonce}" src="${scriptUri}"></script>
<script nonce="${nonce}">
window.originalPostMessage({
id: 123,
source: "apollo-client-devtools",
type: "actor",
message: { type: "initializePanel" },
});
</script>
</body>
</html>
`;
}
}

function getNonce() {
let text = "";
const possible =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (let i = 0; i < 32; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}
36 changes: 36 additions & 0 deletions src/devtools/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { WebSocketServer } from "ws";
import { Disposable } from "vscode";
import { runServer } from "@apollo/client-devtools-vscode/vscode-server";

Check failure on line 3 in src/devtools/server.ts

View workflow job for this annotation

GitHub Actions / Bundle Extension as Artifact Download

Cannot find module '@apollo/client-devtools-vscode/vscode-server' or its corresponding type declarations.
import { Debug } from "../debug";
import { EventEmitter } from "node:events";

export const devtoolsEvents = new EventEmitter<{
toDevTools: [unknown];
fromDevTools: [unknown];
}>();
devtoolsEvents.addListener("toDevTools", (msg) => {
Debug.info("WS > DevTools: " + JSON.stringify(msg));
});
devtoolsEvents.addListener("fromDevTools", (msg) => {
Debug.info("DevTools > WS: " + JSON.stringify(msg));
});

export function startServer(port: number): Disposable {
let wss: WebSocketServer | null = new WebSocketServer({ port });
runServer(wss, {
addListener: (listener) => {

Check failure on line 21 in src/devtools/server.ts

View workflow job for this annotation

GitHub Actions / Bundle Extension as Artifact Download

Parameter 'listener' implicitly has an 'any' type.
devtoolsEvents.addListener("fromDevTools", listener);
return () => {
devtoolsEvents.removeListener("fromDevTools", listener);
};
},
postMessage: (message) => {

Check failure on line 27 in src/devtools/server.ts

View workflow job for this annotation

GitHub Actions / Bundle Extension as Artifact Download

Parameter 'message' implicitly has an 'any' type.
devtoolsEvents.emit("toDevTools", message);
},
});

return new Disposable(() => {
wss?.close();
wss = null;
});
}
Loading
Loading