如何集成Neo4j数据库,NestJS框架和GraphQL? [英] How to integrate Neo4j database, NestJS framework and GraphQL?
本文介绍了如何集成Neo4j数据库,NestJS框架和GraphQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将我的REST API(NestJS)与带有GraphQL查询的新Neo4j数据库集成在一起.有人成功吗?预先感谢
I'm trying to integrate my REST API (NestJS) with new Neo4j database with GraphQL queries. Anybody succeed? Thanks in advance
编辑1 :(我添加了代码)
EDIT 1: (I added my code)
import { Resolver } from "@nestjs/graphql";
import { Query, forwardRef, Inject, Logger } from "@nestjs/common";
import { Neo4jService } from "src/shared/neo4j/neoj4.service";
import { GraphModelService } from "./models/model.service";
import { Movie } from "src/graphql.schema";
@Resolver('Movie')
export class GraphService {
constructor(private readonly _neo4jService: Neo4jService) {}
@Query()
async getMovie() {
console.log("hello");
return neo4jgraphql(/*i don't know how get the query and params*/);
}
}
推荐答案
我正在使用NestInterceptor
来完成此任务:
I am using a NestInterceptor
to accomplish this:
@Injectable()
export class Neo4JGraphQLInterceptor implements NestInterceptor {
intercept(
context: ExecutionContext,
next: CallHandler<any>,
): Observable<any> | Promise<Observable<any>> {
const ctx = GqlExecutionContext.create(context);
return neo4jgraphql(
ctx.getRoot(),
ctx.getArgs(),
ctx.getContext(),
ctx.getInfo(),
);
}
}
要在您的Resolver
中使用它:
@Resolver('Movie')
@UseInterceptors(Neo4JGraphQLInterceptor)
export class MovieResolver {}
我的GraphQLModule
的配置如下:
@Module({
imports: [
GraphQLModule.forRoot({
typePaths: ['./**/*.gql'],
transformSchema: augmentSchema,
context: {
driver: neo4j.driver(
'bolt://neo:7687',
neo4j.auth.basic('neo4j', 'password1234'),
),
},
}),
],
controllers: [...],
providers: [..., MovieResolver, Neo4JGraphQLInterceptor],
})
请注意使用transformSchema: augmentSchema
来启用自动生成的突变和查询(
Note the usage of transformSchema: augmentSchema
to enable auto-generated mutations and queries (GRANDStack: Schema Augmentation)
希望能有所帮助!
这篇关于如何集成Neo4j数据库,NestJS框架和GraphQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文