Skip to content

Commit

Permalink
fix: reload schema when a change to the schema file is detected (#3216)
Browse files Browse the repository at this point in the history
* fix: Invalidate the schema when the file has been changed

* feat: add .json file ending to register changes in introspection files

* fix: handle all types of graphql schema

* feat: changeset
  • Loading branch information
simowe authored Jun 20, 2023
1 parent d991ee2 commit 5513580
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
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}',
),
],
},
Expand Down

0 comments on commit 5513580

Please sign in to comment.