When and How to use GraphQL with microservice architecture

Solution 1:

Definitely approach #1.

Having your clients talk to multiple GraphQL services (as in approach #2) entirely defeats the purpose of using GraphQL in the first place, which is to provide a schema over your entire application data to allow fetching it in a single roundtrip.

Having a shared nothing architecture might seem reasonable from the microservices perspective, but for your client-side code it is an absolute nightmare, because every time you change one of your microservices, you have to update all of your clients. You will definitely regret that.

GraphQL and microservices are a perfect fit, because GraphQL hides the fact that you have a microservice architecture from the clients. From a backend perspective, you want to split everything into microservices, but from a frontend perspective, you would like all your data to come from a single API. Using GraphQL is the best way I know of that lets you do both. It lets you split up your backend into microservices, while still providing a single API to all your application, and allowing joins across data from different services.

If you don't want to use REST for your microservices, you can of course have each of them have its own GraphQL API, but you should still have an API gateway. The reason people use API gateways is to make it more manageable to call microservices from client applications, not because it fits well into the microservices pattern.

Solution 2:

This article recommends approach #1. See the below image too, taken from the mentioned article: enter image description here

One of the main benefits of having everything behind a single endpoint is that data can be routed more effectively than if each request had its own service. While this is the often touted value of GraphQL, a reduction in complexity and service creep, the resultant data structure also allows data ownership to be extremely well defined, and clearly delineated.

Another benefit of adopting GraphQL is the fact that you can fundamentally assert greater control over the data loading process. Because the process for data loaders goes into its own endpoint, you can either honor the request partially, fully, or with caveats, and thereby control in an extremely granular way how data is transferred.

The following article explains these two benefits along with others very well: https://nordicapis.com/7-unique-benefits-of-using-graphql-in-microservices/

Solution 3:

As of mid 2019 the solution for the 1st Approach has now the name "Schema Federation" coined by the Apollo people (Previously this was often referred to as GraphQL stitching). They also propose the modules @apollo/federation and @apollo/gateway for this.

ADD: Please note that with Schema Federation you can't modify the schema at the gateway level. So for every bit you need in your schema, you need to have a separate service.

Solution 4:

For approach #2, in fact that's the way I choose, because it's much easier than maintaining the annoying API gateway manually. With this way you can develop your services independently. Make life much easier :P

There are some great tools to combine schemas into one, e.g. graphql-weaver and apollo's graphql-tools, I'm using graphql-weaver, it's easy to use and works great.

Solution 5:

I have been working with GraphQL and microservices

Based on my experience what works for me is a combination of both approaches depending on the functionality/usage, I will never have a single gateway as in approach 1, but neither a GraphQL for each microservice as approach 2.

For example based on the image of the answer from Enayat, what I would do in this case is to have 3 graph gateways (Not 5 as in the image)

enter image description here

  • App (Product, Basket, Shipping, Inventory, needed/linked to other services)

  • Payment

  • User

This way you need to put extra attention to the design of the needed/linked minimal data exposed from the depending services, like an auth token, userid, paymentid, payment status

In my experience for example, I have the "User" gateway, in that GraphQL I have the user queries/mutations, login, sign in, sign out, change password, recover email, confirm email, delete account, edit profile, upload picture, etc... this graph on it own is quite large!, it is separated because at the end the other services/gateways only cares about the resulting info like userid, name or token.

This way is easier to...

  • Scale/shutdown the different gateways nodes depending on they usage. (for example people might not always be editing their profile or paying... but searching products might be used more frequently).

  • Once a gateways matures, grows, usage is known or you have more expertise on the domain you can identify which are the part of the schema that could have they own gateway (... happened to me with a huge schema that interacts with git repositories, I separated the gateway that interact with a repository and I saw that the only input needed/linked info was... the folder path and expected branch)

  • The history of you repositories is more clear and you can have a repository/developer/team dedicated to a gateway and its involved microservices.

UPDATE:

I have a kubernetes cluster online that is using the same approach that I describe here with all the backends using GraphQL, all opensource, here is the main repository: https://github.com/vicjicaman/microservice-realm

This is an update to my answer because I think that it is better if the answer/approach is backed up code that is running and can be consulted/reviewed, I hope that this helps.