neo4jcyphergraphqlnestjsresolver

How to integrate Neo4j database, NestJS framework and GraphQL?


I'm trying to integrate my REST API (NestJS) with new Neo4j database with GraphQL queries. Anybody succeed? Thanks in advance

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*/);
    }
}

Solution

  • 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(),
        );
      }
    }
    

    To use it in your Resolver:

    @Resolver('Movie')
    @UseInterceptors(Neo4JGraphQLInterceptor)
    export class MovieResolver {}
    

    My GraphQLModule is configured like this:

    @Module({
      imports: [
        GraphQLModule.forRoot({
          typePaths: ['./**/*.gql'],
          transformSchema: augmentSchema,
          context: {
            driver: neo4j.driver(
              'bolt://neo:7687',
              neo4j.auth.basic('neo4j', 'password1234'),
            ),
          },
        }),
      ],
      controllers: [...],
      providers: [..., MovieResolver, Neo4JGraphQLInterceptor],
    })
    

    Note the usage of transformSchema: augmentSchema to enable auto-generated mutations and queries (GRANDStack: Schema Augmentation)

    Hope that helps a bit!