app.module.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. @Module({
  14. imports: [
  15. GraphQLModule,
  16. TypeOrmModule.forRoot({
  17. type: 'mysql',
  18. entities: ['./**/entity/**/*.entity.ts'],
  19. synchronize: true,
  20. logging: true,
  21. host: '192.168.99.100',
  22. port: 3306,
  23. username: 'root',
  24. password: '',
  25. database: 'test',
  26. }),
  27. ],
  28. controllers: [CustomerController],
  29. providers: [
  30. CustomerService,
  31. CustomerResolver,
  32. ProductService,
  33. ProductResolver,
  34. LocaleService,
  35. PasswordService
  36. ],
  37. })
  38. export class AppModule implements NestModule {
  39. constructor(private readonly graphQLFactory: GraphQLFactory) {}
  40. configure(consumer: MiddlewareConsumer) {
  41. const schema = this.createSchema();
  42. consumer
  43. .apply(
  44. graphiqlExpress({
  45. endpointURL: '/graphql',
  46. subscriptionsEndpoint: `ws://localhost:3001/subscriptions`,
  47. }),
  48. )
  49. .forRoutes('/graphiql')
  50. .apply(graphqlExpress(req => ({ schema, rootValue: req })))
  51. .forRoutes('/graphql');
  52. }
  53. createSchema() {
  54. const typeDefs = this.graphQLFactory.mergeTypesByPaths('./**/*.graphql');
  55. return this.graphQLFactory.createSchema({ typeDefs });
  56. }
  57. }