如何通过swagger-to-graphql工具使用Apollo-Gateway?

时间:2019-08-11 11:17:13

标签: graphql apollo-client apollo-server apollo-gateway

我正在通过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}`);
});

我想念什么?

1 个答案:

答案 0 :(得分:1)

来自the docs

  

将现有模式转换为联合服务是构建联合图的第一步。为此,我们将使用@ apollo / federation包中的buildFederatedSchema()函数。

您不能仅向网关提供任何现有服务-服务必须符合federation spec。当前执行此操作的唯一方法是使用buildFederatedSchema创建服务的架构。目前,buildFederatedSchema does not accept existing schemas因此,联盟与为您生成架构的任何其他工具都不兼容。希望该功能会在不久的将来添加。