graphqlapollo-clientapollo-serverapollo-gateway

How to use Apollo-Gateway with swagger-to-graphql tool?


I'm using Swagger Petstore via swagger-to-graphql npm module and able to run the GraphQL Playground for it.

graphQLSchema('./swagger.json', 'http://petstore.swagger.io/v2', {
  Authorization: 'Basic YWRkOmJhc2ljQXV0aA=='
}).then(schema => {
  const app = express();
  app.use('/', graphqlHTTP(() => {
    return {
      schema,
      graphiql: true
    };
  }));
  app.listen(4001, 'localhost', () => {
    console.info('http://localhost:4001/');
  });
}).catch(e => {
  console.log(e);
});

However, when I tried to feed the service to Apollo Gateway, it throws Error: Apollo Server requires either an existing schema or typeDefs

const gateway = new ApolloGateway({
  serviceList: [
    { name: 'pet', url: 'http://localhost:4001' }
  ],
});
const server = new ApolloServer({
  gateway,

  // Currently, subscriptions are enabled by default with Apollo Server, however,
  // subscriptions are not compatible with the gateway.  We hope to resolve this
  // limitation in future versions of Apollo Server.  Please reach out to us on
  // https://spectrum.chat/apollo/apollo-server if this is critical to your adoption!
  subscriptions: false,
});

server.listen().then(({ url }) => {
  console.log(`🚀 Server ready at ${url}`);
});

What am I missing?


Solution

  • From the docs:

    Converting an existing schema into a federated service is the first step in building a federated graph. To do this, we'll use the buildFederatedSchema() function from the @apollo/federation package.

    You cannot provide just any existing service to the gateway -- the service must meet the federation spec. The only way to currently do that is to use buildFederatedSchema to create the service's schema. At this time, buildFederatedSchema does not accept existing schemas so federation is not compatible with any other tools that generate a schema for you. Hopefully that feature will be added in the near future.