如何在带有 nestjs 框架的 neo4j-graphql-js 端点中使用拦截器?
How can I use interceptor in neo4j-graphql-js endpoint with nestjs framework?
我正在使用 neo4j-graphql-js 库将 graphql 查询转换为密码,我需要实现一个拦截器来验证返回的内容是否属于请求它的用户。为此,我需要实现拦截器,但我遇到的问题是我没有解析器,因为它会生成 liberia.我怎样才能让它通过拦截器?如果不能使用拦截器,有没有办法在响应中实现一个中间件?
我正在使用 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
中使用 app.useGlobalInterceptors(MyCustomInterceptor)
方法或在 any 模块中全局绑定您的拦截器,您可以在您的提供程序中添加拦截器数组
@Module({
imports: [/* your imports*/],
providers: [
{
provide: APP_INTERCEPTOR,
useClass: MyCustomInterceptor
},
/* the rest of your providers*/
],
})
export class GraphqlModule {}
APP_INTERCEPTOR
是从 @nestjs/core
导入的。请记住,这确实会全局绑定拦截器。所有对您服务器的请求都将通过此拦截器。
我正在使用 neo4j-graphql-js 库将 graphql 查询转换为密码,我需要实现一个拦截器来验证返回的内容是否属于请求它的用户。为此,我需要实现拦截器,但我遇到的问题是我没有解析器,因为它会生成 liberia.我怎样才能让它通过拦截器?如果不能使用拦截器,有没有办法在响应中实现一个中间件?
我正在使用 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
中使用 app.useGlobalInterceptors(MyCustomInterceptor)
方法或在 any 模块中全局绑定您的拦截器,您可以在您的提供程序中添加拦截器数组
@Module({
imports: [/* your imports*/],
providers: [
{
provide: APP_INTERCEPTOR,
useClass: MyCustomInterceptor
},
/* the rest of your providers*/
],
})
export class GraphqlModule {}
APP_INTERCEPTOR
是从 @nestjs/core
导入的。请记住,这确实会全局绑定拦截器。所有对您服务器的请求都将通过此拦截器。