我正在尝试将我的REST API(NestJS)与新的Neo4j数据库与GraphQL查询集成。有人成功吗?提前致谢
编辑1 :(我添加了我的代码)
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*/);
}
}
我没有参与GraphQL,但我知道有一个npm包(Neo4j-graphql-js)将GraphQL查询转换为Cypher查询。它使GraphQL和Neo4j更容易一起使用。
另外,检查GRANDstack它是一个用于构建基于Graph的应用程序的全栈开发集成。
如果你在这里提出这些类型的问题,你只会投票,我建议你访问Neo4j Community。
我正在使用NestInterceptor
来实现这一目标:
@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
来启用自动生成的突变和查询(GRANDStack: Schema Augmentation)
希望那有所帮助!