nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.69k stars 7.63k forks source link

Graphql Request info in Execution Context #11643

Closed mmRoshani closed 1 year ago

mmRoshani commented 1 year ago

Is there an existing issue for this?

Current behavior

I was working with an interceptor that implements the NestInterceptor with the following code:

@Injectable()
export class SomeInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    switch (context.getType()) {
      case 'http': {
        // ...
        break;
      }
      case 'rpc': {
        // ...
        break;
      }
      case 'ws': {
        // ...
        break;
      }
      default:
        console.log(context.getType());
    }
  }
}

then after sending a Graphql request, it came into default, and the output of context.getType() was graphql although the type is:

export type ContextType = 'http' | 'ws' | 'rpc';

so you are not allowed to have graphql case. you also can browse the arguments-host.interface.ts file.

Minimum reproduction code

https://github.com/mmRoshani/nest-issue

Steps to reproduce

No response

Expected behavior

shouldn't the type be something like:

export type ContextType = 'http' | 'ws' | 'rpc' | 'graphql';

then we also add the GraphqlArgumentsHost interface in arguments-host.interface.ts:

/**
 * Methods to obtain Graphql data object.
 *
 * @publicApi
 */
export interface GraphqlArgumentsHost {
  /**
   * Returns the in-flight `request` object.
   */
  getRequest<T = any>(): T;
  /**
   * Returns the in-flight `response` object.
   */
  getResponse<T = any>(): T;
  getNext<T = any>(): T;
}

and then add switchToGraphql to the ArgumentsHost interface like so:

export interface ArgumentsHost {
  //...
  switchToGraphql(): GraphqlArgumentsHost;

then go ahead and implement the switchToGraphql in the execution-context-host.ts file like so:

export class ExecutionContextHost implements ExecutionContext {

 //...
// I am not sure about this implementation.
  switchToGraphql(): GraphqlArgumentsHost {
    return Object.assign(this, {
      getRequest: () => this.getArgByIndex(0),
      getResponse: () => this.getArgByIndex(1),
      getNext: () => this.getArgByIndex(2),
    });
  }
}

Package

Other package

No response

NestJS version

^9.4.1

Packages versions

{
  "scripts": {
    "build": "nest build",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch"
  },
  "dependencies": {
    "@apollo/server": "^4.7.1",
    "@aws-sdk/client-s3": "^3.332.0",
    "@grpc/grpc-js": "^1.8.14",
    "@grpc/proto-loader": "^0.7.7",
    "@nestjs/apollo": "^11.0.5",
    "@nestjs/common": "^9.4.1",
    "@nestjs/config": "^2.3.2",
    "@nestjs/core": "^9.4.1",
    "@nestjs/graphql": "^11.0.5",
    "@nestjs/jwt": "^10.0.3",
    "@nestjs/microservices": "^9.4.1",
    "@nestjs/mongoose": "^9.2.2",
    "@nestjs/platform-express": "^9.4.1",
    "@nestjs/swagger": "^6.3.0",
    "@nestjs/terminus": "^9.2.2",
    "@nestjs/websockets": "^9.4.1",
    "@ntegral/nestjs-sentry": "^4.0.0",
    "@opentelemetry/api": "^1.4.1",
    "@opentelemetry/auto-instrumentations-node": "^0.37.0",
    "@opentelemetry/exporter-trace-otlp-http": "^0.39.1",
    "@opentelemetry/exporter-zipkin": "^1.13.0",
    "@opentelemetry/instrumentation": "^0.39.1",
    "@opentelemetry/instrumentation-express": "^0.32.3",
    "@opentelemetry/instrumentation-grpc": "^0.39.1",
    "@opentelemetry/instrumentation-http": "^0.39.1",
    "@opentelemetry/instrumentation-nestjs-core": "^0.32.4",
    "@opentelemetry/resources": "^1.13.0",
    "@opentelemetry/sdk-node": "^0.39.1",
    "@opentelemetry/sdk-trace-node": "^1.13.0",
    "@opentelemetry/semantic-conventions": "^1.13.0",
    "@sentry/node": "^7.52.1",
    "@typegoose/typegoose": "^11.1.0",
    "@willsoto/nestjs-prometheus": "^5.1.1",
    "abacl": "^4.1.2",
    "bcryptjs": "^2.4.3",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.0",
    "cron-validator": "^1.3.1",
    "crypto-js": "^4.1.1",
    "graphql": "^16.6.0",
    "graphql-query-complexity": "^0.12.0",
    "helmet": "^7.0.0",
    "ioredis": "^5.3.2",
    "kafkajs": "^2.2.4",
    "lodash": "^4.17.21",
    "log": "^6.3.1",
    "log-node": "^8.0.3",
    "mongoose": "^7.1.1",
    "multer-s3": "^3.0.1",
    "naming-conventions-modeler": "^1.4.0",
    "nest-commander": "^3.7.1",
    "nestjs-minio-client": "^2.0.0",
    "opentelemetry-instrumentation-kafkajs": "^0.35.0",
    "phone": "^3.1.37",
    "prom-client": "^14.2.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.8.1"
  },
  "devDependencies": {
    "@compodoc/compodoc": "^1.1.19",
    "@nestjs/cli": "^9.5.0",
    "@nestjs/schematics": "^9.2.0",
    "@nestjs/testing": "^9.4.1",
    "@types/bcryptjs": "^2.4.2",
    "@types/crypto-js": "^4.1.1",
    "@types/express": "^4.17.17",
    "@types/jest": "29.5.1",
    "@types/lodash": "^4.14.194",
    "@types/log": "^6.3.0",
    "@types/multer-s3": "^3.0.0",
    "@types/node": "20.2.0",
    "@types/supertest": "^2.0.12",
    "@typescript-eslint/eslint-plugin": "^5.59.6",
    "@typescript-eslint/parser": "^5.59.6",
    "eslint": "^8.40.0",
    "eslint-config-prettier": "^8.8.0",
    "eslint-plugin-prettier": "^4.2.1",
    "jest": "29.5.0",
    "prettier": "^2.8.8",
    "source-map-support": "^0.5.21",
    "supertest": "^6.3.3",
    "ts-jest": "29.1.0",
    "ts-loader": "^9.4.2",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "4.2.0",
    "typescript": "^5.0.4"
  },
  "engines": {
    "node": "^18.15.0",
    "npm": "^9.5.0"
  }
}

Node.js version

^18.15.0

In which operating systems have you tested?

Other

No response

kamilmysliwiec commented 1 year ago

Please search through some of our old issues on this (this has been discussed several times in the past). You can also use our Discord channel (support).