app.module.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
  2. import { TypeOrmModule } from '@nestjs/typeorm';
  3. import { GraphQLModule } from '@nestjs/graphql';
  4. import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';
  5. import { GraphQLFactory } from '@nestjs/graphql';
  6. import { CustomerService } from './api/customer/customer.service';
  7. import { CustomerController } from './api/customer/customer.controller';
  8. import { CustomerResolver } from './api/customer/customer.resolver';
  9. import { ProductService } from './api/product/product.service';
  10. import { ProductResolver } from './api/product/product.resolver';
  11. import { LocaleService } from './locale/locale.service';
  12. import { PasswordService } from "./auth/password.service";
  13. import { AuthService } from "./auth/auth.service";
  14. import { AuthController } from "./api/auth/auth.controller";
  15. import { JwtStrategy } from "./auth/jwt.strategy";
  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: [
  32. AuthController,
  33. CustomerController
  34. ],
  35. providers: [
  36. AuthService,
  37. JwtStrategy,
  38. PasswordService,
  39. CustomerService,
  40. CustomerResolver,
  41. ProductService,
  42. ProductResolver,
  43. LocaleService,
  44. PasswordService
  45. ],
  46. })
  47. export class AppModule implements NestModule {
  48. constructor(private readonly graphQLFactory: GraphQLFactory) {}
  49. configure(consumer: MiddlewareConsumer) {
  50. const schema = this.createSchema();
  51. consumer
  52. .apply(
  53. graphiqlExpress({
  54. endpointURL: '/graphql',
  55. subscriptionsEndpoint: `ws://localhost:3001/subscriptions`,
  56. }),
  57. )
  58. .forRoutes('/graphiql')
  59. .apply(graphqlExpress(req => ({ schema, rootValue: req })))
  60. .forRoutes('/graphql');
  61. }
  62. createSchema() {
  63. const typeDefs = this.graphQLFactory.mergeTypesByPaths('./**/*.graphql');
  64. return this.graphQLFactory.createSchema({ typeDefs });
  65. }
  66. }