forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql.js
50 lines (46 loc) · 2.1 KB
/
graphql.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { readCompressedJsonFileFallbackLazily } from '../../lib/read-json-file.js'
import { allVersions } from '../../lib/all-versions.js'
const previews = readCompressedJsonFileFallbackLazily('./lib/graphql/static/previews.json')
const upcomingChanges = readCompressedJsonFileFallbackLazily(
'./lib/graphql/static/upcoming-changes.json'
)
const changelog = readCompressedJsonFileFallbackLazily('./lib/graphql/static/changelog.json')
const prerenderedObjects = readCompressedJsonFileFallbackLazily(
'./lib/graphql/static/prerendered-objects.json'
)
const prerenderedInputObjects = readCompressedJsonFileFallbackLazily(
'./lib/graphql/static/prerendered-input-objects.json'
)
const prerenderedMutations = readCompressedJsonFileFallbackLazily(
'./lib/graphql/static/prerendered-mutations.json'
)
const explorerUrl =
process.env.NODE_ENV === 'production'
? 'https://graphql.github.com/explorer'
: 'http://localhost:3000'
export default function graphqlContext(req, res, next) {
const currentVersionObj = allVersions[req.context.currentVersion]
// ignore requests to non-GraphQL reference paths
// and to versions that don't exist
if (!req.pagePath.includes('/graphql/') || !currentVersionObj) {
return next()
}
// Get the relevant name of the GraphQL schema files for the current version
// For example, free-pro-team@latest corresponds to dotcom,
// [email protected] corresponds to ghes-2.22,
// and github-ae@latest corresponds to ghae
const graphqlVersion = currentVersionObj.miscVersionName
req.context.graphql = {
schemaForCurrentVersion: readCompressedJsonFileFallbackLazily(
`lib/graphql/static/schema-${graphqlVersion}.json`
)(),
previewsForCurrentVersion: previews()[graphqlVersion],
upcomingChangesForCurrentVersion: upcomingChanges()[graphqlVersion],
prerenderedObjectsForCurrentVersion: prerenderedObjects()[graphqlVersion],
prerenderedInputObjectsForCurrentVersion: prerenderedInputObjects()[graphqlVersion],
prerenderedMutationsForCurrentVersion: prerenderedMutations()[graphqlVersion],
explorerUrl,
changelog: changelog(),
}
return next()
}