app.module.ts 2.3 KB

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