const knex = require('knex'); const db2g = require('db2graphql'); const { ApolloServer } = require('@apollo/server'); const { startStandaloneServer } = require('@apollo/server/standalone'); // dotenv - not used if running in docker compose // require('dotenv').config(); // get env variables const { DB_HOST, DB_PORT, DB_USERNAME, DB_PASSWORD, DB_DATABASE } = process.env; const start = async (cb) => { /**************************************/ const api = new db2g('mariadb', knex({ client: 'mysql', connection: { host: DB_HOST, port: DB_PORT, user: DB_USERNAME, password: DB_PASSWORD, database: DB_DATABASE } })); await api.connect(DB_DATABASE); // Connects to database and extracts database schema // Get generated schema and resolvers const schema = api.getSchema(); const resolvers = api.getResolvers(); /**************************************/ // Create Apollo Server and start if (!schema) throw new Error('Error: empty schema'); console.log(schema); const server = new ApolloServer({ typeDefs: schema, resolvers, introspection: true, playground: true, }); startStandaloneServer(server).then(({ url }) => { console.log(`🚀 Server ready at ${url}`); }); } start();