agmoss / nestjs-graphql-azure-functions

Serverless GraphQL API with Nest.js and Azure Functions
MIT License
19 stars 2 forks source link

Apollo Federation is not working with this approach #2

Open mdv27 opened 1 year ago

mdv27 commented 1 year ago

Thanks for creating this repository. I am able to create subgraph with azure function using the similar styles. I am unable to create the gateway with azure function which imports the subgraph. Gateway works fine as nestjs application server but does not when exposed as azure function. I am getting 404 error on http://localhost:7071/api/graphql

Gateway function.json

{
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "route": "graphql"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ],
  "scriptFile": "../dist/main/index.js"
}

Gateway index.ts

import { Context, HttpRequest } from "@azure/functions";
import { AzureHttpAdapter } from "@nestjs/azure-func-http";
import { createApp } from "../src/main";

export default function (context: Context, req: HttpRequest) {
  context.res = {
    headers: {
      "Content-Type": "application/json"
    }
  };

    AzureHttpAdapter.handle(createApp, context, req);    
}

main.js imported in index.ts

import { INestApplication, ValidationPipe } from "@nestjs/common";
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";

export async function createApp(): Promise<INestApplication> {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new ValidationPipe());
  app.enableCors();  
  app.setGlobalPrefix("api");
  await app.init();  
  return app;
}

app.module.ts

import { IntrospectAndCompose } from '@apollo/gateway';
import { ApolloGatewayDriver, ApolloGatewayDriverConfig } from '@nestjs/apollo';
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [
    GraphQLModule.forRoot<ApolloGatewayDriverConfig>({
      driver: ApolloGatewayDriver,
      gateway: {
        supergraphSdl: new IntrospectAndCompose({
          subgraphs: [
            { name: 'servicenow-subgraph-api', url: 'http://localhost:4000/api/graphql' }
          ]
        })
      }
    })
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
agmoss commented 1 year ago

Hi @mdv27

Thanks for flagging this. I have yet to try using this template with the apollo federation approach. Furthermore, this template is 5 major semvers behind the current @nestjs/graphql. Thus, the approach will run into some problems.

I am game to investigate and ideally upgrade this repo to reflect current changes in the nestjs and gql ecosystem. Do you have a minimum reproducible example you can share with me? Thx.