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

Commit 8766711

Browse files
committed
Update cache key logic
- pass directly through to extendschema to avoid duplicate logic - use endpoint name instead of url (as this may not be unique)
1 parent 249e78e commit 8766711

File tree

1 file changed

+51
-24
lines changed

1 file changed

+51
-24
lines changed

packages/server/src/GraphQLCache.js

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import {
4242
INPUT_OBJECT_TYPE_EXTENSION,
4343
DIRECTIVE_DEFINITION,
4444
} from 'graphql/language/kinds';
45-
import {getGraphQLConfig, GraphQLConfig} from 'graphql-config';
45+
import {getGraphQLConfig, GraphQLConfig, GraphQLEndpoint} from 'graphql-config';
4646
import {getQueryAndRange} from './MessageProcessor';
4747
import stringToHash from './stringToHash';
4848
import glob from 'glob';
@@ -370,8 +370,8 @@ export class GraphQLCache implements GraphQLCacheInterface {
370370

371371
_extendSchema(
372372
schema: GraphQLSchema,
373-
schemaPath: string,
374-
projectName: string,
373+
schemaPath: ?string,
374+
schemaCacheKey: ?string,
375375
): GraphQLSchema {
376376
const graphQLFileMap = this._graphQLFileListCache.get(this._configDir);
377377
const typeExtensions = [];
@@ -405,22 +405,25 @@ export class GraphQLCache implements GraphQLCacheInterface {
405405
});
406406
});
407407
});
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-
});
413408

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);
421425
}
422426

423-
this._typeExtensionMap.set(typeExtCacheKey, hash);
424427
return extendSchema(schema, {
425428
kind: DOCUMENT,
426429
definitions: typeExtensions,
@@ -439,26 +442,25 @@ export class GraphQLCache implements GraphQLCacheInterface {
439442

440443
const projectName = appName || 'undefinedName';
441444
const schemaPath = projectConfig.schemaPath;
442-
const endpointsExtension = projectConfig.endpointsExtension;
443-
const endpoint = endpointsExtension && endpointsExtension.getEndpoint(); // XXX pass name?
445+
const endpointInfo = this._getDefaultEndpoint(projectConfig);
444446

445447
let schemaCacheKey = null;
446448
let schema = null;
447449

448-
if (endpoint) {
449-
schemaCacheKey = `${endpoint.url}:${projectName}`;
450+
if (endpointInfo) {
451+
schemaCacheKey = `${endpointInfo.endpointName}:${projectName}`;
450452

451453
// Maybe use cache
452454
if (this._schemaMap.has(schemaCacheKey)) {
453455
schema = this._schemaMap.get(schemaCacheKey);
454456
return schema && queryHasExtensions
455-
? this._extendSchema(schema, schemaPath, projectName)
457+
? this._extendSchema(schema, schemaPath, schemaCacheKey)
456458
: schema;
457459
}
458460

459461
// Read schema from network
460462
try {
461-
schema = await endpoint.resolveSchema();
463+
schema = await endpointInfo.endpoint.resolveSchema();
462464
} catch (failure) {
463465
// Never mind
464466
}
@@ -471,7 +473,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
471473
if (this._schemaMap.has(schemaCacheKey)) {
472474
schema = this._schemaMap.get(schemaCacheKey);
473475
return schema && queryHasExtensions
474-
? this._extendSchema(schema, schemaPath, projectName)
476+
? this._extendSchema(schema, schemaPath, schemaCacheKey)
475477
: schema;
476478
}
477479

@@ -490,7 +492,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
490492
}
491493

492494
if (this._graphQLFileListCache.has(this._configDir)) {
493-
schema = this._extendSchema(schema, schemaPath, projectName);
495+
schema = this._extendSchema(schema, schemaPath, schemaCacheKey);
494496
}
495497

496498
if (schemaCacheKey) {
@@ -499,6 +501,31 @@ export class GraphQLCache implements GraphQLCacheInterface {
499501
return schema;
500502
};
501503

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+
502529
/**
503530
* Given a list of GraphQL file metadata, read all files collected from watchman
504531
* and create fragmentDefinitions and GraphQL files cache.

0 commit comments

Comments
 (0)