繁体   English   中英

如何在 neo4j-graphql-js 端点中使用带有 nestjs 框架的拦截器?

[英]How can I use interceptor in neo4j-graphql-js endpoint with nestjs framework?

我正在使用 neo4j-graphql-js 库将 graphql 查询转换为 cypher,我需要实现一个拦截器来验证返回的内容是否属于请求它的用户。 为此,我需要实现拦截器,但我遇到的问题是我没有解析器,因为它会生成利比里亚。 如何通过拦截器使其成为 go ? 如果无法使用拦截器,有没有办法在响应中实现一个中间件?

我正在使用nestjs 框架。 我使用 neo4j 数据库。

谢谢你。

模块:

@Module({
  imports: [
    GraphQLModule.forRootAsync({
      useClass: GraphqlConfigService,
    }),
  ],
  providers: [neo4jProvider],
})

export class GraphqlModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(GraphQLAuthMiddleware,GraphQLRoleMiddleware)
      .forRoutes('graphql');
  }

}

@Injectable()
export class GraphqlConfigService implements GqlOptionsFactory {
  async createGqlOptions(): Promise<GqlModuleOptions> {
   const schema = buildSchema();

    return {
      playground: true,
      schema: schema,
      path: '/graphql/queries',
      context: {
        driver: neo4j.driver(
          'bolt://neo4j_db:7687',
          neo4j.auth.basic('neo4j', 'root')
        )
      }

    };
  }
}

function buildSchema(): GraphQLSchema {
  return makeAugmentedSchema({
    typeDefs,
    config: {
      query: true,
      mutation: true
    }
  });
}

您可以使用 main.ts 中的main.ts app.useGlobalInterceptors(MyCustomInterceptor)方法全局绑定拦截器,也可以在任何模块中将拦截器添加到 providers 数组中

@Module({
  imports: [/* your imports*/],
  providers: [
    {
      provide: APP_INTERCEPTOR,
      useClass: MyCustomInterceptor
    },
    /* the rest of your providers*/
  ],
})
export class GraphqlModule {}

APP_INTERCEPTOR是从@nestjs/core导入的。 请记住,这确实会全局绑定拦截器。 所有对您服务器的请求都将通过此拦截器 go 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM