GraphQL - Modify Request & Response

Getting Started
Installation
Setup
Inspect Traffic
HTTP Rules (Modify Traffic)
Session Book
Mock Server
API Client
File Server
Workspace
Public API
Guides
TroubleShooting
Subscription & Billing
Getting Started
Installation
Setup
Inspect Traffic
API Client
HTTP Rules (Modify Traffic)
Mock Server
File Server
Workspace
Public API
Guides
Session Book
Subscription & Billing
TroubleShooting
 

GraphQL Support

This page explains how you can alter GraphQL API requests and responses using Requestly.

Modifying GraphQL responses

In GraphQL, there is typically just one HTTP endpoint for all client interactions. And to specify which operation to execute, it is a common practice to pass operationName in the request payload.
For example,
POST /graphql { "operationName": "getUsers", "query": ` query getUsers { users { id email } } ` }
To modify response of a GraphQL request, you may create a Modify API Response rule and target operationName field in the request payload.
notion image
Select Resource Type as GraphQL API and in GraphQL Operation (Request Payload Filter), enter:
  • Key as operationName
  • value as getUsers
💡
Note: Key is the JSON key in the request payload. It also supports nested path. For instance, if operation name is available at path data.operationName instead of root, Key can be specified as data.operationName. Incase request body starts with an array like [{ "operationName": "value", ...}] , Key can be specified as 0.operationName .
 
If your GraphQL request does not specify operationName, you should select Resource Type as REST API and use Dynamic (JavaScript) mode to filter the request and override the response.
For example, in the below GraphQL request, there is no operationName field. The operation getUsers is instead specified in query field.
POST /graphql { "query": ` query getUsers { users { id email } } ` }
The JavaScript code would look like:
function modifyResponse(args) { const {url, response, requestData, responseJSON} = args; if (requestData.query?.includes("query getUsers")) { // return custom response from this query console.log("Requestly: Modifying response", { query: requestData.query }); return {...responseJSON, custom: true}; } // return original response return response; }
 

Modifying GraphQL requests

If you have a use-case to modify the GraphQL query or variables, you may create a Modify Request Body rule where the request body can be altered.