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

Correctly load .graphqlconfigs which do not define projects #226

Merged
merged 1 commit into from
Apr 16, 2018
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
21 changes: 10 additions & 11 deletions packages/server/src/MessageProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
findGraphQLConfigFile,
getGraphQLConfig,
GraphQLProjectConfig,
GraphQLConfig,
} from 'graphql-config';
import {GraphQLLanguageService} from 'graphql-language-service-interface';
import {Position, Range} from 'graphql-language-service-utils';
Expand Down Expand Up @@ -107,12 +108,9 @@ export class MessageProcessor {
}

this._graphQLCache = await getGraphQLCache(rootPath);
const config = getGraphQLConfig(rootPath);
if (this._watchmanClient) {
this._subcribeWatchman(
rootPath,
this._graphQLCache,
this._watchmanClient,
);
this._subcribeWatchman(config, this._watchmanClient);
}
this._languageService = new GraphQLLanguageService(this._graphQLCache);

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

// Otherwise, subcribe watchman according to project config(s).
const config = getGraphQLConfig(rootPath);
let projectConfigs: GraphQLProjectConfig[] =
Object.values(config.getProjects() || {}) || [];
const projectMap = config.getProjects();
let projectConfigs: GraphQLProjectConfig[] = projectMap
? Object.values(projectMap)
: [];

// There can either be a single config or one or more project
// configs, but not both.
if (projectConfigs.length === 0) {
Expand All @@ -162,7 +161,7 @@ export class MessageProcessor {
watchmanClient.subscribe(
projectConfig.configDir,
this._graphQLCache.handleWatchmanSubscribeEvent(
rootPath,
config.configDir,
projectConfig,
),
);
Expand Down
32 changes: 32 additions & 0 deletions packages/server/src/__tests__/MessageProcessor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ import {beforeEach, describe, it} from 'mocha';

import {MessageProcessor} from '../MessageProcessor';
import MockWatchmanClient from '../__mocks__/MockWatchmanClient';
import {GraphQLConfig} from 'graphql-config';
import {GraphQLCache} from '../GraphQLCache';

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

const queryDir = `${__dirname}/__queries__`;
const schemaPath = `${__dirname}/__schema__/StarWarsSchema.graphql`;
const textDocumentTestString = `
{
hero(episode: NEWHOPE){
Expand All @@ -46,6 +49,7 @@ describe('MessageProcessor', () => {
};
},
updateFragmentDefinition() {},
handleWatchmanSubscribeEvent() {},
};
messageProcessor._languageService = {
getAutocompleteSuggestions: (query, position, uri) => {
Expand Down Expand Up @@ -167,4 +171,32 @@ describe('MessageProcessor', () => {
const result = await messageProcessor.handleDefinitionRequest(test);
expect(result[0].uri).to.equal(`file://${queryDir}/testFragment.graphql`);
});

it('loads configs without projects when watchman is present', async () => {
const config = new GraphQLConfig(
{
schemaPath,
includes: `${queryDir}/*.graphql`,
},
'not/a/real/config',
);

await messageProcessor._subcribeWatchman(config, mockWatchmanClient);
});

it('loads configs with projects when watchman is present', async () => {
const config = new GraphQLConfig(
{
projects: {
foo: {
schemaPath,
includes: `${queryDir}/*.graphql`,
},
},
},
'not/a/real/config',
);

await messageProcessor._subcribeWatchman(config, mockWatchmanClient);
});
});