GraphQL Integration With Mule 4

Author: Shruti Puri

What is GraphQL?

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools. GraphQL was developed internally by Facebook in 2012 before being publicly released in 2015.

Why GraphQL?

  • Send a GraphQL query to your API and get exactly what you need, nothing more and nothing less. GraphQL queries always return predictable results. Apps using GraphQL are fast and stable because they control the data they get, not the server.
  • GraphQL queries access not just the properties of one resource but also smoothly follow references between them. While typical REST APIs require loading from multiple URLs, GraphQL APIs get all the data your app needs in a single request. Apps using GraphQL can be quick, even on slow mobile network connections.
  • GraphQL APIs are organized in terms of types and fields, not endpoints. Access the full capabilities of your data from a single endpoint. GraphQL uses types to ensure Apps only ask for what’s possible and provide clear and helpful errors. Apps can use types to avoid writing manual parsing code.
  • Add new fields and types to your GraphQL API without impacting existing queries. Aging fields can be deprecated and hidden from tools. Using a single evolving version, GraphQL APIs give apps continuous access to new features and encourage cleaner, more maintainable server code.
  • GraphQL creates a uniform API across your entire application without being limited by a specific storage engine. Write GraphQL APIs that leverage your existing data and code with GraphQL engines available in many languages. You provide functions for each field in the type system, and GraphQL calls them with optimal concurrency.

Use Case 1:

If REST API is returning 100 fields, your client needs only 10 fields out of those 100 fields. So, GraphQL can extract only those required 10 fields from REST API and return the response. So, that’s the scenario. Now, here we have Accounts REST API has the following fields:

Id, FirstName, LastName, address, phone, website, company, accountType

But according to the following use case Client 1 has requirements of 4 fields while Client 2 has requirements of 6 fields. With REST API architecture two endpoints need to be created or in mule using choice router taking client as queryparam fields can be set according to our requirement, but those changes will be at the code level. Later if the requirement changes entire code has to be changed but with graphql these changes don’t require any code-level changes.

Use Case 2:

We have a scenario where the Client requires aggregation of 3 REST API responses. GraphQL can route multiple requests from REST API, aggregating the response from API, and sending it back to the client.

This article assumes that you are familiar with Anypoint Studio, Connectors, Global elements.


  1. Anypoint Studio 7
  2. Postman

Go to

Copy clone URL from github repository.

Create a Folder in your local system: graphql-demo. Go to command prompt and type the following command:

git clone

Once the project is cloned in your local system, go to pom.xml and change the mule-modules-parent version from 1.0.0 to 1.1.3.

Go the the location where project is cloned and then go to command prompt and run command:

mvn clean install

Once the project is built successfully, Let’s get started!

Step 1: Create sample Mule project in Anypoint Studio.

Step 2: Add following dependency in pom.xml.

<dependency> <groupId></groupId>

After adding this dependency graphQL module will be visible under Mule Palette.

Step 3: Create dummy REST API for accounts, balances and transactions.

  • Go to src/main/mule(Flows).Right click New-> Mule Configuration File.
  • Add accounts.json, balances.json, transactions.json files under src/main/resources.
  • Create flows as shown below.
  • Read accounts.json file using Read component of File module stating File path as ${app.home}/accounts.json.
  • Read balances.json file using Read component of File module stating File path as ${app.home}/balances.json.
  • Read transactions.json file using Read component of File module stating File path as ${app.home}/transactions.json.

Step 4: Right click on src/main/resources New-> Folder give Folder Name as schema.

Step 5: Right click on schema Folder under src/main/resources New-> File.

Step 6: Create Graphql schema as shown below:

Step 7: Create flow as shown below:

Configure GraphQL config

  • Give Field name — account.
  • Configure Execute component.
  • Configure Request component.

Step 7: Hit graphql endpoint from postman. Select body-> GraphQl.

Now keep changing the fields according to requirement.

  • For use case 2: Edit the schema.graphqls file.

Step 8 : Add following flows. In GraphField Resolver give Field Name as balances.

  • Add following script in Execute component.
  • Configure Request Component as following:

Step 9: Similarly create flow for resolving transactions-give Field Name as transactions in Graph field resolver component.

Step 10: Hitting the request from postman with following query:






Scripting-Execute Module