backend/lib/seed/app.ts
2025-05-14 21:45:16 +02:00

73 lines
2.5 KiB
TypeScript

import 'reflect-metadata';
import { buildSchema, NonEmptyArray } from 'type-graphql';
import { authMiddleware, errorMiddleware, ctxMiddleware, complexityMiddleware } from '@seed/graphql/Middleware';
import Firebase from '@seed/services/auth/FirebaseService';
import { AppResolvers } from '@src/__indexes/__resolvers.index';
import { SettingsCache } from './graphql/Settings';
import { runBeforeAll } from '@src/__indexes/__bootstrap';
import { nullToUndefined } from './helpers/Utils';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { ApolloServer } = require('apollo-server');
const PORT = process.env.PORT || 4000;
const bootstrap = async (): Promise<any> => {
process.env.NODE_ENV = 'local';
const settingsI = SettingsCache.getInstance();
await settingsI.refreshCache();
try {
const schema = await buildSchema({
resolvers: (AppResolvers as unknown) as NonEmptyArray<Function>,
authChecker: authMiddleware,
validate: false, // disable automatic validation or pass the default config object
});
const server = new ApolloServer({
// typeDefs,
// resolvers,
schema,
formatError: errorMiddleware,
formatResponse: (response): any => {
return nullToUndefined(response);
},
context: ctxMiddleware,
tracing: false,
plugins: [
{
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
requestDidStart: () => ({
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
didResolveOperation({ request, document }) {
return complexityMiddleware(schema, request.variables, document);
},
}),
},
],
});
try {
const token = await Firebase.getInstance().createTokenId(process.env.TEST_USER_EMAIL ? process.env.TEST_USER_EMAIL : '');
console.log(`Token for ${process.env.TEST_USER_EMAIL}`, token.idToken);
} catch (error) {
console.log('no token available');
}
await runBeforeAll();
server.listen(PORT).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
} catch (error) {
console.log('[GRAPH ERROR]', error);
throw error;
}
};
bootstrap();