app.module.ts 1.9 KB

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