mirror of
https://github.com/wassname/talk.git
synced 2026-08-11 11:27:10 +08:00
feature: added generated graph types
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
const { Linter, Configuration } = require("tslint");
|
||||
const { generateTSTypesAsString } = require("graphql-schema-typescript");
|
||||
const { getGraphQLConfig } = require("graphql-config");
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
|
||||
function lint(files) {
|
||||
const linter = new Linter({ fix: true });
|
||||
|
||||
for (const { fileName, types } of files) {
|
||||
const configuration = Configuration.findConfiguration(null, fileName)
|
||||
.results;
|
||||
linter.lint(fileName, types, configuration);
|
||||
}
|
||||
}
|
||||
|
||||
function getFileName(name) {
|
||||
return path.join(
|
||||
__dirname,
|
||||
"..",
|
||||
"src",
|
||||
"core",
|
||||
"server",
|
||||
"graph",
|
||||
name,
|
||||
"schema",
|
||||
"__generated__",
|
||||
"types.ts"
|
||||
);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const config = getGraphQLConfig(__dirname);
|
||||
const projects = config.getProjects();
|
||||
|
||||
const files = [
|
||||
{
|
||||
name: "tenant",
|
||||
fileName: getFileName("tenant"),
|
||||
config: {
|
||||
contextType: "TenantContext",
|
||||
importStatements: [
|
||||
'import { Cursor } from "talk-server/models/connection";',
|
||||
'import TenantContext from "talk-server/graph/tenant/context";',
|
||||
],
|
||||
customScalarType: { Cursor: "Cursor", Time: "string" },
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "management",
|
||||
fileName: getFileName("management"),
|
||||
config: {
|
||||
contextType: "ManagementContext",
|
||||
importStatements: [
|
||||
'import ManagementContext from "talk-server/graph/management/context";',
|
||||
],
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
for (const file of files) {
|
||||
// Load the graph schema.
|
||||
const schema = projects[file.name].getSchema();
|
||||
|
||||
// Create the generated directory.
|
||||
const dir = path.dirname(file.fileName);
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir);
|
||||
}
|
||||
|
||||
// Create the types for this file.
|
||||
file.types = await generateTSTypesAsString(schema, {
|
||||
tabSpaces: 2,
|
||||
typePrefix: "GQL",
|
||||
contextType: "any",
|
||||
strictNulls: false,
|
||||
minimizeInterfaceImplementation: true,
|
||||
...file.config,
|
||||
});
|
||||
}
|
||||
|
||||
// Send the files off to the linter to be linted and written.
|
||||
lint(files);
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
main()
|
||||
.then(files => {
|
||||
for (const { fileName } of files) {
|
||||
console.log(`Generated ${fileName}`);
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
});
|
||||
Reference in New Issue
Block a user