remove graph
This commit is contained in:
@@ -1,25 +1,9 @@
|
||||
import { ArgumentsHost, Catch, ExceptionFilter, HttpException, HttpStatus, ExecutionContext } from '@nestjs/common';
|
||||
import { GqlExecutionContext } from '@nestjs/graphql';
|
||||
import { ArgumentsHost, Catch, ExceptionFilter, HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { FastifyReply } from 'fastify';
|
||||
|
||||
@Catch(HttpException)
|
||||
export class HttpExceptionFilter implements ExceptionFilter {
|
||||
catch(exception: HttpException, host: ArgumentsHost) {
|
||||
// Check if this is a GraphQL context
|
||||
if (host.getType<'graphql' | 'http'>() === 'graphql') {
|
||||
try {
|
||||
// In GraphQL context, host is actually an ExecutionContext
|
||||
const gqlContext = GqlExecutionContext.create(host as ExecutionContext);
|
||||
if (gqlContext && gqlContext.getInfo()) {
|
||||
// For GraphQL, let the exception propagate - GraphQL will handle it automatically
|
||||
// We can't use HTTP response methods in GraphQL context
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
// Not a GraphQL context, continue with HTTP handling
|
||||
}
|
||||
}
|
||||
|
||||
// Handle HTTP/REST context
|
||||
try {
|
||||
const ctx = host.switchToHttp();
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
|
||||
import { GqlExecutionContext } from '@nestjs/graphql';
|
||||
import { FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { FastifyReply } from 'fastify';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
@@ -8,59 +7,13 @@ import { map } from 'rxjs/operators';
|
||||
export class ResponseInterceptor implements NestInterceptor {
|
||||
constructor() {}
|
||||
|
||||
private isGraphQLBody(body: unknown): boolean {
|
||||
if (!body || typeof body !== 'object') {
|
||||
return false;
|
||||
}
|
||||
const bodyObj = body as Record<string, unknown>;
|
||||
return !!(bodyObj.query || bodyObj.mutation || bodyObj.operationName);
|
||||
}
|
||||
|
||||
intercept(context: ExecutionContext, next: CallHandler<Record<string, unknown>>): Observable<unknown> {
|
||||
// Check if this is a GraphQL request
|
||||
// GraphQL requests should bypass this interceptor as they handle their own response format
|
||||
|
||||
// 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<FastifyRequest>();
|
||||
// Check URL path
|
||||
if (request.url?.includes('/graphql')) {
|
||||
return next.handle();
|
||||
}
|
||||
// Check if request body contains GraphQL query/mutation
|
||||
if (this.isGraphQLBody(request.body)) {
|
||||
return next.handle();
|
||||
}
|
||||
} catch {
|
||||
// Not an HTTP context, might be GraphQL - skip wrapping
|
||||
return next.handle();
|
||||
}
|
||||
|
||||
// Method 3: Check if handler is a GraphQL resolver by checking the class name
|
||||
const handler = context.getHandler();
|
||||
const className = context.getClass()?.name || '';
|
||||
const handlerName = handler?.name || '';
|
||||
if (className.includes('Resolver') || handlerName.includes('Resolver')) {
|
||||
return next.handle();
|
||||
}
|
||||
|
||||
// For REST endpoints, wrap the response
|
||||
try {
|
||||
const ctx = context.switchToHttp().getResponse<FastifyReply>();
|
||||
const statusCode = ctx.statusCode;
|
||||
|
||||
// If statusCode is undefined, it might be a GraphQL request - skip wrapping
|
||||
// If statusCode is undefined, skip wrapping
|
||||
if (statusCode === undefined) {
|
||||
return next.handle();
|
||||
}
|
||||
@@ -87,7 +40,7 @@ export class ResponseInterceptor implements NestInterceptor {
|
||||
}),
|
||||
);
|
||||
} catch {
|
||||
// If we can't get HTTP context either, return data as-is (likely GraphQL)
|
||||
// If we can't get HTTP context, return data as-is
|
||||
return next.handle();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user