Skip to content
This repository was archived by the owner on Sep 2, 2020. It is now read-only.

Commit b06ca4d

Browse files
authored
Merge pull request #226 from mgadda/mgadda/fix-unhandled-errors
Correctly load .graphqlconfigs which do not define projects
2 parents 55b4380 + 1e418a5 commit b06ca4d

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

packages/server/src/MessageProcessor.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
findGraphQLConfigFile,
2626
getGraphQLConfig,
2727
GraphQLProjectConfig,
28+
GraphQLConfig,
2829
} from 'graphql-config';
2930
import {GraphQLLanguageService} from 'graphql-language-service-interface';
3031
import {Position, Range} from 'graphql-language-service-utils';
@@ -107,12 +108,9 @@ export class MessageProcessor {
107108
}
108109

109110
this._graphQLCache = await getGraphQLCache(rootPath);
111+
const config = getGraphQLConfig(rootPath);
110112
if (this._watchmanClient) {
111-
this._subcribeWatchman(
112-
rootPath,
113-
this._graphQLCache,
114-
this._watchmanClient,
115-
);
113+
this._subcribeWatchman(config, this._watchmanClient);
116114
}
117115
this._languageService = new GraphQLLanguageService(this._graphQLCache);
118116

@@ -135,8 +133,7 @@ export class MessageProcessor {
135133
// Use watchman to subscribe to project file changes only if watchman is
136134
// installed. Otherwise, rely on LSP watched files did change events.
137135
async _subcribeWatchman(
138-
rootPath: string,
139-
graphqlCache: GraphQLCache,
136+
config: GraphQLConfig,
140137
watchmanClient: GraphQLWatchman,
141138
) {
142139
if (!watchmanClient) {
@@ -147,9 +144,11 @@ export class MessageProcessor {
147144
await watchmanClient.checkVersion();
148145

149146
// Otherwise, subcribe watchman according to project config(s).
150-
const config = getGraphQLConfig(rootPath);
151-
let projectConfigs: GraphQLProjectConfig[] =
152-
Object.values(config.getProjects() || {}) || [];
147+
const projectMap = config.getProjects();
148+
let projectConfigs: GraphQLProjectConfig[] = projectMap
149+
? Object.values(projectMap)
150+
: [];
151+
153152
// There can either be a single config or one or more project
154153
// configs, but not both.
155154
if (projectConfigs.length === 0) {
@@ -162,7 +161,7 @@ export class MessageProcessor {
162161
watchmanClient.subscribe(
163162
projectConfig.configDir,
164163
this._graphQLCache.handleWatchmanSubscribeEvent(
165-
rootPath,
164+
config.configDir,
166165
projectConfig,
167166
),
168167
);

packages/server/src/__tests__/MessageProcessor-test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ import {beforeEach, describe, it} from 'mocha';
1414

1515
import {MessageProcessor} from '../MessageProcessor';
1616
import MockWatchmanClient from '../__mocks__/MockWatchmanClient';
17+
import {GraphQLConfig} from 'graphql-config';
18+
import {GraphQLCache} from '../GraphQLCache';
1719

1820
describe('MessageProcessor', () => {
1921
const mockWatchmanClient = new MockWatchmanClient();
2022
const messageProcessor = new MessageProcessor(undefined, mockWatchmanClient);
2123

2224
const queryDir = `${__dirname}/__queries__`;
25+
const schemaPath = `${__dirname}/__schema__/StarWarsSchema.graphql`;
2326
const textDocumentTestString = `
2427
{
2528
hero(episode: NEWHOPE){
@@ -46,6 +49,7 @@ describe('MessageProcessor', () => {
4649
};
4750
},
4851
updateFragmentDefinition() {},
52+
handleWatchmanSubscribeEvent() {},
4953
};
5054
messageProcessor._languageService = {
5155
getAutocompleteSuggestions: (query, position, uri) => {
@@ -167,4 +171,32 @@ describe('MessageProcessor', () => {
167171
const result = await messageProcessor.handleDefinitionRequest(test);
168172
expect(result[0].uri).to.equal(`file://${queryDir}/testFragment.graphql`);
169173
});
174+
175+
it('loads configs without projects when watchman is present', async () => {
176+
const config = new GraphQLConfig(
177+
{
178+
schemaPath,
179+
includes: `${queryDir}/*.graphql`,
180+
},
181+
'not/a/real/config',
182+
);
183+
184+
await messageProcessor._subcribeWatchman(config, mockWatchmanClient);
185+
});
186+
187+
it('loads configs with projects when watchman is present', async () => {
188+
const config = new GraphQLConfig(
189+
{
190+
projects: {
191+
foo: {
192+
schemaPath,
193+
includes: `${queryDir}/*.graphql`,
194+
},
195+
},
196+
},
197+
'not/a/real/config',
198+
);
199+
200+
await messageProcessor._subcribeWatchman(config, mockWatchmanClient);
201+
});
170202
});

0 commit comments

Comments
 (0)