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

feature: reload service on local schema change #62

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@
"default": true,
"markdownDescription": "Show a \"Run in Studio\" button to the right of Operation Signatures.",
"scope": "window"
},
"apollographql.run.reloadServiceAfterLocalSchemaChanged": {
"type": "boolean",
"default": true,
"markdownDescription": "Reload service after local schema changed",
"scope": "window"
}
}
},
Expand Down
13 changes: 13 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ export function activate(context: ExtensionContext) {
client.sendNotification("apollographql/reloadService");
});

client.onNotification("apollographql/localSchemaChanged", (params) => {
if (
workspace
.getConfiguration("apollographql")
.get("run.reloadServiceAfterLocalSchemaChanged")
) {
client.outputChannel.appendLine("------------------------------");
client.outputChannel.appendLine(`🚀 Apollo GraphQL Reload Service`);
client.outputChannel.appendLine("------------------------------");
client.sendNotification("apollographql/reloadService");
}
});

// For some reason, non-strings can only be sent in one direction. For now, messages
// coming from the language server just need to be stringified and parsed.
client.onNotification("apollographql/tagsLoaded", (params) => {
Expand Down
4 changes: 4 additions & 0 deletions src/language-server/config/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@ export function parseServiceSpecifier(specifier: ServiceSpecifier) {
const [id, tag] = specifier.split("@").map((x) => x.trim());
return [id, tag] as ServiceIDAndTag;
}

export function getFileNameFromUri(uri: string) {
return uri.substring(uri.lastIndexOf("/") + 1);
}
7 changes: 7 additions & 0 deletions src/language-server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ connection.onDidChangeWatchedFiles((params) => {
workspace.reloadProjectForConfig(uri);
}

if (workspace.localSchemaChanged(uri)) {
connection.sendNotification(
"apollographql/localSchemaChanged",
undefined
);
}

// Don't respond to changes in files that are currently open,
// because we'll get content change notifications instead
if (type === FileChangeType.Changed) {
Expand Down
22 changes: 21 additions & 1 deletion src/language-server/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
NotificationHandler,
PublishDiagnosticsParams,
} from "vscode-languageserver";
import { QuickPickItem } from "vscode";
import { QuickPickItem, workspace } from "vscode";
import { GraphQLProject, DocumentUri } from "./project/base";
import { dirname } from "path";
import fg from "glob";
Expand All @@ -12,6 +12,7 @@ import {
ApolloConfig,
isClientConfig,
ServiceConfig,
getFileNameFromUri,
} from "./config";
import { LanguageServerLoadingHandler } from "./loadingHandler";
import { ServiceID, SchemaTag, ClientIdentity } from "./engine";
Expand Down Expand Up @@ -244,6 +245,25 @@ export class GraphQLWorkspace {
return Array.from(this.projectsByFolderUri.values()).flat();
}

private getLocalSchemaFileNames(): string[] {
const localSchemaUris: string[] = [];
this.projects.forEach((p) => {
if (p.config.service?.localSchemaFile) {
if (typeof p.config.service.localSchemaFile === "string") {
localSchemaUris.push(p.config.service.localSchemaFile);
} else {
localSchemaUris.push(...p.config.service.localSchemaFile);
}
}
});
return localSchemaUris.map((uri) => getFileNameFromUri(uri));
}

localSchemaChanged(uri: string): boolean {
const fileNames = this.getLocalSchemaFileNames();
return fileNames.some((fileName) => uri.endsWith(fileName)) ? true : false;
}

projectForFile(uri: DocumentUri): GraphQLProject | undefined {
const cachedResult = this._projectForFileCache.get(uri);
if (cachedResult) {
Expand Down
3 changes: 2 additions & 1 deletion src/languageServerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ export function getLanguageServerClient(
"dart",
"reason",
"elixir",
"json",
],
synchronize: {
fileEvents: [
workspace.createFileSystemWatcher("**/.env?(.local)"),
workspace.createFileSystemWatcher(
"**/*.{graphql,js,ts,jsx,tsx,vue,svelte,py,rb,dart,re,ex,exs}"
"**/*.{graphql,js,ts,jsx,tsx,vue,svelte,py,rb,dart,re,ex,exs,json}"
),
],
},
Expand Down
1 change: 1 addition & 0 deletions src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Messages = {
"apollographql/loadingComplete": number;
"apollographql/loading": { message: string; token: number };
"apollographql/engineDecorations": { decorations: EngineDecoration[] };
"apollographql/localSchemaChanged": undefined;
serverDebugMessage: {
type: "info" | "warning" | "error" | "errorTelemetry";
message: string;
Expand Down