JSByte: 3 ways to adopt GraphQL in a REST microservices architecture
There are three main ways to integrate with GraphQL in an existing microservices architecture:
Build a GraphQL wrapper around REST microservices: This is a very common technique used in enterprises that have existing downstream REST microservices. A GraphQL layer is built on top of existing REST API. Further, there's two ways of implementing the GraphQL wrapper -
The GraphQL layer acts as a pure orchestration layer. No business logic is added to the orchestration layer. The role of this layer is to wrap existing services and surface them as GraphQL endpoint. This layer is usually very lightweight.
Build a GraphQL layer around new logic and existing microservices. In this technique, existing REST services are wrapped in a GraphQL resolver, and new logic is put in new GraphQL resolvers. All new logic is written as GraphQL mutations, whereas existing microservices can be orchestrated.
Build a REST wrapper around GraphQL services: This technique is useful when existing clients have already integrated with REST, and there is no control to change the integration. In this method, the downstream service is a GraphQL layer where the business logic resides. A REST layer is built on top of GraphQL layer to provide compatibility to clients that have an existing integration. The benefit of this technique is that the power of GraphQL seeps through to the client without having to reintegrate. New clients can choose to integrate with GraphQL or REST API.
REST and GraphQL APIs co-exist: In this method, existing REST APIs are used in conjunction with GraphQL APIs. A good example of this is when authentication and authorization logic lives in an existing REST API that cannot be migrated easily. The client needs to decide when they need to call GraphQL vs REST service. In this type of integration, often the client makes both REST and GraphQL API calls.
On a personal note
Last week I gave a talk at CityJS conference, and talked in detail about adoption of GraphQL in an enterprise and how to begin introducing GraphQL in a REST microservices architecture. If you are interested, you can find the slides here
Peace! ✌🏼