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: skip config updates when no custom filename is defined #1951

Merged
merged 2 commits into from
Oct 11, 2021
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
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 @@ -422,4 +427,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();
});
});
});