1
0
mirror of synced 2025-12-20 02:19:14 -05:00
Files
docs/middleware/contextualizers/graphql.js
Kevin Heis a92d820888 Remove remaining JSON requires (#20110)
* Remove remaining JSON requires

* Lint

* Update feature-flags.js
2021-06-23 14:57:50 -07:00

40 lines
1.8 KiB
JavaScript

const fs = require('fs')
const path = require('path')
const readJsonFile = require('../../lib/read-json-file')
const previews = readJsonFile('./lib/graphql/static/previews.json')
const upcomingChanges = readJsonFile('./lib/graphql/static/upcoming-changes.json')
const changelog = readJsonFile('./lib/graphql/static/changelog.json')
const prerenderedObjects = readJsonFile('./lib/graphql/static/prerendered-objects.json')
const prerenderedInputObjects = readJsonFile('./lib/graphql/static/prerendered-input-objects.json')
const allVersions = require('../../lib/all-versions')
const explorerUrl = process.env.NODE_ENV === 'production'
? 'https://graphql.github.com/explorer'
: 'http://localhost:3000'
module.exports = 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,
// enterprise-server@2.22 corresponds to ghes-2.22,
// and github-ae@latest corresponds to ghae
const graphqlVersion = currentVersionObj.miscVersionName
req.context.graphql = {
schemaForCurrentVersion: JSON.parse(fs.readFileSync(path.join(process.cwd(), `lib/graphql/static/schema-${graphqlVersion}.json`))),
previewsForCurrentVersion: previews[graphqlVersion],
upcomingChangesForCurrentVersion: upcomingChanges[graphqlVersion],
prerenderedObjectsForCurrentVersion: prerenderedObjects[graphqlVersion],
prerenderedInputObjectsForCurrentVersion: prerenderedInputObjects[graphqlVersion],
explorerUrl,
changelog
}
return next()
}