When and How to use GraphQL with Microservice Architecture

前端 未结 9 1137
遇见更好的自我
遇见更好的自我 2021-01-29 17:13

I\'m trying to understand where GraphQL is most suitable to use within a Microservice architecture.

There is some debate about having only 1 GraphQL schema that works as

9条回答
  •  感情败类
    2021-01-29 17:56

    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 nether 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)

    • 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.

提交回复
热议问题