Refactor code structure and dependencies

This commit is contained in:
2024-09-29 21:46:03 +07:00
parent 34d9ee63e6
commit 88176bddc1
12 changed files with 500 additions and 45 deletions

View File

@@ -0,0 +1,26 @@
import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
@Injectable()
export class GraphQLValidationMiddleware implements NestMiddleware {
use(req: Request, res: Response, next: NextFunction) {
// Only handle POST requests
if (req.method === 'POST' && req.headers['content-type'] === 'application/json') {
const { query, mutation, subscription } = req.body;
// If none of these are present, return a custom error response
if (!query && !mutation && !subscription) {
return res.status(400).json({
errors: [
{
message: 'Must provide a valid GraphQL query, mutation, or subscription.',
},
],
});
}
}
// Continue to the next middleware or GraphQL handler
next();
}
}