Building a Simple Polyglot GraphQL Server
Thanks for visiting this page. Please note that this is not an introductory tutorial on GraphQL. If you are new to this topic, please take a look at the official documentation: https://graphql.org/
Here is a quick example showcasing how to build a Polyglot GraphQL Server availing Schema Stitching feature offered by Apollo GraphQL. In case you are new to Schema Stitching feature feature, please read the following doc https://www.apollographql.com/docs/graphql-tools/schema-stitching.html
This example uses two GraphQL services, each built using different programming languages:
- Employee Service: This is a a very simple GraphQL server built in Java. This service let you to query or mutate department objects along with list of employees belonging to each department. Please see the code here: https://github.com/jobinesh/oracle-code-one/tree/master/polyglot-demo/emp-service
- Now, suppose a customer comes with a requirements of combining these APIs(Queries) offered by two services in to one for realizing certain use case functionalities. For instance, while displaying department details on UI, instead of numeric locationId value, customers wants to see more detailed location information such as city, zip, country etc. As you might have noticed at the beginning, the location details are offered by Location Service in our implementation. So we need a way to compose Employee and Location services in to one for use by the GraphQL client. We use Schema Stitching feature to combine multiple schemas offered various services in to one. In our example, we will build a third service(HR Service), that uses Schema Stitching feature to combine GraphQL schemas from Employee and Location Service. The following diagram shows how the query is resolved at runtime when client issues a query that reads data from both Employee and Location service:
4. As a last step, we need to orchestrate and run all these individual services as a collection of cloud native micro services. We can easily achieve this by using the tools such as Docker and Kubernetes. The detailed instructions for setting up and running this example is available here: https://github.com/jobinesh/oracle-code-one/blob/master/polyglot-demo/README.md . Have fun !
The views expressed on this post are my own and do not necessarily reflect the views of my employer.