我试图理解GraphQL在微服务架构中最适合使用的地方。
对于只有一个GraphQL模式作为API网关代理请求到目标微服务并强制它们的响应,存在一些争论。微服务仍然使用REST / Thrift协议进行通信。
另一种方法是使用多个GraphQL模式,每个微服务一个。使用一个较小的API Gateway服务器,将请求路由到目标微服务,并将请求的所有信息+ GraphQL查询。
1号的方法
使用一个GraphQL模式作为API网关会有一个缺点,每次你改变你的微服务契约输入/输出时,我们必须在API网关端相应地改变GraphQL模式。
2方法
如果每个微服务都使用多个GraphQL模式,在某种程度上是有意义的,因为GraphQL强制了一个模式定义,消费者需要尊重微服务给出的输入/输出。
问题
你在哪里发现GraphQL适合设计微服务架构?
你将如何设计一个具有GraphQL实现的API网关?
对于问题1,Intuit在几年前宣布迁移到One Intuit API生态系统(https://www.slideshare.net/IntuitDeveloper/building-the-next-generation-of-quickbooks-app-integrations-quickbooks-connect-2017)时承认了GraphQL的强大功能。Intuit选择了方法1。您提到的缺点实际上阻止了开发人员引入可能破坏客户端应用程序的破坏性模式更改。
GraphQL在许多方面帮助提高了开发人员的工作效率。
When designing a new microservice for a domain, engineers (backend/ frontend/ stakeholders) agree on a schema for the domain entities. Once the schema is approved, it is merged with the master domain schema (universal) and deployed on the Gateway.Front end engineers can start coding client applications with this schema while backend engineers implement the functionality. Having one universal schema means that there are no two microservices with redundant functionality.
GraphQL has helped client applications become simpler and faster. Want to retrieve data from /update data to multiple microservices? All client applications have to do is fire ONE GraphQL request and the API Gateway abstraction layer will take care to fetch and collate data from multiple sources (microservices). Open source frameworks like Apollo (https://www.apollographql.com/) have accelerated the pace of GraphQL adoption.
With mobile being the first choice for modern applications, it is important to design for lower data bandwidth requirements from ground zero. GraphQL helps by allowing client apps to request for specific fields only.
问题2:我们在API Gateway上构建了一个自定义抽象层,它知道模式的哪一部分属于哪个服务(提供者)。当查询请求到达时,抽象层将请求转发给适当的服务。一旦基础服务返回响应,抽象层就负责返回所请求的字段。
然而,现在有几个平台(Apollo服务器、GraphQL -yoga等)允许人们在短时间内构建一个GraphQL抽象层。
The way it is being described in this question, I believe that using a custom API gateway as an orchestration service can make a lot of sense for complex enterprise focused applications. GraphQL can be a good technology choice for that orchestration service, at least as far as querying goes. The advantage to your first approach (one schema for all microservices) is the ability to stitch together the data from multiple microservices in a single request. That may, or may not, be very important depending on your situation. If the GUI calls for rendering data from multiple microservices all at once, then this approach can simplify the client code such that a single call can return data that is suitable for data binding with the GUI elements of such frameworks as Angular or React. This advantage doesn't apply for mutations.
The disadvantage is tight coupling between the data APIs and the orchestration service. Releases can no longer be atomic. If you refrain from introducing backwards breaking changes in your data APIs, then this can introduce complexity only when rolling back a release. For example, if you are about to release new versions of two data APIs with the corresponding changes in the orchestration service and you need to roll one of those releases back but not the other, then you will be forced to roll back all three anyway.
在GraphQL和REST的比较中,你会发现GraphQL没有RESTful api那么高效,所以我不建议在数据api上用GraphQL代替REST。
对于问题1,Intuit在几年前宣布迁移到One Intuit API生态系统(https://www.slideshare.net/IntuitDeveloper/building-the-next-generation-of-quickbooks-app-integrations-quickbooks-connect-2017)时承认了GraphQL的强大功能。Intuit选择了方法1。您提到的缺点实际上阻止了开发人员引入可能破坏客户端应用程序的破坏性模式更改。
GraphQL在许多方面帮助提高了开发人员的工作效率。
When designing a new microservice for a domain, engineers (backend/ frontend/ stakeholders) agree on a schema for the domain entities. Once the schema is approved, it is merged with the master domain schema (universal) and deployed on the Gateway.Front end engineers can start coding client applications with this schema while backend engineers implement the functionality. Having one universal schema means that there are no two microservices with redundant functionality.
GraphQL has helped client applications become simpler and faster. Want to retrieve data from /update data to multiple microservices? All client applications have to do is fire ONE GraphQL request and the API Gateway abstraction layer will take care to fetch and collate data from multiple sources (microservices). Open source frameworks like Apollo (https://www.apollographql.com/) have accelerated the pace of GraphQL adoption.
With mobile being the first choice for modern applications, it is important to design for lower data bandwidth requirements from ground zero. GraphQL helps by allowing client apps to request for specific fields only.
问题2:我们在API Gateway上构建了一个自定义抽象层,它知道模式的哪一部分属于哪个服务(提供者)。当查询请求到达时,抽象层将请求转发给适当的服务。一旦基础服务返回响应,抽象层就负责返回所请求的字段。
然而,现在有几个平台(Apollo服务器、GraphQL -yoga等)允许人们在短时间内构建一个GraphQL抽象层。