GraphQL Remote Stitching Micro-services with NodeJS

David Novicki
codeburst
Published in
6 min readFeb 5, 2018

--

Micro-services have been gaining popularity because of their awesome architecture to modularize services and provide a movement away from monolithic applications. Micro-services are great but it does not take much to become a mess if architected incorrectly. It also does not help there has yet to be a ‘de facto standard’ for micro-services. Many of the common patterns of micro-services include using a network of restful APIs, each with their own functions and purpose, connected to a central front-facing API. For this example we will use GraphQL for awesome and flexible queries as well as a new feature called Remote Stitching (more on this later).

GraphQL and all it’s glory

GraphQL has really changed the restful API space for the better. It gives you one endpoint and a query language for services or your frontend to converse. This lets us build what is called a GraphQL schema that is essentially a rulebook for anyone querying it. GraphQL is endless and really neat but this tutorial is meant for those with some knowledge on how GraphQL already works. For those who need to play some catch up on GraphQL, please checkout http://graphql.org/learn/.

Remote Stitching with GraphQL

Remote stitching is just badass. It lets you set up a GraphQL instance that goes out and connects to other GraphQL instances and merge them all together to one central schema. One of the cool features is it requires very minimal changes each time you deploy a new micro-service, you can just add the url of the new service and restart your application so it fetches the new schema.

Getting Started

I have created a working project on github(https://github.com/supercycle91/graphql-microservices-example) for anyone to play around with. It is composed of three services of minimal code to show Proof on Concept. Basically, ‘main-api’ is the central GraphQL server that stitches ‘service-1’ and ‘service-2’. ‘service-1’ is a simple service to grab users from a data store and ‘service-2’ stands to return articles. These GraphQL schemas are very minimal as I did not want to ‘fuzz’ the important concepts of this tutorial.

GraphQL Playground

GraphQL Playground is an awesome IDE for querying your GraphQL instances. This application is important for us to ensure we are seeing that everything working as intended. Download(https://github.com/graphcool/graphql-playground).

Installation

Clone down the repository:

git clone https://github.com/supercycle91/graphql-microservices-example`

Install all dependencies for all three projects:

npm run install-all

Now that dependencies are installed, we need to start up our services. Now order is important here for Remote Stitching to work. It is important that we start all of our micro-services before we start our main API. This makes sure that each service is live for our main API to fetch their schemas on startup. For now we will start ‘service-1’ and ‘service-2’ and test them to show how they work independently.

To start, run the two commands below in two different terminals:

npm run start-service-1npm run start-service-2

If done correctly then you should have a total of two services running on localhost:8082 and localhost:8083.

Code

Let’s take a quick look at all three projects in this repository to get a fundamental understanding of their purpose and what the code is doing.

Service 1

This is our first service that supplies users. It consists of a server.js file and GraphQL schema.js file.

The server file below simply grabs the GraphQL schema and builds our GraphQL server.

server.js

The schema file below opens up a ‘user’ query which requires a param of ‘id’. This function will simply return a ‘user’ object from a data file I have supplied based on requested ‘id’.

schema.js

Service 2

This is our secondary service that supplies articles. It consists of a server.js file and GraphQL schema.js file just like our first service but for articles.

The server file below simply grabs the GraphQL schema and builds our GraphQL server.

server.js

This schema file below opens up a ‘article’ query which requires a param of ‘id’. This function will simply return a ‘article’ object from a data file I have supplied based on requested ‘id’.

schema.js

Main API

This is our main file for stitching together all of our remote schemas. It is a basic ExpressJS server that will grab introspect schemas from all urls that we provide in ‘endpoints’ array. Once all of these schemas are received, we do a merge into one large schema and start up our GraphQL server.

Below is server source code.

server.js

Below is our introspection.js file and may be a little confusing at first. This file is responsible for retrieving a remote schema. We call ‘getIntrospectSchema’ with each url we want it to stitch.

introspection.js

Testing our services

Surely we want to see each individual micro-service up and running and providing the task it was built to do. To do this we will open up GraphQL Playground and put in our first service url: http://localhost:8082/graphql.

Here I have provided a sample query and shown a working result.

Below is the visible schema for the ‘users’ microservice.

Once this has worked and we have played around with querying our service, we will need to test our second service, http://localhost:8083. Go ahead and add http://localhost:8083/graphql to GraphQL Playground and ensure you can see the schema and feel free to query an article.

Below is the visible query for this ‘articles’ micro-service.

Now we are up and running and we can run our central GraphQL API that will go out and stitch together both of these services together so we can interact with just one endpoint.

Running our Central API

The time has come to run our central API instance. This will go out and gave the schemas of the urls we have provided it and create one big schema.

Open and new terminal and start the main API:

npm run start-main

Now lets plugin our new central API url into GraphQL Playground, http://localhost:8081/graphql.

As you can see below, we can visibly see both the ‘users’ and ‘articles’ schema. We can even query them at the same time!

Below is the mapping of both of our services’ queries.

As you may notice we can now query both of our services with the same GraphQL endpoint. This is the beauty of Remote Stitching and gives us the ability to keep modularized services but have them all connect seamlessly for one central service.

Conclusion

So this is just the tip of the iceberg and is intended to give you a simple yet scaleable system to build on. Want a challenge and to take this a step further? Try using AWS Lambda’s to create your micro-service net with Remote Stitching. At Dermveda we focus in providing ‘always up’ micro-services to feel at ease with knowing our services will not go down. GraphQL Remote Stitching is very powerful has infinite possibilities so please check it out and build your own GraphQL micro-services.

Resources

If you are confused or want more information please checkout these resources:

--

--