我正在尝试将我的REST API(NestJS)与带有GraphQL查询的新Neo4j数据库集成在一起。有人成功吗?预先感谢
编辑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*/);
}
}
答案 0 :(得分:1)
这是对我有用的...虽然不像我想要的那么优雅,但它可以工作;我想只有一个服务/提供者访问我的数据库,而不是从每个模块访问该服务,即使这也可以工作。所以我坚持使用myModule-> myResolver-> myService-> Neo4jService的Nest格式。因此Neo4jService被注入所有xService中。必要时,我正在使用neo4jGraphql,expandSchema和Cypher。
代码:
**appmodule.ts**
....
import { makeExecutableSchema } from 'graphql-tools';
import { v1 as neo4j } from 'neo4j-driver';
import { augmentTypeDefs, augmentSchema } from 'neo4j-graphql-js';
import { Neo4jService } from './neo4j/neo4j.service';
import { MyModule } from './my/my.module';
import { MyResolver } from './my/my.resolver';
import { MyService } from './my/my.service';
....
import { typeDefs } from './generate-schema'; // SDL type file
...
const driver = neo4j.driver('bolt://localhost:3000', neo4j.auth.basic('neo4j', 'neo4j'))
const schema = makeExecutableSchema({
typeDefs: augmentTypeDefs(typeDefs),
});
const augmentedSchema = augmentSchema(schema); // Now we have an augmented schema
@Module({
imports: [
MyModule,
GraphQLModule.forRoot({
schema: augmentedSchema,
context: {
driver,
},
}),
],
controllers: [],
providers: [ Neo4jService,
myResolver,
],
})
export class AppModule {}
**myResolver.ts**
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { MyService } from './my.service';
@Resolver('My')
export class MyResolver {
constructor(
private readonly myService: MyService) {}
@Query()
async getData(object, params, ctx, resolveInfo) {
return await this.myService.getData(object, params, ctx, resolveInfo);
}
*//Notice I am just passing the graphql params, etc to the myService*
}
**myService.ts**
import { Injectable } from '@nestjs/common';
import { Neo4jService } from '../neo4j/neo4j.service';
@Injectable()
export class MyService {
constructor(private neo4jService: Neo4jService) {}
async getData(object, params, ctx, resolveInfo) {
return await this.neo4jService.getData(object, params, ctx, resolveInfo);
}
*// Again I am just passing the graphql params, etc to the neo4jService*
}
**neo4jService.ts**
import { Injectable } from '@nestjs/common';
import { neo4jgraphql } from 'neo4j-graphql-js';
@Injectable()
export class Neo4jService {
getData(object, params, ctx, resolveInfo) {
return neo4jgraphql(object, params, ctx, resolveInfo);
}
.....
......
}
所以基本上我推迟了使用neo4jgraphql,直到我们到达neo4jService为止。现在我所有的数据库调用都在这里.....就像我说的不太优雅,但是可以。
挑战:Graphql generate将不接受@relation ...我发现已进行了更改,现在您需要extraTypeDefs。 ...希望这可以帮助 编辑 Nestjs需要花费很长时间来处理expandSchema ...所以我建议暂时跳过它。.
答案 1 :(得分:1)
这是我为(NestJS + GraphQL + Neo4j)创建的示例。我希望这会有所帮助!
答案 2 :(得分:0)
我还没有使用GraphQL,但是我知道有一个npm包(Neo4j-graphql-js)可以将GraphQL查询转换为Cypher查询。一起使用GraphQL和Neo4j更加容易。
还要检查GRANDstack,这是用于构建基于Graph的应用程序的全栈开发集成。
如果您在此处提出这些类型的问题,则只会投票,我建议您访问Neo4j Community。
答案 3 :(得分:0)
我正在使用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)
希望能有所帮助!