Skip to content

Commit

Permalink
fix: skip config updates when no custom filename is defined (#1951)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
GoodForOneFare committed Oct 11, 2021
1 parent 8361017 commit 72bff0e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-peaches-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-language-service-server': patch
---

fix: skip config updates when no custom filename is defined
3 changes: 2 additions & 1 deletion packages/graphql-language-service-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"devDependencies": {
"@types/mkdirp": "^1.0.1",
"cross-env": "^7.0.2",
"graphql": "experimental-stream-defer"
"graphql": "experimental-stream-defer",
"vscode-languageserver-protocol": "^3.15.3"
}
}
11 changes: 6 additions & 5 deletions packages/graphql-language-service-server/src/MessageProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,11 +564,12 @@ export class MessageProcessor {
throw Error('No cache available for handleWatchedFilesChanged');
}
// update when graphql config changes!
if (
['graphql.config', 'graphqlrc', this._settings.load.fileName].some(
v => change.uri.match(v)?.length,
)
) {
const configMatchers = [
'graphql.config',
'graphqlrc',
this._settings.load.fileName,
].filter(Boolean);
if (configMatchers.some(v => change.uri.match(v)?.length)) {
this._logger.info('updating graphql config');
this._updateGraphQLConfig();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
import { tmpdir } from 'os';
import { SymbolKind } from 'vscode-languageserver';
import { FileChangeType } from 'vscode-languageserver-protocol';
import { Position, Range } from 'graphql-language-service-utils';

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

const baseConfig = { dirpath: __dirname };
jest.mock('fs', () => ({
...jest.requireActual<typeof import('fs')>('fs'),
readFileSync: jest.fn(jest.requireActual('fs').readFileSync),
}));

describe('MessageProcessor', () => {
const logger = new Logger(tmpdir());
Expand All @@ -48,6 +52,7 @@ describe('MessageProcessor', () => {
beforeEach(async () => {
const gqlConfig = await loadConfig({ rootDir: __dirname, extensions: [] });
// loadConfig.mockRestore();
messageProcessor._settings = { load: {} };
messageProcessor._graphQLCache = new GraphQLCache({
configDir: __dirname,
config: gqlConfig,
Expand Down Expand Up @@ -480,4 +485,55 @@ export function Example(arg: string) {}`;
const contents = parseDocument(text, 'test.js');
expect(contents.length).toEqual(0);
});

describe('handleWatchedFilesChangedNotification', () => {
const mockReadFileSync: jest.Mock = jest.requireMock('fs').readFileSync;

beforeEach(() => {
mockReadFileSync.mockReturnValue('');
messageProcessor._updateGraphQLConfig = jest.fn();
});

it('skips config updates for normal file changes', async () => {
await messageProcessor.handleWatchedFilesChangedNotification({
changes: [
{
uri: `${pathToFileURL('.')}/foo.graphql`,
type: FileChangeType.Changed,
},
],
});

expect(messageProcessor._updateGraphQLConfig).not.toHaveBeenCalled();
});

it('updates config for standard config filename changes', async () => {
await messageProcessor.handleWatchedFilesChangedNotification({
changes: [
{
uri: `${pathToFileURL('.')}/.graphql.config`,
type: FileChangeType.Changed,
},
],
});

expect(messageProcessor._updateGraphQLConfig).toHaveBeenCalled();
});

it('updates config for custom config filename changes', async () => {
const customConfigName = 'custom-config-name.yml';
messageProcessor._settings = { load: { fileName: customConfigName } };

await messageProcessor.handleWatchedFilesChangedNotification({
changes: [
{
uri: `${pathToFileURL('.')}/${customConfigName}`,
type: FileChangeType.Changed,
},
],
});

expect(messageProcessor._updateGraphQLConfig).toHaveBeenCalled();
});
});
});

0 comments on commit 72bff0e

Please sign in to comment.