@@ -42,7 +42,7 @@ import {
42
42
INPUT_OBJECT_TYPE_EXTENSION ,
43
43
DIRECTIVE_DEFINITION ,
44
44
} from 'graphql/language/kinds' ;
45
- import { getGraphQLConfig , GraphQLConfig } from 'graphql-config' ;
45
+ import { getGraphQLConfig , GraphQLConfig , GraphQLEndpoint } from 'graphql-config' ;
46
46
import { getQueryAndRange } from './MessageProcessor' ;
47
47
import stringToHash from './stringToHash' ;
48
48
import glob from 'glob' ;
@@ -370,8 +370,8 @@ export class GraphQLCache implements GraphQLCacheInterface {
370
370
371
371
_extendSchema (
372
372
schema : GraphQLSchema ,
373
- schemaPath : string ,
374
- projectName : string ,
373
+ schemaPath : ? string ,
374
+ schemaCacheKey : ? string ,
375
375
) : GraphQLSchema {
376
376
const graphQLFileMap = this . _graphQLFileListCache . get ( this . _configDir ) ;
377
377
const typeExtensions = [ ] ;
@@ -405,22 +405,25 @@ export class GraphQLCache implements GraphQLCacheInterface {
405
405
} ) ;
406
406
} ) ;
407
407
} ) ;
408
- const sorted = typeExtensions . sort ( ( a : any , b : any ) => {
409
- const aName = a . definition ? a . definition . name . value : a . name . value ;
410
- const bName = b . definition ? b . definition . name . value : b . name . value ;
411
- return aName > bName ? 1 : - 1 ;
412
- } ) ;
413
408
414
- const hash = stringToHash ( JSON . stringify ( sorted ) ) ;
415
- const typeExtCacheKey = `${schemaPath } :${projectName } `;
416
- if (
417
- this . _typeExtensionMap . has ( typeExtCacheKey ) &&
418
- this . _typeExtensionMap . get ( typeExtCacheKey ) === hash
419
- ) {
420
- return schema ;
409
+ if ( schemaCacheKey ) {
410
+ const sorted = typeExtensions . sort ( ( a : any , b : any ) => {
411
+ const aName = a . definition ? a . definition . name . value : a . name . value ;
412
+ const bName = b . definition ? b . definition . name . value : b . name . value ;
413
+ return aName > bName ? 1 : - 1 ;
414
+ } ) ;
415
+ const hash = stringToHash ( JSON . stringify ( sorted ) ) ;
416
+
417
+ if (
418
+ this . _typeExtensionMap . has ( schemaCacheKey ) &&
419
+ this . _typeExtensionMap . get ( schemaCacheKey ) === hash
420
+ ) {
421
+ return schema ;
422
+ }
423
+
424
+ this . _typeExtensionMap . set ( schemaCacheKey , hash ) ;
421
425
}
422
426
423
- this . _typeExtensionMap . set ( typeExtCacheKey , hash ) ;
424
427
return extendSchema ( schema , {
425
428
kind : DOCUMENT ,
426
429
definitions : typeExtensions ,
@@ -439,26 +442,25 @@ export class GraphQLCache implements GraphQLCacheInterface {
439
442
440
443
const projectName = appName || 'undefinedName' ;
441
444
const schemaPath = projectConfig . schemaPath ;
442
- const endpointsExtension = projectConfig . endpointsExtension ;
443
- const endpoint = endpointsExtension && endpointsExtension . getEndpoint ( ) ; // XXX pass name?
445
+ const endpointInfo = this . _getDefaultEndpoint ( projectConfig ) ;
444
446
445
447
let schemaCacheKey = null ;
446
448
let schema = null ;
447
449
448
- if ( endpoint ) {
449
- schemaCacheKey = `${ endpoint . url } :${ projectName } ` ;
450
+ if ( endpointInfo ) {
451
+ schemaCacheKey = `${ endpointInfo . endpointName } :${ projectName } ` ;
450
452
451
453
// Maybe use cache
452
454
if ( this . _schemaMap . has ( schemaCacheKey ) ) {
453
455
schema = this . _schemaMap . get ( schemaCacheKey ) ;
454
456
return schema && queryHasExtensions
455
- ? this . _extendSchema ( schema , schemaPath , projectName )
457
+ ? this . _extendSchema ( schema , schemaPath , schemaCacheKey )
456
458
: schema ;
457
459
}
458
460
459
461
// Read schema from network
460
462
try {
461
- schema = await endpoint . resolveSchema ( ) ;
463
+ schema = await endpointInfo . endpoint . resolveSchema ( ) ;
462
464
} catch ( failure ) {
463
465
// Never mind
464
466
}
@@ -471,7 +473,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
471
473
if ( this . _schemaMap . has ( schemaCacheKey ) ) {
472
474
schema = this . _schemaMap . get ( schemaCacheKey ) ;
473
475
return schema && queryHasExtensions
474
- ? this . _extendSchema ( schema , schemaPath , projectName )
476
+ ? this . _extendSchema ( schema , schemaPath , schemaCacheKey )
475
477
: schema ;
476
478
}
477
479
@@ -490,7 +492,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
490
492
}
491
493
492
494
if ( this . _graphQLFileListCache . has ( this . _configDir ) ) {
493
- schema = this . _extendSchema ( schema , schemaPath , projectName ) ;
495
+ schema = this . _extendSchema ( schema , schemaPath , schemaCacheKey ) ;
494
496
}
495
497
496
498
if ( schemaCacheKey ) {
@@ -499,6 +501,31 @@ export class GraphQLCache implements GraphQLCacheInterface {
499
501
return schema ;
500
502
} ;
501
503
504
+ _getDefaultEndpoint (
505
+ projectConfig : GraphQLProjectConfig ,
506
+ ) : ?{ endpointName : string , endpoint : GraphQLEndpoint } {
507
+ // Jumping through hoops to get the default endpoint by name (needed for cache key)
508
+ const endpointsExtension = projectConfig . endpointsExtension ;
509
+ if ( ! endpointsExtension ) {
510
+ return null ;
511
+ }
512
+
513
+ const defaultRawEndpoint = endpointsExtension . getRawEndpoint ( ) ;
514
+ const rawEndpointsMap = endpointsExtension . getRawEndpointsMap ( ) ;
515
+ const endpointName = Object . keys ( rawEndpointsMap ) . find (
516
+ name => rawEndpointsMap [ name ] === defaultRawEndpoint ,
517
+ ) ;
518
+
519
+ if ( ! endpointName ) {
520
+ return null ;
521
+ }
522
+
523
+ return {
524
+ endpointName ,
525
+ endpoint : endpointsExtension . getEndpoint ( endpointName ) ,
526
+ } ;
527
+ }
528
+
502
529
/**
503
530
* Given a list of GraphQL file metadata, read all files collected from watchman
504
531
* and create fragmentDefinitions and GraphQL files cache.
0 commit comments