sourcegraph/client/shared/dev/generateGraphQlOperations.js
Erik Seliger c03cc7bd1e
Support multiple GraphQL schema files (#20077)
This PR adjusts all configs/generators/linters that we use to support multiple `.graphql` files to be stitched together for the final schema.
This should help a bit with the MASSIVE `schema.graphql` file that has grown to around 10k loc by now, by encapsulating different fields of concern into separate files. Using the `extend` keyword, there is nothing that cannot be expressed with separate files that could have been with a single file, so there are no drawbacks to this method, that I'm aware of.

In a follow-up PR, I will restructure the backend code for this a bit and make it so the enterprise schema is not used in OSS (which gives better introspection, because there is less noise from fields that aren't accessible because OSS) and doesn't require us to implement a `defaultXXResolver`.
2021-04-19 14:35:49 +02:00

113 lines
3.2 KiB
JavaScript

// @ts-check
const path = require('path')
const { generate } = require('@graphql-codegen/cli')
const ROOT_FOLDER = path.resolve(__dirname, '../../../')
const WEB_FOLDER = path.resolve(ROOT_FOLDER, './client/web')
const BROWSER_FOLDER = path.resolve(ROOT_FOLDER, './client/browser')
const SHARED_FOLDER = path.resolve(ROOT_FOLDER, './client/shared')
const SCHEMA_PATH = path.join(ROOT_FOLDER, './cmd/frontend/graphqlbackend/*.graphql')
const SHARED_DOCUMENTS_GLOB = [`${SHARED_FOLDER}/src/**/*.{ts,tsx}`, `!${SHARED_FOLDER}/src/testing/**/*.*`]
const WEB_DOCUMENTS_GLOB = [
`${WEB_FOLDER}/src/**/*.{ts,tsx}`,
`!${WEB_FOLDER}/src/regression/**/*.*`,
`!${WEB_FOLDER}/src/end-to-end/**/*.*`,
]
const BROWSER_DOCUMENTS_GLOB = [
`${BROWSER_FOLDER}/src/**/*.{ts,tsx}`,
`!${BROWSER_FOLDER}/src/end-to-end/**/*.*`,
'!**/*.d.ts',
]
// Define ALL_DOCUMENTS_GLOB as the union of the previous glob arrays.
const ALL_DOCUMENTS_GLOB = [...new Set([...SHARED_DOCUMENTS_GLOB, ...WEB_DOCUMENTS_GLOB, ...BROWSER_DOCUMENTS_GLOB])]
const plugins = [`${SHARED_FOLDER}/dev/extractGraphQlOperationCodegenPlugin.js`, 'typescript', 'typescript-operations']
/**
* Generates TypeScript files with types for all GraphQL operations.
*/
async function generateGraphQlOperations() {
await generate(
{
schema: SCHEMA_PATH,
hooks: {
afterOneFileWrite: 'prettier --write',
},
errorsOnly: true,
config: {
preResolveTypes: true,
operationResultSuffix: 'Result',
omitOperationSuffix: true,
skipTypename: true,
namingConvention: {
typeNames: 'keep',
enumValues: 'keep',
transformUnderscore: true,
},
declarationKind: 'interface',
avoidOptionals: {
field: true,
inputValue: false,
object: true,
},
scalars: {
DateTime: 'string',
JSON: 'object',
JSONValue: 'unknown',
GitObjectID: 'string',
JSONCString: 'string',
PublishedValue: "boolean | 'draft'",
},
},
generates: {
[path.join(BROWSER_FOLDER, './src/graphql-operations.ts')]: {
documents: BROWSER_DOCUMENTS_GLOB,
config: {
onlyOperationTypes: true,
noExport: false,
enumValues: '../../shared/src/graphql-operations',
interfaceNameForOperations: 'BrowserGraphQlOperations',
},
plugins,
},
[path.join(WEB_FOLDER, './src/graphql-operations.ts')]: {
documents: WEB_DOCUMENTS_GLOB,
config: {
onlyOperationTypes: true,
noExport: false,
enumValues: '../../shared/src/graphql-operations',
interfaceNameForOperations: 'WebGraphQlOperations',
},
plugins,
},
[path.join(SHARED_FOLDER, './src/graphql-operations.ts')]: {
documents: SHARED_DOCUMENTS_GLOB,
config: {
onlyOperationTypes: true,
noExport: false,
interfaceNameForOperations: 'SharedGraphQlOperations',
},
plugins,
},
},
},
true
)
}
module.exports = {
generateGraphQlOperations,
SHARED_DOCUMENTS_GLOB,
WEB_DOCUMENTS_GLOB,
ALL_DOCUMENTS_GLOB,
}