Skip to content

Commit

Permalink
feat: optionally provide LSP an instantiated GraphQLConfig (#1432)
Browse files Browse the repository at this point in the history
for programatic usage of the LSP server, we would prefer to have an already instantiated GraphQLConfig object, so that folks can provide their own loading mechanism (HTTP, different FS loading pattern, etc)
  • Loading branch information
zth committed Mar 18, 2020
1 parent fdda8f0 commit 012db2a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/graphql-language-service-server/src/GraphQLCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ const {
export async function getGraphQLCache(
configDir: Uri,
extensions?: Array<(config: GraphQLConfig) => GraphQLConfig>,
config?: GraphQLConfig,
): Promise<GraphQLCacheInterface> {
let graphQLConfig = await loadConfig({ rootDir: configDir });
let graphQLConfig = config || (await loadConfig({ rootDir: configDir }));
if (extensions && extensions.length > 0) {
for (const extension of extensions) {
graphQLConfig = await extension(graphQLConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type CachedDocumentType = {

export class MessageProcessor {
_graphQLCache!: GraphQLCache;
_graphQLConfig: GraphQLConfig | undefined;
_languageService!: GraphQLLanguageService;
_textDocumentCache: Map<string, CachedDocumentType>;

Expand All @@ -82,12 +83,14 @@ export class MessageProcessor {
constructor(
logger: Logger,
extensions?: Array<(config: GraphQLConfig) => GraphQLConfig>,
config?: GraphQLConfig,
) {
this._textDocumentCache = new Map();
this._isInitialized = false;
this._willShutdown = false;
this._logger = logger;
this._extensions = extensions;
this._graphQLConfig = config;
}

async handleInitializeRequest(
Expand Down Expand Up @@ -117,7 +120,11 @@ export class MessageProcessor {
);
}

this._graphQLCache = await getGraphQLCache(rootPath, this._extensions);
this._graphQLCache = await getGraphQLCache(
rootPath,
this._extensions,
this._graphQLConfig,
);
this._languageService = new GraphQLLanguageService(this._graphQLCache);

if (!serverCapabilities) {
Expand Down
14 changes: 12 additions & 2 deletions packages/graphql-language-service-server/src/startServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type Options = {
configDir?: string;
// array of functions to transform the graphql-config and add extensions dynamically
extensions?: Array<(config: GraphQLConfig) => GraphQLConfig>;
// pre-existing GraphQLConfig
config?: GraphQLConfig;
};
('graphql-language-service-types');

Expand Down Expand Up @@ -100,6 +102,7 @@ export default async function startServer(options: Options): Promise<void> {
logger,
options.configDir,
options.extensions,
options.config,
);
connection.listen();
})
Expand All @@ -116,7 +119,13 @@ export default async function startServer(options: Options): Promise<void> {
break;
}
const connection = createMessageConnection(reader, writer, logger);
addHandlers(connection, logger, options.configDir, options.extensions);
addHandlers(
connection,
logger,
options.configDir,
options.extensions,
options.config,
);
connection.listen();
}
} catch (err) {
Expand All @@ -129,8 +138,9 @@ function addHandlers(
logger: Logger,
configDir?: string,
extensions?: Array<(config: GraphQLConfig) => GraphQLConfig>,
config?: GraphQLConfig,
): void {
const messageProcessor = new MessageProcessor(logger, extensions);
const messageProcessor = new MessageProcessor(logger, extensions, config);
connection.onNotification(
DidOpenTextDocumentNotification.type,
async params => {
Expand Down

0 comments on commit 012db2a

Please sign in to comment.