如何将 Apollo-Gateway 与 swagger-to-graphql 工具一起使用?

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

我正在通过 swagger-to-graphql npm 模块使用 Swagger Petstore,并且能够 运行 GraphQL Playground。

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);
});

但是,当我尝试将服务提供给 Apollo Gateway 时,它抛出 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}`);
});

我错过了什么?

来自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.

您不能只向网关提供任何现有服务 -- 该服务必须满足 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 因此联合与为您生成模式的任何其他工具不兼容。希望不久的将来会添加该功能。