You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.8KB

  1. import { Module } from '@nestjs/common';
  2. import { GraphQLModule} from '@nestjs/graphql';
  3. import { ConnectionContext } from 'subscriptions-transport-ws';
  4. import * as WebSocket from 'ws';
  5. import { Client } from './client';
  6. import { UUID } from './global/scalars/UUID';
  7. import { GlobalModule } from './global/global.module';
  8. import { PersonModule } from './person/person.module';
  9. import { ApparatusModule } from './apparatus/apparatus.module';
  10. import { OrganizerModule } from './organizer/organizer.module'
  11. import { EventModule } from './event/event.module'
  12. @Module({
  13. imports: [
  14. GlobalModule,
  15. PersonModule,
  16. ApparatusModule,
  17. OrganizerModule,
  18. EventModule,
  19. GraphQLModule.forRoot({
  20. installSubscriptionHandlers: true,
  21. autoSchemaFile: 'schema.gql',
  22. debug: false,
  23. playground: true,
  24. // path: 'gql',
  25. context: async ({ req, connection }) => {
  26. if (connection?.context?.client) {
  27. return { client: connection.context.client };
  28. }
  29. const client = new Client();
  30. if (req?.headers?.token) {
  31. await client.login({token: req.headers.token});
  32. }
  33. return { client }
  34. },
  35. subscriptions: {
  36. onConnect: (headers: { token: string, clientId: UUID }, websocket: any) => {
  37. const ip = websocket?.upgradeReq?.headers?.['x-forwarded-for'] || websocket._socket.remoteAddress;
  38. const client = new Client(headers.clientId, ip);
  39. if (headers.token) {
  40. client.login({token: headers.token}).then();
  41. }
  42. return { ...headers, client };
  43. },
  44. onDisconnect: (websocket: WebSocket, context: ConnectionContext) => {
  45. context.initPromise.then(() => {
  46. })
  47. },
  48. },
  49. resolvers: {},
  50. })
  51. ],
  52. controllers: [],
  53. providers: [],
  54. })
  55. export class AppModule {}