HTTP requester to call Graphql APIs
There is no MuleSoft certified connector available to call Graphql APIs in MuleSoft. In case if we come across a requirement to call Graphql endpoint in MuleSoft then we can leverage the existing HTTP Requestor to call Graphql APIs. Graphql calls are internally a POST call over HTTP.
For the demo we can call below graphql endpoint
Endpoint
https://swapi-graphql.netlify.app/.netlify/functions/index
Query payload
query Query($first: Int) {
allFilms (last: $first) {
films{
title
director
releaseDate
speciesConnection {
species {
name
classification
homeworld {
name
}
}
}
}
}
}
Graphql Variable
{
"first": 1
}
We can see the below postman call to see Graphql endpoint in action

To show Graphql endpoint call using HTTP POST
HTTP POST payload
{
"query": "query Query($first: Int) {\r\n allFilms (last: $first) {\r\n films{\r\n title\r\n director\r\n releaseDate\r\n speciesConnection {\r\n species {\r\n name\r\n classification\r\n homeworld {\r\n name\r\n }\r\n }\r\n }\r\n }\r\n }\r\n}\r\n",
"variables": {
"first": 1
}
}
POSTMAN call

Let’s do the same in MuleSoft
Create a project in Anypoint studio

Add the transformation to create the POST payload

Create the required HTTP requestor

Add logger to log the response

Now run the application and hit the http listener endpoint i.e., http://localhost:8081/graphql

We can see the logged message in Mule application

Sample Application – graphql-service
Sample POSTMAN collection – GraphQL.postman_collection.json
how did you convert the graphql query to the string for use in dataweave ?
Failing with unable to transform error for all $ references in dataweave. Can you please help