app.module.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
  2. import { GraphQLModule } from '@nestjs/graphql';
  3. import { GraphQLFactory } from '@nestjs/graphql';
  4. import { TypeOrmModule } from '@nestjs/typeorm';
  5. import { graphiqlExpress, graphqlExpress } from 'apollo-server-express';
  6. import { AuthController } from './api/auth/auth.controller';
  7. import { CustomerController } from './api/customer/customer.controller';
  8. import { CustomerResolver } from './api/customer/customer.resolver';
  9. import { CustomerService } from './api/customer/customer.service';
  10. import { ProductResolver } from './api/product/product.resolver';
  11. import { ProductService } from './api/product/product.service';
  12. import { AuthService } from './auth/auth.service';
  13. import { JwtStrategy } from './auth/jwt.strategy';
  14. import { PasswordService } from './auth/password.service';
  15. import { LocaleService } from './locale/locale.service';
  16. @Module({
  17. imports: [
  18. GraphQLModule,
  19. TypeOrmModule.forRoot({
  20. type: 'mysql',
  21. entities: ['./**/entity/**/*.entity.ts'],
  22. synchronize: true,
  23. logging: true,
  24. host: '192.168.99.100',
  25. port: 3306,
  26. username: 'root',
  27. password: '',
  28. database: 'test',
  29. }),
  30. ],
  31. controllers: [AuthController, CustomerController],
  32. providers: [
  33. AuthService,
  34. JwtStrategy,
  35. PasswordService,
  36. CustomerService,
  37. CustomerResolver,
  38. ProductService,
  39. ProductResolver,
  40. LocaleService,
  41. PasswordService,
  42. ],
  43. })
  44. export class AppModule implements NestModule {
  45. constructor(private readonly graphQLFactory: GraphQLFactory) {}
  46. configure(consumer: MiddlewareConsumer) {
  47. const schema = this.createSchema();
  48. consumer
  49. .apply(
  50. graphiqlExpress({
  51. endpointURL: '/graphql',
  52. subscriptionsEndpoint: `ws://localhost:3001/subscriptions`,
  53. }),
  54. )
  55. .forRoutes('/graphiql')
  56. .apply(graphqlExpress(req => ({ schema, rootValue: req })))
  57. .forRoutes('/graphql');
  58. }
  59. createSchema() {
  60. const typeDefs = this.graphQLFactory.mergeTypesByPaths('./**/*.graphql');
  61. return this.graphQLFactory.createSchema({ typeDefs });
  62. }
  63. }