Business and Tech

Developing GraphQL in MuleSoft

Written by:
Published on March 15, 2021

Developing GraphQL in MuleSoft

GraphQL benefits and whether it trumps API or works alongside it is a discussion for another blog post. This blog post takes a very quick walk-through of the GraphQL router from MuleSoft – aimed at developers to quickly get things up and running and try it out for themselves.

GraphQL Mulesoft DevOps

Step 1: Install GraphQL Router


GraphQL router which started as a Hackathon entry for MuleSoft components is available at MuleSoft labs GitHub repository at https://github.com/mulesoft-labs/graphql-router

You need to have appropriate credentials to deploy to your Anypoint Platform in your .m2/settings.xml. Once downloaded, you can deploy the connector to your Exchange using the command:

mvn clean deploy

If the deployment is successful, you should be able to see the GraphQL router in your exchange like below:

GraphQL Mulesoft DevOps

Step 2: Configure a GraphQL demo project


There is an excellent article that covers MuleSoft and GraphQL available at https://dzone.com/articles/implementing-graphql-with-mulesoft

The linked project from it is a good place to get started with testing the GraphQL capabilities. The project is available at https://github.com/Jitendra85/MuleSoft-GraphQL-Demo

Download and open the project in the Anypoint Studio and update the pom.xml to point the project to the connector deployed to your organization in Step 1.

For example, see below:

GraphQL Mulesoft DevOps

Step 3: Run and test the application


Run the application on Anypoint Studio and test it from http://localhost:8081/graphiql

GraphQL Mulesoft DevOps

Try with input to query multiple APIs:

GraphQL Mulesoft DevOps

Step 4: Understand the workings


There are three REST API implementation end points in the project (think of it like API calls) namely:

  1. /accounts/{accountId}
  2. /accounts/{accountId}/balances
  3. /accounts/{accountId}/transactions

There are two important pieces of configuration required for GraphQL router: Schema component which looks like below:

GraphQL Mulesoft DevOps
  1. Graphql field resolver components (which are similar to APIKit resource resolvers)

There are three main resolvers added in the project namely:

  1. Account
  2. Transactions
  3. Balances
GraphQL Mulesoft DevOps

This resolver flows help map the incoming GraphQL query fields to different API resources/API implementations. In other words, you can configure these field resolver flows to implement any logic you want to return back to the caller (not just APIs, use any connector/compute and return values…)

Parting thoughts


GraphQL implementation provides simple to use and powerful way to query multiple related data (based on schema/entity) in one call – which could span multiple APIs/implementations on the backend. This is simple to see and implement in one application/API as shown above.

But when the requirement is to span multiple APIs with security (especially authorization) factors and for mutation (or update) scenarios, more comprehensive implementation/design considerations will be required. With respect to MuleSoft implementation, even though it hasn’t solved all the major issues, it does make it extremely easy to enable GraphQL for a select number of use cases extremely easily and well!

Thanks for reading!


End Note

Hope you found this article interesting, do drop us a comment below with your inputs, views, and opinions regarding Developing GraphQL in MuleSoft

Also, if you are interested in learning more about an exciting new code quality product that reduces your Mule project costs by 79%, follow the below link :

https://integralzone.com/iz-analyzer-mule-benefits/

Leave a Reply

Your email address will not be published. Required fields are marked *

Other Blog Posts

Other Blog Posts

MUnit Testing Done Right

Writing unit test cases is the key requirement of any MuleSoft implementation. But in many organizations, you end up skipping to write the unit test cases in MuleSoft applications – since it is considered as too time-consuming or is too complex to write. And even in cases where test cases are written, they are generic …

Read more

How to ship high-quality code faster

All software is built using code. Today, the software we’ve come to rely on in our everyday lives is ubiquitous across our society, at home, and at work. Without fail, the software and applications gaining the most adoption and stickiness, are the ones that rise to success through rapid innovation, and superior user experience. So, …

Read more

Mule Code Analysis with Azure Dev Ops

Azure Dev Ops is one of the leading Dev Ops tools of choice for most organizations trying to cover the entire application life cycle. One of the key requirements for Dev Ops tools is to integrate and provide a way to maintain MuleSoft code quality and security for the applications going through the Dev Ops …

Read more