import { ContextBuilder , } from './builder' import { AuthMiddleWare } from './auth' const http = require('http') const path = require('path'); const express = require('express') const bodyParser = require('body-parser') const cors = require('cors') const app = express(); const httpServer = http.createServer(app) const graphqlServer = require('magic-graphql') const { Database } = require('./db'); export const Server = async (port, configs, router = null, opts = null ) => { try { const dbs = await Database(configs) if (router) { app.use(bodyParser.json()) app.use(express.json()); app.use( cors({ methods: ["GET", "POST", "PUT", "PATCH", "DELETE"], exposedHeaders: ["Content-Disposition"], credentials: true }) ); app.use('/', (req, res, next) => { req.dbs = dbs ; next(); } ,router) } graphqlServer(app, Object.assign({ modelDirPath: dbs, graphqlEndpint: '/graphql', subscriptions: true, httpServer: httpServer, dataloader: true, dataloaderOptions: { max: 500, cache: true, batch: true }, context:{ dbs }, contextWrapper: opts.auth ? AuthMiddleWare : null }, opts)) httpServer.listen({ port, secret: "crypto-signing" }, () => { console.log(`✅ Starting server`); console.log(`🚀 Server ready at Port:${port}`) }) return dbs; } catch (error) { console.log(error); } }