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

fix: reload schema when a change to the schema file is detected #3216

Merged
merged 4 commits into from
Jun 20, 2023
Merged
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 .changeset/soft-dingos-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'graphql-language-service-server': patch
'vscode-graphql': patch
---

fix: reload schema when a change to the schema file is detected
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
return schema;
};

_invalidateSchemaCacheForProject(projectConfig: GraphQLProjectConfig) {
invalidateSchemaCacheForProject(projectConfig: GraphQLProjectConfig) {
const schemaKey = this._getSchemaCacheKeyForProject(
projectConfig,
) as string;
Expand Down
38 changes: 38 additions & 0 deletions packages/graphql-language-service-server/src/MessageProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,8 @@ export class MessageProcessor {
await this._updateObjectTypeDefinition(uri, contents);

const project = this._graphQLCache.getProjectForFile(uri);
await this._updateSchemaIfChanged(project, uri);

let diagnostics: Diagnostic[] = [];

if (
Expand Down Expand Up @@ -1138,6 +1140,42 @@ export class MessageProcessor {
await this._graphQLCache.updateFragmentDefinition(rootDir, uri, contents);
}

async _updateSchemaIfChanged(
project: GraphQLProjectConfig,
uri: Uri,
): Promise<void> {
await Promise.all(
this._unwrapProjectSchema(project).map(async schema => {
const schemaFilePath = path.resolve(project.dirpath, schema);
const uriFilePath = URI.parse(uri).fsPath;
if (uriFilePath === schemaFilePath) {
await this._graphQLCache.invalidateSchemaCacheForProject(project);
}
}),
);
}

_unwrapProjectSchema(project: GraphQLProjectConfig): string[] {
const projectSchema = project.schema;

const schemas: string[] = [];
if (typeof projectSchema === 'string') {
schemas.push(projectSchema);
} else if (Array.isArray(projectSchema)) {
for (const schemaEntry of projectSchema) {
if (typeof schemaEntry === 'string') {
schemas.push(schemaEntry);
} else if (schemaEntry) {
schemas.push(...Object.keys(schemaEntry));
}
}
} else {
schemas.push(...Object.keys(projectSchema));
}

return schemas;
}

async _updateObjectTypeDefinition(
uri: Uri,
contents: CachedContent[],
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode-graphql/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export async function activate(context: ExtensionContext) {
// TODO: load ignore
// These ignore node_modules and .git by default
workspace.createFileSystemWatcher(
'**/{*.graphql,*.graphqls,*.gql,*.js,*.mjs,*.cjs,*.esm,*.es,*.es6,*.jsx,*.ts,*.tsx,*.vue,*.svelte,*.cts,*.mts}',
'**/{*.graphql,*.graphqls,*.gql,*.js,*.mjs,*.cjs,*.esm,*.es,*.es6,*.jsx,*.ts,*.tsx,*.vue,*.svelte,*.cts,*.mts,*.json}',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'**/{*.graphql,*.graphqls,*.gql,*.js,*.mjs,*.cjs,*.esm,*.es,*.es6,*.jsx,*.ts,*.tsx,*.vue,*.svelte,*.cts,*.mts,*.json}',
'**/*.{graphql,graphqls,gql,js,mjs,cjs,esm,es,es6,jsx,ts,tsx,vue,svelte,cts,mts,json}',

@acao I think this can be simplified to ?

Copy link
Collaborator

@dimaMachina dimaMachina Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also es, esm and es6 extension should be removed 😅 never saw that somebody use it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'**/{*.graphql,*.graphqls,*.gql,*.js,*.mjs,*.cjs,*.esm,*.es,*.es6,*.jsx,*.ts,*.tsx,*.vue,*.svelte,*.cts,*.mts,*.json}',
'**/*.{graphql,graphqls,gql,js,mjs,cjs,jsx,ts,tsx,vue,svelte,cts,mts,json}',

Copy link
Member

@acao acao Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@B2o5T it would need to be tested manually, vscode uses it's own library for glob expansion

Copy link
Member

@acao acao Jun 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@B2o5T same re: these unusual seeming extensions - however, they were requested by users earlier this year and last, and there were lots of emoji reacts 🤷🏻‍♀️ anecdotal experience isn't always enough to evaluate userland haha

there really is no reason not to be exhaustive and inclusive here, since it doesn't hurt anything

),
],
},
Expand Down