Override GraphQL APIs Request Body using Modify Request Rule

Requestly
Requestly
Published in
Mar 14, 2024

--

If you need to override the request body for a GraphQL API. For scenarios where you need to switch models or modify fields in a query, use the programmatic option in the request body rule to selectively modify GraphQL requests.

Follow the below steps to override API Requests.

  1. Create a new Modify Request Rule.
  2. Use your API hostname & path as source conditions like — company.com/api/graphql
  3. Under Request Body select Programmatic
  4. We can filter and modify the request using the below code. Make sure you replace [modified query] with your query.
  5. Save the rule and test.
function modifyRequestBody(args) {
const { method, url, body, bodyAsJson } = args;
if(bodyAsJson[0].operationName = 'ListArticles'){
bodyAsJson[0].query = '[modified query]'
}
return body;
}

--

--

Requestly
Requestly

Setup redirects, modify headers, switch hosts, insert user scripts and much more. Install it on Chrome & Firefox and join our family of more than 100,000 devs!