Skip to content

Commit c45c378

Browse files
authored
memoize reading lib/graphql/static/schema*.json files (github#28443)
1 parent c6e517a commit c45c378

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

lib/read-json-file.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ export function readCompressedJsonFileFallback(xpath) {
3131
}
3232
}
3333

34+
// This is used to make sure the `readCompressedJsonFileFallbackLazily()`
35+
// function isn't used with the same exact first argument more than once.
36+
const globalCacheCounter = {}
37+
3438
// Wrapper on readCompressedJsonFileFallback that initially only checks
3539
// if the file exists but doesn't read the content till you call it.
3640
export function readCompressedJsonFileFallbackLazily(xpath) {
@@ -57,7 +61,18 @@ export function readCompressedJsonFileFallbackLazily(xpath) {
5761
}
5862
}
5963
return () => {
60-
if (!cache.has(xpath)) cache.set(xpath, readCompressedJsonFileFallback(xpath))
64+
if (!cache.has(xpath)) {
65+
cache.set(xpath, readCompressedJsonFileFallback(xpath))
66+
if (globalCacheCounter[xpath]) {
67+
console.warn(
68+
"If this happens it's because the readCompressedJsonFileFallbackLazily " +
69+
'function has been called non-globally. Only use ' +
70+
'readCompressedJsonFileFallback once at module-level.'
71+
)
72+
throw new Error(`Globally reading the same file more than once (${xpath})`)
73+
}
74+
globalCacheCounter[xpath] = 1
75+
}
6176
return cache.get(xpath)
6277
}
6378
}

middleware/contextualizers/graphql.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { readCompressedJsonFileFallbackLazily } from '../../lib/read-json-file.js'
1+
import {
2+
readCompressedJsonFileFallbackLazily,
3+
readCompressedJsonFileFallback,
4+
} from '../../lib/read-json-file.js'
25
import { allVersions } from '../../lib/all-versions.js'
36
const previews = readCompressedJsonFileFallbackLazily('./lib/graphql/static/previews.json')
47
const upcomingChanges = readCompressedJsonFileFallbackLazily(
@@ -20,6 +23,17 @@ const explorerUrl =
2023
? 'https://graphql.github.com/explorer'
2124
: 'http://localhost:3000'
2225

26+
const graphQLVersionSchemaCache = new Map()
27+
function readGraphQLVersionSchema(graphqlVersion) {
28+
if (!graphQLVersionSchemaCache.has(graphqlVersion)) {
29+
graphQLVersionSchemaCache.set(
30+
graphqlVersion,
31+
readCompressedJsonFileFallback(`lib/graphql/static/schema-${graphqlVersion}.json`)
32+
)
33+
}
34+
return graphQLVersionSchemaCache.get(graphqlVersion)
35+
}
36+
2337
export default function graphqlContext(req, res, next) {
2438
const currentVersionObj = allVersions[req.context.currentVersion]
2539
// ignore requests to non-GraphQL reference paths
@@ -34,9 +48,7 @@ export default function graphqlContext(req, res, next) {
3448
const graphqlVersion = currentVersionObj.miscVersionName
3549

3650
req.context.graphql = {
37-
schemaForCurrentVersion: readCompressedJsonFileFallbackLazily(
38-
`lib/graphql/static/schema-${graphqlVersion}.json`
39-
)(),
51+
schemaForCurrentVersion: readGraphQLVersionSchema(graphqlVersion),
4052
previewsForCurrentVersion: previews()[graphqlVersion],
4153
upcomingChangesForCurrentVersion: upcomingChanges()[graphqlVersion],
4254
prerenderedObjectsForCurrentVersion: prerenderedObjects()[graphqlVersion],

0 commit comments

Comments
 (0)