Apollo-server: Apollo Gateway engineConfig for managed federation

Created on 14 Apr 2020  ·  10Comments  ·  Source: apollographql/apollo-server

Hi. I have been using Apollo Gateway with managed federation for quite some time now and it used to work very well, but recently, I noticed an error in the gateway like this after upgrading dependencies:

Screenshot from 2020-04-14 15-35-37

Error checking for changes to service definitions: WhenserviceListis not set, an Apollo Engine configuration must be provided. See https://www.apollographql.com/docs/apollo-server/federation/managed-federation/ for more information. WhenserviceListis not set, an Apollo Engine configuration must be provided. See https://www.apollographql.com/docs/apollo-server/federation/managed-federation/ for more information.

Which did not come before. Since it is a managed federation, I don't have a serviceList and this is how I used to initialize my ApolloGateway:

import { ApolloGateway, RemoteGraphQLDataSource } from '@apollo/gateway';
const gateway = new ApolloGateway({
        buildService(service) {
            return new RemoteGraphQLDataSource({
                url: service.url,
                willSendRequest: ({ request, context }) => {
                    request.http.headers.set('authorization', context.token);
                }
            });
        },
        debug: false
    });

const server = new ApolloServer({
        gateway,
        subscriptions: false,
        introspection: true,
        playground: {
            endpoint: config.apollo.playgroundEndpoint
        },
        engine: {
            apiKey: process.env.ENGINE_API_KEY,
            schemaTag: process.env.SCHEMA_TAG
        },
        context: async ({ req }) => {
            // Some code here....
            // Return variables
            return {variables};
        }
    });

May I know what changed I have to make? Looks like there are some new parameters but I don't find any documentation related to that.

For eg. I am not sure what to give in federationVersion which is a property of ManagedConfig as per this commit: https://github.com/apollographql/apollo-server/commit/f6ba2dea9b4f7c8aa602cad66398685d8bdbc3a2#diff-7b5e66976a637efc823826a985fa8e65

Thanks.

👩‍🚀 federation

All 10 comments

Can you provide the versions you were running before and the versions you're running now? There are not any new parameters, and things should have remained the same for you between upgrades, aside from the breaking changes listed in the CHANGELOG.md.

@abernix Thanks for your quick reply. I used to run @apollo/gateway ^0.11.7 but now I am running ^0.14.0

It doesn't seem to crash, but digging through the code I find a line like this:

if (!this.engineConfig) {
      throw new Error(
        'When `serviceList` is not set, an Apollo Engine configuration must be provided. See https://www.apollographql.com/docs/apollo-server/federation/managed-federation/ for more information.',
      );
    }

May I know if I am missing some config? Thanks.

And the check seems to exist for remote and local config but I can't find one for managed config:

if (isLocalConfig(config)) {
      return { isNewSchema: false };
    }

    if (isRemoteConfig(config)) {
      const serviceList = config.serviceList.map(serviceDefinition => ({
        ...serviceDefinition,
        dataSource: this.createAndCacheDataSource(serviceDefinition),
      }));

      return getServiceDefinitionsFromRemoteEndpoint({
        serviceList,
        ...(config.introspectionHeaders
          ? { headers: config.introspectionHeaders }
          : {}),
        serviceSdlCache: this.serviceSdlCache,
      });
    }

I noticed when logging that this.engineConfig is coming out to be undefined for me but has value within updateComposition function and hence it is throwing the error. But, debugging why its undefined. Any ideas?

@abernix I got the issue and looks like a bug. Its because of it being asynchronous I guess.

When loadServiceDefinitions function is called for the first time this.engineConfig is not initialized leading to undefined, but later it gets the value in subsequent calls. So, for the first time, this condition turns out to be true if you log it but works subsequently.

if (!this.engineConfig) {
         console.log('this.engineConfig-----', this.engineConfig);
          throw new Error('When `serviceList` is not set, an Apollo Engine configuration must be provided. See https://www.apollographql.com/docs/apollo-server/federation/managed-federation/ for more information.');
    } else {
         console.log('I have value-----', this.engineConfig);
}

Screenshot:
Screenshot from 2020-04-14 16-51-35

Your issue sounds somewhat connected to https://github.com/apollographql/apollo-server/pull/3979 which I'm cutting an alpha release for right now. I'll let you know when it's ready.

I've just published @apollo/[email protected] with the fix provided by @trevor-scheer in #3979. Try installing it and seeing if that resolves the problem for you? (It's published on the next tag, but you can also just install it directly — e.g. npm install @apollo/[email protected] or yarn add @apollo/[email protected])

@abernix Perfect! Tried it out. This works. Thanks. You can close the issue if you want.

Thanks for reporting it! I'll promote this to latest in a couple hours.

This has been promoted to latest.

Was this page helpful?
0 / 5 - 0 ratings