将 GraphQL Schema 公开为 REST api 的方法有哪些?

What are the ways to expose GraphQL Schema as REST api?

我开发了 Graphql api,并希望从中公开一些 REST API 端点。所以这里我的意图是使用相同的 Graphql 逻辑将其公开为 rest api。到目前为止,我收到了有关使用 sofa 来实现此目的的帖子,并想检查是否有任何我们可以使用的 NestJS 或 Graphql 现有包。

您已经开发了一个 GraphQL API,然后您想要将其公开为 REST,或者您想在您的 GraphQL API 中集成一个 REST API?如果是第一个,那你就完全错过了 GraphQL 的重点。如果是第二个,那么您需要在 Apollo Server 构造函数中传递上下文参数,其中包含一个 dataSource 参数。然后你必须定义一个扩展 RESTDataSource (https://www.apollographql.com/docs/apollo-server/data/data-sources/).

的 class

这取决于您到目前为止构建应用程序的方式。通常,NestJS 提倡 N 层架构,其中业务逻辑属于 Services/Providers。当以这种方式构建时,在 GraphQL 解析器和 HTTP 控制器之间共享逻辑变得相对简单。

解析器和控制器应该纯粹充当接收 requests/data 并将它们传递给适当的服务进行处理的路由组件。

@Resolver()
class MyResolver {
   constructor(private readonly service: MyService) { }

   @Query()
   doSomething() {
     return this.service.doSomething();
   }
}

@Controller()
class MyController {
   constructor(private readonly service: MyService) { }

   @Get()
   doSomething() {
     return this.service.doSomething();
   }
}