nestjs / graphql

GraphQL (TypeScript) module for Nest framework (node.js) 🍷
https://docs.nestjs.com/graphql/quick-start
MIT License
1.46k stars 397 forks source link

Inability to Use `transformSchema` and Custom Directives in Apollo Gateway Configuration with `@nestjs/graphql` #3296

Open ahmad-punch opened 3 months ago

ahmad-punch commented 3 months ago

Is there an existing issue for this?

Current behavior

The issue manifests when attempting to use the transformSchema and publicDirectiveTransformer in the Apollo Gateway configuration within a NestJS application using the code-first approach. Specifically, the transformSchema function never fires, and no console logs are generated, even when placed directly in the transformSchema function.

Additionally, the issue allows for any arbitrary property (like abc: true) to be added to the Gateway configuration without throwing any errors, indicating that the configuration is not being properly validated or processed by the @nestjs/graphql package.

Minimum reproduction code

https://github.com/ahmad-punch/nestjs-gateway-federation2-transformSchema-issue-reproduction-code.git

Steps to reproduce

Minimum Reproduction Code

You can reproduce this issue with the following code setup:

Approach:

GitHub Repository: A minimal reproduction repository is available here: https://github.com/ahmad-punch/nestjs-gateway-federation2-transformSchema-issue-reproduction-code.git

Steps to Reproduce

Generic Steps:

  1. Create a NestJS project with the @nestjs/graphql and @apollo/gateway packages.
  2. Set up the GraphQLModule with the Apollo Gateway Driver as shown in the provided app.module.ts.
  3. Attempt to use transformSchema or any other schema transformation functions.
  4. Observe that transformSchema is never called, and no logs from this function are visible.
  5. Also, add any arbitrary key (like abc: true) to the gateway configuration and observe that it does not throw an error or get processed.

Steps Using GitHub Repository:

  1. Clone the repository:

    
    git clone https://github.com/ahmad-punch/nestjs-gateway-federation2-transformSchema-issue-reproduction-code.git`
  2. Navigate to the platform-service folder, install dependencies, and start the service:

    
        cd /platform-service 
        npm install
        npm run dev  `
  3. Once the platform service is started, navigate to the gateway folder, install dependencies, and start the gateway service:

    
    cd ../gateway
    npm install
    npm run dev 
    `
  4. Open the GraphQL Playground for the gateway, and execute the following query:

    
       query {
       findAllTest {
       exampleField
           }
         } 
       `
  5. Observe the logs of the gateway server. You will notice that the gateway does not detect the directive, nor does it run any logs mentioned in the transformSchema function in the app.module.ts of the gateway. However, it should.

Expected behavior

Package version

12.2.0

Graphql version

    "dependencies": {
                                 "@apollo/gateway": "^2.8.1",
                                 "@apollo/server": "^4.11.0",
                                 "@apollo/subgraph": "^2.8.1",
                                 "@nestjs/apollo": "^12.2.0",
                                 "@nestjs/common": "^10.0.0",
                                 "@nestjs/config": "^3.2.3",
                                 "@nestjs/core": "^10.0.0",
                                 "@nestjs/graphql": "^12.2.0",
                                 "@nestjs/microservices": "^10.3.10",
                                 "@nestjs/mongoose": "^10.0.10",
                                 "@nestjs/platform-express": "^10.0.0",
                                  "graphql": "^16.9.0",
                                  "jsonwebtoken": "^9.0.2",
                                  "mongoose": "^8.5.1",
                                  "reflect-metadata": "^0.2.0",
                                  "rxjs": "^7.8.1"
                           }

NestJS version

10.0.0

Node.js version

18.17.0

In which operating systems have you tested?

Other

Why I'm Doing This

The main goal is to add a directive in the subgraph (e.g., @public) that will instruct the supergraph/gateway to leave certain queries public, effectively bypassing authentication for those queries. The gateway should respect this directive, allowing the subgraph to control which queries require authentication and which do not.

However, due to the issues outlined above, the intended behavior cannot be achieved. The transformSchema function is not being called, custom directives are not processed correctly, and the configuration is not being validated properly. This blocks the implementation of a flexible, directive-based authentication system in a federated GraphQL architecture.

ahmad-punch commented 3 months ago

I was reading and I am not sure but this seems relevant:

https://github.com/nestjs/graphql/pull/2059#issue-1159936226

jpiv commented 1 week ago

+1 on this directive support is needed if you want to use a real router instead of Apollo Gateway