@@ -220,10 +220,9 @@ export class MessageProcessor {
220
220
require ( 'dotenv' ) . config ( { path : settings . dotEnvPath } ) ;
221
221
}
222
222
this . _settings = { ...settings , ...vscodeSettings } ;
223
- const rootDir =
224
- this . _settings ?. load ?. rootDir ??
225
- this . _loadConfigOptions ?. rootDir ??
226
- this . _rootPath ;
223
+ const rootDir = this . _settings ?. load ?. rootDir . length
224
+ ? this . _settings ?. load ?. rootDir
225
+ : this . _rootPath ;
227
226
this . _rootPath = rootDir ;
228
227
this . _loadConfigOptions = {
229
228
...Object . keys ( this . _settings ?. load ?? { } ) . reduce ( ( agg , key ) => {
@@ -247,9 +246,9 @@ export class MessageProcessor {
247
246
this . _graphQLCache ,
248
247
this . _logger ,
249
248
) ;
250
- if ( this . _graphQLCache ?. getGraphQLConfig ( ) ) {
251
- const config = ( this . _graphQLConfig =
252
- this . _graphQLCache . getGraphQLConfig ( ) ) ;
249
+ if ( this . _graphQLConfig || this . _graphQLCache ?. getGraphQLConfig ) {
250
+ const config =
251
+ this . _graphQLConfig ?? this . _graphQLCache . getGraphQLConfig ( ) ;
253
252
await this . _cacheAllProjectFiles ( config ) ;
254
253
}
255
254
this . _isInitialized = true ;
@@ -296,6 +295,24 @@ export class MessageProcessor {
296
295
`\nfor more information on using 'graphql-config' with 'graphql-language-service-server', \nsee the documentation at ${ configDocLink } ` ,
297
296
) ;
298
297
}
298
+ async _isGraphQLConfigFile ( uri : string ) {
299
+ const configMatchers = [ 'graphql.config' , 'graphqlrc' , 'graphqlconfig' ] ;
300
+ if ( this . _settings ?. load ?. fileName ?. length ) {
301
+ configMatchers . push ( this . _settings . load . fileName ) ;
302
+ }
303
+
304
+ const fileMatch = configMatchers
305
+ . filter ( Boolean )
306
+ . some ( v => uri . match ( v ) ?. length ) ;
307
+ if ( fileMatch ) {
308
+ return fileMatch ;
309
+ }
310
+ if ( uri . match ( 'package.json' ) ?. length ) {
311
+ const graphqlConfig = await import ( URI . parse ( uri ) . fsPath ) ;
312
+ return Boolean ( graphqlConfig ?. graphql ) ;
313
+ }
314
+ return false ;
315
+ }
299
316
300
317
async handleDidOpenOrSaveNotification (
301
318
params : DidSaveTextDocumentParams | DidOpenTextDocumentParams ,
@@ -304,13 +321,16 @@ export class MessageProcessor {
304
321
* Initialize the LSP server when the first file is opened or saved,
305
322
* so that we can access the user settings for config rootDir, etc
306
323
*/
324
+ const isGraphQLConfigFile = await this . _isGraphQLConfigFile (
325
+ params . textDocument . uri ,
326
+ ) ;
307
327
try {
308
328
if ( ! this . _isInitialized || ! this . _graphQLCache ) {
309
329
// don't try to initialize again if we've already tried
310
330
// and the graphql config file or package.json entry isn't even there
311
- // if (this._isGraphQLConfigMissing === true) {
312
- // return null;
313
- // }
331
+ if ( this . _isGraphQLConfigMissing === true && ! isGraphQLConfigFile ) {
332
+ return null ;
333
+ }
314
334
// then initial call to update graphql config
315
335
await this . _updateGraphQLConfig ( ) ;
316
336
}
@@ -343,21 +363,7 @@ export class MessageProcessor {
343
363
344
364
await this . _invalidateCache ( textDocument , uri , contents ) ;
345
365
} else {
346
- const configMatchers = [
347
- 'graphql.config' ,
348
- 'graphqlrc' ,
349
- 'graphqlconfig' ,
350
- ] . filter ( Boolean ) ;
351
- if ( this . _settings ?. load ?. fileName ) {
352
- configMatchers . push ( this . _settings . load . fileName ) ;
353
- }
354
-
355
- const hasGraphQLConfigFile = configMatchers . some (
356
- v => uri . match ( v ) ?. length ,
357
- ) ;
358
- const hasPackageGraphQLConfig =
359
- uri . match ( 'package.json' ) ?. length && require ( uri ) ?. graphql ;
360
- if ( hasGraphQLConfigFile || hasPackageGraphQLConfig ) {
366
+ if ( isGraphQLConfigFile ) {
361
367
this . _logger . info ( 'updating graphql config' ) ;
362
368
await this . _updateGraphQLConfig ( ) ;
363
369
return { uri, diagnostics : [ ] } ;
0 commit comments