app.module.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. @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. ProductResolver,
  39. PasswordService,
  40. ],
  41. })
  42. export class AppModule implements NestModule {
  43. constructor(private readonly graphQLFactory: GraphQLFactory) {}
  44. configure(consumer: MiddlewareConsumer) {
  45. const schema = this.createSchema();
  46. consumer
  47. .apply(
  48. graphiqlExpress({
  49. endpointURL: '/graphql',
  50. subscriptionsEndpoint: `ws://localhost:3001/subscriptions`,
  51. }),
  52. )
  53. .forRoutes('/graphiql')
  54. .apply(graphqlExpress(req => ({ schema, rootValue: req })))
  55. .forRoutes('/graphql');
  56. }
  57. createSchema() {
  58. const typeDefs = this.graphQLFactory.mergeTypesByPaths('./**/*.graphql');
  59. return this.graphQLFactory.createSchema({ typeDefs });
  60. }
  61. }