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.
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:
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:
Step 3: Run and test the application
Run the application on Anypoint Studio and test it from http://localhost:8081/graphiql
Try with input to query multiple APIs:
Step 4: Understand the workings
There are three REST API implementation end points in the project (think of it like API calls) namely:
There are two important pieces of configuration required for GraphQL router: Schema component which looks like below:
- Graphql field resolver components (which are similar to APIKit resource resolvers)
There are three main resolvers added in the project namely:
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…)
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!
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 :