How to apply input validation `class-validator` from nestjs to neo4j query and mutations

46 Views Asked by At

I have just started a new project with Graphql, Nestjs, Neo4j(With OGM) and (I am now to all of them.) I was wondering if there's a way to implement class-validation for Query and Resolver validations?


type Person {
    name: String!
    born: Int!
    actedInMovies: [Movie!]! @relationship(type: "ACTED_IN", properties: "ActedIn", direction: OUT)
    directedMovies: [Movie!]! @relationship(type: "DIRECTED", direction: OUT)
}

type Movie {
    title: String!
    released: Int!
    actors: [Person!]! @relationship(type: "ACTED_IN", properties: "ActedIn", direction: IN)
    director: Person! @relationship(type: "DIRECTED", direction: IN)
}

interface ActedIn @relationshipProperties {
    roles: [String!]
}

considering the above schema we will have queries (with aggregation) and mutations generated (for Person and Movies) right out of the box if we follow their official documentation. I was looking for a way to apply input validation to either the scehma or the query and mutations somehow.

I scrolled through the official docs and didn't find anything helpful.

Any suggestions would be really helpful, thank you in advance.

I have tried to add an middleware to my module class like below but I think this solution is could be improved (unable to think how).

Graphql Module

export class GqlModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(LoggerMiddleware).forRoutes('/graphql');
  }
}

Logging Middleware

import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';

@Injectable()
export class LoggerMiddleware implements NestMiddleware {
  use(req: Request, res: Response, next: NextFunction) {
    console.log('Request...', req.body);
    next();
  }
}

0

There are 0 best solutions below