我们看到越来越多的工具(例如 Apollo Federation)支持将多个 GraphQL 端点聚合到一个图中。但必须提醒的是,不要滥用 GraphQL,尤其是作为服务间的协议时。我们的实践是仅将 GraphQL 用于服务端资源聚合。使用这种模式时,微服务会持续发布明确定义的 RESTful API,而聚合服务或 BFF(Backend for Frontends) 模式则使用 GraphQL 解析器集成其他服务资源。图的形状需由领域建模实践驱动,以确保在必要时(在每个限界上下文都是一个微服务的情况下)将统一语言局限在子图内。该技术简化了聚合服务或 BFF 的内部实现,同时鼓励对服务进行良好的建模以避免贫血 REST。
One pattern that comes up again and again when building microservice-style architectures is how to handle the aggregation of many resources server-side. In recent years, we've seen the emergence of a number of patterns such as Backend for Frontend (BFF) and tools such as Falcor to address this. Our teams have started using GraphQL for server-side resource aggregation instead. This differs from the usual mode of using GraphQL where clients directly query a GraphQL server. When using this technique, the services continue to expose RESTful APIs but under-the-hood aggregate services use GraphQL resolvers as the implementation for stitching resources from other services. This technique simplifies the internal implementation of aggregate services or BFFs by using GraphQL.