Sheraz Ali
Sheraz Ali

Reputation: 315

Apollo gateway not picking up managed federation configurations

This could be quite new to everyone but hoping for the best to find out the solution.

I've been trying to setup apollo managed federation through ApolloGateway for federated my services by following the official documentation. https://www.apollographql.com/docs/graph-manager/managed-federation/setup/#4-deploy-the-modified-gateway

.env

NODE_ENV=development
APOLLO_KEY=service:service_name:hash

ApolloGateway

import 'reflect-metadata';
import express from 'express';
import {ApolloServer} from 'apollo-server-express';
import {ApolloGateway} from '@apollo/gateway';
import {config} from 'dotenv';
config();

const port = process.env.NODE_PORT || 7000;
const nodeEnv = process.env.NODE_ENV || 'localhost';
const nodeHost = process.env.NODE_HOST || 'http://localhost';
const apolloGatewayConfig: any = {
  __exposeQueryPlanExperimental: false,
};
if (nodeEnv === 'localhost' || true) {
  apolloGatewayConfig.serviceList = [
    {
      name: 'vendors',
      url: `${process.env.GMS_VENDORS_NODE_HOST}/graphql`,
    }
  ];
}
const gateway = new ApolloGateway(apolloGatewayConfig);

(async () => {
  const app = express();
  app.get('/health', (_, res: any): void => {
    res.send({gateway: true});
  });

  const {schema, executor} = await gateway.load(); // breaking point
  const server = new ApolloServer({
    schema,
    executor,
    engine: true,
    subscriptions: false,
  });
  server.applyMiddleware({app, path: '/graphql'});

  app.listen({port}, () =>
    console.log(`API Gateway is ready at ${nodeHost}:${port}`)
  );
})();

at line const {schema, executor} = await gateway.load(); it throws an error

UnhandledPromiseRejectionWarning: Error: WhenserviceListis not set, an Apollo Engine configuration must be provided.

I've been following official docs but not sure what am I missing here?

Upvotes: 2

Views: 3791

Answers (1)

Quill
Quill

Reputation: 2755

Not sure if this solves your problem, but I had a similar issue where my introspection queries would go through local gateway instead of Apollo Studio;

First, I had to deploy the individual services in a federation configuration through the CLI (as reporting schema directly is not available yet)

npx apollo service:push --graph=<graph> --key=<my-key> --localSchemaFile=src/schema.graphql --serviceName=<serviceName> --serviceURL=<serviceUrl> --variant=dev

Then, inside the gateway code I had to remove the serviceList from the ApolloGateway constructor as instructed from the Apollo Studio setup docs:

This option specifies the name and URL for each of your graph's implementing services. With managed federation, this information is no longer hardcoded in the gateway's constructor! Instead, the gateway regularly polls Apollo for this information. This enables you to add and remove implementing services from your graph without needing to restart your gateway.

Remove the serviceList argument from your ApolloGateway constructor entirely:

const gateway = new ApolloGateway({
  serviceList: [
    { name: 'test', url: 'http://localhost:4000/graphql'},
  ]
});
...
const gateway = new ApolloGateway();

In your case, this should solve your issue but you should also update your use of Apollo Server as well. Instead of using { schema, executor }, you can embed the gateway directly:

const server = new ApolloServer({
  gateway,
  subscriptions: false,
});

Upvotes: 2

Related Questions