From a8f2f4b50c1655547215874a92e3d34082cf6dde Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Fri, 21 Nov 2025 16:31:04 +0330 Subject: [PATCH] bug --- src/core/interceptors/response.interceptor.ts | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/core/interceptors/response.interceptor.ts b/src/core/interceptors/response.interceptor.ts index bc1167c..632186c 100755 --- a/src/core/interceptors/response.interceptor.ts +++ b/src/core/interceptors/response.interceptor.ts @@ -3,32 +3,43 @@ import { GqlExecutionContext } from '@nestjs/graphql'; import { FastifyReply, FastifyRequest } from 'fastify'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; -import { Reflector } from '@nestjs/core'; @Injectable() export class ResponseInterceptor implements NestInterceptor { - constructor(private reflector: Reflector) {} + constructor() {} + + private isGraphQLBody(body: unknown): boolean { + if (!body || typeof body !== 'object') { + return false; + } + const bodyObj = body as Record; + return !!(bodyObj.query || bodyObj.mutation || bodyObj.operationName); + } intercept(context: ExecutionContext, next: CallHandler>): Observable { // Check if this is a GraphQL request // GraphQL requests should bypass this interceptor as they handle their own response format - // Method 1: Check context type (most reliable for GraphQL) - const contextType = context.getType(); - if (contextType === 'graphql') { - return next.handle(); + // Method 1: Try to create GraphQL context - if it doesn't throw, it's GraphQL + try { + const gqlContext = GqlExecutionContext.create(context); + // If we can create it and it has GraphQL-specific properties, it's GraphQL + if (gqlContext && gqlContext.getInfo()) { + return next.handle(); + } + } catch { + // Not a GraphQL context, continue } // Method 2: Check request URL and body for GraphQL endpoint try { const request = context.switchToHttp().getRequest(); // Check URL path - if (request.url?.includes('/graphql') || request.routerPath?.includes('/graphql')) { + if (request.url?.includes('/graphql')) { return next.handle(); } // Check if request body contains GraphQL query/mutation - const body = request.body as any; - if (body && typeof body === 'object' && (body.query || body.mutation || body.operationName)) { + if (this.isGraphQLBody(request.body)) { return next.handle(); } } catch { @@ -44,17 +55,6 @@ export class ResponseInterceptor implements NestInterceptor { return next.handle(); } - // Method 4: Try to create GraphQL context - if it doesn't throw, it's GraphQL - try { - const gqlContext = GqlExecutionContext.create(context); - // If we can create it and it has GraphQL-specific properties, it's GraphQL - if (gqlContext && gqlContext.getInfo) { - return next.handle(); - } - } catch { - // Not a GraphQL context, continue - } - // For REST endpoints, wrap the response try { const ctx = context.switchToHttp().getResponse();