【问题标题】:How to use Apollo-Gateway with swagger-to-graphql tool?如何将 Apollo-Gateway 与 swagger-to-graphql 工具一起使用?
【发布时间】:2019-12-18 08:15:34
【问题描述】:

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

我错过了什么?

【问题讨论】:

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


    【解决方案1】:

    来自the docs

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

    您不能只向网关提供任何现有服务——该服务必须满足federation spec。当前唯一的方法是使用buildFederatedSchema 创建服务的模式。目前,buildFederatedSchemadoes not accept existing schemas 所以联合不兼容任何其他为您生成模式的工具。希望不久的将来会添加该功能。

    【讨论】:

      猜你喜欢
      • 2021-04-10
      • 2022-07-29
      • 2021-04-24
      • 2017-11-11
      • 2020-03-27
      • 2020-01-17
      • 2021-09-13
      • 2018-09-05
      • 2021-01-11
      相关资源
      最近更新 更多