103 lines
3.6 KiB
TypeScript
103 lines
3.6 KiB
TypeScript
import { nullToUndefined } from '@seed/helpers/Utils';
|
|
import { ApolloServer } from 'apollo-server-lambda';
|
|
import { buildSchema } from 'type-graphql';
|
|
import { ctxMiddleware, authMiddleware, errorMiddleware, complexityMiddleware } from './Middleware';
|
|
|
|
import { SettingsCache } from './Settings';
|
|
|
|
export async function createServer(resolversEntry: any): Promise<ApolloServer> {
|
|
const schema = await buildSchema({
|
|
resolvers: resolversEntry,
|
|
authChecker: authMiddleware,
|
|
validate: false,
|
|
});
|
|
|
|
const server = new ApolloServer({
|
|
// typeDefs,
|
|
// resolvers,
|
|
schema,
|
|
formatError: errorMiddleware,
|
|
formatResponse: (response): any => {
|
|
return nullToUndefined(response);
|
|
},
|
|
context: ctxMiddleware,
|
|
tracing: true,
|
|
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);
|
|
},
|
|
}),
|
|
},
|
|
],
|
|
});
|
|
return server;
|
|
}
|
|
|
|
export class ServerInstance {
|
|
private static instance: ServerInstance;
|
|
public handler!: any;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
private constructor() {}
|
|
|
|
public static async getInstance(resolversEntry?: any): Promise<ServerInstance> {
|
|
const settingsI = SettingsCache.getInstance();
|
|
await settingsI.refreshCache();
|
|
|
|
if (!this.instance) {
|
|
const instance = new ServerInstance();
|
|
|
|
try {
|
|
// let { typeDefs, resolvers } = await buildTypeDefsAndResolvers({
|
|
// resolvers: resolversEntry,
|
|
// authChecker: authMiddleware,
|
|
// });
|
|
|
|
// typeDefs = typeDefs.replace('scalar Upload', 'scalar UploadFix');
|
|
// resolvers = resolvers;
|
|
|
|
const server = await createServer(resolversEntry);
|
|
instance.handler = server.createHandler({
|
|
cors: {
|
|
origin: '*',
|
|
credentials: true,
|
|
allowedHeaders: 'Content-Type,Authorization,X-Api-Key,X-Amz-Security-Token,X-Amz-User-Agent,organisationid',
|
|
},
|
|
});
|
|
console.log(`[GraphQL] Using new server in ${process.env.NODE_ENV}`);
|
|
this.instance = instance;
|
|
} catch (error) {
|
|
console.log('[GRAPH ERROR]', error);
|
|
throw error;
|
|
}
|
|
} else {
|
|
console.log(`[GraphQL] Using same server in ${process.env.NODE_ENV}`);
|
|
}
|
|
|
|
return this.instance;
|
|
}
|
|
|
|
public run(event: AWSLambda.APIGatewayProxyEvent, context: AWSLambda.Context): Promise<any> {
|
|
try {
|
|
console.log('BEGIN ACTION', event.body ? JSON.parse(event.body) : 'NO BODY');
|
|
} catch (error) {
|
|
console.log('BEGIN ACTION - Not JSON');
|
|
}
|
|
return new Promise((resolve, reject) => {
|
|
const callback = (error: any, body: unknown): void => {
|
|
console.log('END ACTION');
|
|
if (error) {
|
|
console.error(error);
|
|
reject(error);
|
|
} else resolve(body);
|
|
};
|
|
|
|
this.handler(event, context, callback);
|
|
});
|
|
}
|
|
}
|