Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private async init() {
this.backendCreator = new GraphQLBackendCreator(this.model, this.config);
this.backend = await this.backendCreator.createBackend("sqlite3");
}
export async function generateTypeDefs(
model: string,
config: { [id: string]: any },
) {
const creator = new GraphQLBackendCreator(model, config);
const backend = await creator.createBackend("sqlite3");
const { typeDefs } = sourceModule(transpile(backend.schema));
return typeDefs;
}
export async function generateDatabase(
model: string,
config: { [id: string]: any },
) {
const backend: GraphQLBackendCreator = new GraphQLBackendCreator(
model,
config,
);
const connectionConfig = {
filename: `${folders.database}/db.sqlite`,
};
const manager = new DatabaseSchemaManager("sqlite3", connectionConfig);
backend.registerDataResourcesManager(manager);
await backend.createDatabase();
}
const models = new GlobSync(`${folders.model}/*.graphql`)
if (models.found.length === 0) {
logError(`No graphql file found inside ${folders.model} folder.`)
process.exit(0)
}
const pathForSchema: string = folders.schema
const outputSchemaPath: string = join(pathForSchema, 'generated.ts')
const customResolvers: string = join(folders.resolvers, "/custom")
const generatedResolvers: string = join(folders.resolvers, "/generated")
const schemaText = loadSchema(folders.model);
const backend: GraphQLBackendCreator = new GraphQLBackendCreator(schemaText, graphqlCRUD)
const generated: IGraphQLBackend = await backend.createBackend({ format: 'ts' })
checkAndCreateFolders(pathForSchema, customResolvers, generatedResolvers);
generated.resolvers.custom.forEach((output: OutputResolver) => {
if (!existsSync(`${customResolvers}/${output.name}.ts`) || output.name === 'index') {
writeFileSync(`${customResolvers}/${output.name}.ts`, output.output)
}
})
writeFileSync(outputSchemaPath, generated.schema)
writeFileSync(`${folders.resolvers}/index.ts`, generated.resolvers.index)
generated.resolvers.types.forEach((output: OutputResolver) => writeFileSync(`${generatedResolvers}/${output.name}.ts`, output.output))
if (client) {
export const createRuntime = async (client: Knex) => {
const schemaText = loadSchema(jsonConfig.folders.model);
const backend = new GraphQLBackendCreator(schemaText, jsonConfig.graphqlCRUD);
const dbClientProvider = new PgKnexDBDataProvider(client);
const dbConfig = {
client: jsonConfig.db.database,
connection: jsonConfig.db.dbConfig
};
migrateDB(dbConfig, schemaText).then((ops) => {
console.log(ops);
});
const pubSub = new PubSub();
const runtime = await backend.createRuntime(dbClientProvider, pubSub);
const generatedSchema = runtime.schema;
const executableSchema = makeExecutableSchema({
export const createRuntime = async (client: Knex) => {
const schemaText = loadSchema(jsonConfig.folders.model);
const backend = new GraphQLBackendCreator(schemaText, jsonConfig.graphqlCRUD);
const dbClientProvider = new PgKnexDBDataProvider(client);
const dbConfig = {
client: jsonConfig.db.database,
connection: jsonConfig.db.dbConfig
};
migrate(dbConfig, schemaText, {
dbSchemaName: jsonConfig.db.schema
}).then((ops) => {
console.log(ops);
});
const pubSub = new PubSub();
const runtime = await backend.createRuntime(dbClientProvider, pubSub);
const generatedSchema = runtime.schema;
logError(`No graphql file found inside ${process.cwd()}/model folder.`)
process.exit(0)
}
const configPath = `${process.cwd()}/config.json`
const { database, generation } = JSON.parse(readFileSync(configPath, "utf8"))
const { templateType } = JSON.parse(readFileSync(`${process.cwd()}/.template.json`, 'utf8'))
const path: string = process.cwd()
const schemaText: string = models.found.map((m: string) => readFileSync(`${path}/${m}`, 'utf8')).join('\n')
const outputSchemaPath: string = `${process.cwd()}/src/schema/generated.ts`
const outputResolverPath: string = `${process.cwd()}/src/resolvers`
const backend: GraphQLBackendCreator = new GraphQLBackendCreator(schemaText, generation)
const generated: IGraphQLBackend = await backend.createBackend(database, templateType)
writeFileSync(outputSchemaPath, generated.schema)
writeFileSync(`${outputResolverPath}/index.ts`, generated.resolvers.index)
if(!existsSync(`${outputResolverPath}/custom`)) {
mkdirSync(`${outputResolverPath}/custom`)
}
if(!existsSync(`${outputResolverPath}/generated`)) {
mkdirSync(`${outputResolverPath}/generated`)
}
generated.resolvers.custom.forEach((output: OutputResolver) => {
export const createRuntime = async (client: Knex, pubSub: PubSub) => {
const runtimeSchema = readFileSync(resolve(__dirname, "./runtime.graphql"), 'utf8');
const backend = new GraphQLBackendCreator(runtimeSchema, jsonConfig.graphqlCRUD);
const dbClientProvider = new PgKnexDBDataProvider(client);
const runtime = await backend.createRuntime(dbClientProvider, pubSub);
const generatedSchema = runtime.schema;
const executableSchema = makeExecutableSchema({
typeDefs: gql`${generatedSchema}`,
resolvers: runtime.resolvers,
resolverValidationOptions: {
requireResolversForResolveType: false
}
});
return executableSchema;
}