Skip to content

Commit 72bff0e

Browse files
fix: skip config updates when no custom filename is defined (#1951)
Skip config reload when no custom filename is defined. Previously, any project without a load.fileName configuration would reload configuration on every change event. This is because match(undefined) always returns true.
1 parent 8361017 commit 72bff0e

File tree

4 files changed

+70
-7
lines changed

4 files changed

+70
-7
lines changed

.changeset/itchy-peaches-return.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'graphql-language-service-server': patch
3+
---
4+
5+
fix: skip config updates when no custom filename is defined

packages/graphql-language-service-server/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"devDependencies": {
4545
"@types/mkdirp": "^1.0.1",
4646
"cross-env": "^7.0.2",
47-
"graphql": "experimental-stream-defer"
47+
"graphql": "experimental-stream-defer",
48+
"vscode-languageserver-protocol": "^3.15.3"
4849
}
4950
}

packages/graphql-language-service-server/src/MessageProcessor.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,12 @@ export class MessageProcessor {
564564
throw Error('No cache available for handleWatchedFilesChanged');
565565
}
566566
// update when graphql config changes!
567-
if (
568-
['graphql.config', 'graphqlrc', this._settings.load.fileName].some(
569-
v => change.uri.match(v)?.length,
570-
)
571-
) {
567+
const configMatchers = [
568+
'graphql.config',
569+
'graphqlrc',
570+
this._settings.load.fileName,
571+
].filter(Boolean);
572+
if (configMatchers.some(v => change.uri.match(v)?.length)) {
572573
this._logger.info('updating graphql config');
573574
this._updateGraphQLConfig();
574575
}

packages/graphql-language-service-server/src/__tests__/MessageProcessor-test.ts

+57-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99
import { tmpdir } from 'os';
1010
import { SymbolKind } from 'vscode-languageserver';
11+
import { FileChangeType } from 'vscode-languageserver-protocol';
1112
import { Position, Range } from 'graphql-language-service-utils';
1213

1314
import { MessageProcessor } from '../MessageProcessor';
@@ -24,7 +25,10 @@ import type { DefinitionQueryResult, Outline } from 'graphql-language-service';
2425
import { Logger } from '../Logger';
2526
import { pathToFileURL } from 'url';
2627

27-
const baseConfig = { dirpath: __dirname };
28+
jest.mock('fs', () => ({
29+
...jest.requireActual<typeof import('fs')>('fs'),
30+
readFileSync: jest.fn(jest.requireActual('fs').readFileSync),
31+
}));
2832

2933
describe('MessageProcessor', () => {
3034
const logger = new Logger(tmpdir());
@@ -48,6 +52,7 @@ describe('MessageProcessor', () => {
4852
beforeEach(async () => {
4953
const gqlConfig = await loadConfig({ rootDir: __dirname, extensions: [] });
5054
// loadConfig.mockRestore();
55+
messageProcessor._settings = { load: {} };
5156
messageProcessor._graphQLCache = new GraphQLCache({
5257
configDir: __dirname,
5358
config: gqlConfig,
@@ -480,4 +485,55 @@ export function Example(arg: string) {}`;
480485
const contents = parseDocument(text, 'test.js');
481486
expect(contents.length).toEqual(0);
482487
});
488+
489+
describe('handleWatchedFilesChangedNotification', () => {
490+
const mockReadFileSync: jest.Mock = jest.requireMock('fs').readFileSync;
491+
492+
beforeEach(() => {
493+
mockReadFileSync.mockReturnValue('');
494+
messageProcessor._updateGraphQLConfig = jest.fn();
495+
});
496+
497+
it('skips config updates for normal file changes', async () => {
498+
await messageProcessor.handleWatchedFilesChangedNotification({
499+
changes: [
500+
{
501+
uri: `${pathToFileURL('.')}/foo.graphql`,
502+
type: FileChangeType.Changed,
503+
},
504+
],
505+
});
506+
507+
expect(messageProcessor._updateGraphQLConfig).not.toHaveBeenCalled();
508+
});
509+
510+
it('updates config for standard config filename changes', async () => {
511+
await messageProcessor.handleWatchedFilesChangedNotification({
512+
changes: [
513+
{
514+
uri: `${pathToFileURL('.')}/.graphql.config`,
515+
type: FileChangeType.Changed,
516+
},
517+
],
518+
});
519+
520+
expect(messageProcessor._updateGraphQLConfig).toHaveBeenCalled();
521+
});
522+
523+
it('updates config for custom config filename changes', async () => {
524+
const customConfigName = 'custom-config-name.yml';
525+
messageProcessor._settings = { load: { fileName: customConfigName } };
526+
527+
await messageProcessor.handleWatchedFilesChangedNotification({
528+
changes: [
529+
{
530+
uri: `${pathToFileURL('.')}/${customConfigName}`,
531+
type: FileChangeType.Changed,
532+
},
533+
],
534+
});
535+
536+
expect(messageProcessor._updateGraphQLConfig).toHaveBeenCalled();
537+
});
538+
});
483539
});

0 commit comments

Comments
 (0)