【问题标题】:How can I use interceptor in neo4j-graphql-js endpoint with nestjs framework?如何在 neo4j-graphql-js 端点中使用带有 nestjs 框架的拦截器?
【发布时间】:2020-02-11 07:15:40
【问题描述】:

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

我正在使用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
    }
  });
}

【问题讨论】:

    标签: javascript neo4j graphql interceptor nestjs


    【解决方案1】:

    您可以使用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 导入的。请记住,这确实会全局绑定拦截器。对您服务器的所有请求都将通过此拦截器。

    【讨论】:

      猜你喜欢
      • 2021-04-10
      • 2019-11-17
      • 1970-01-01
      • 2019-05-01
      • 2021-08-17
      • 2020-12-16
      • 2020-01-03
      • 2020-12-12
      • 2021-08-08
      相关资源
      最近更新 更多