1
0

app.module.ts 1.6 KB

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