Unlike REST APIs, GraphQL APIs provide a flexible approach to querying and mutating data through a single endpoint. They allow clients to specify exactly what data is needed, reducing over-fetching and under-fetching issues common with REST APIs. GraphQL APIs can be modified to customize requests and responses for testing and debugging. This guide provides detailed steps to achieve this using Requestly.
Modifying GraphQL Responses
In GraphQL, client interactions are typically managed through a single HTTP endpoint. The specific operation to execute is often indicated by the operationName
field in the request payload.
Example Request
POST <https://www.example.com/graphqlapi>
{
"operationName": "getUsers",
"query": `
query getUsers {
users {
id
email
}
}
`
}
To modify the response of a GraphQL API:
Steps to Modify API Response
Open HTTP Rules
Navigate to the HTTP Rules section in the web or desktop app.
Create a New Rule
Click on "+ New Rule" and select Modify API Response.
Configure Resource Type
Choose "GraphQL API" as the Resource Type.
Set GraphQL Operation Filter
In the GraphQL Operation (Request Payload Filter) section:
-
Enter Key as
operationName
. -
Enter Value as the operation name you want to target (e.g.,
getUsers
).
Customize Response
Define the new response you want to serve for the specified operation.
Save and Activate
Click Save to store the rule and toggle it ON to apply the changes.
Test the Rule
Validate the rule by sending a request matching your conditions and verifying the modified response.
For additional details, visit the Modify API Response Rule Page.
Notes
-
The Key represents the JSON key in the request payload and supports nested paths. For example:
-
If
operationName
is located atdata.operationName
, specifydata.operationName
. -
For array payloads, such as
[{"operationName": "value"}]
, use0.operationName
as the key.
-
-
If the request does not include
operationName
, select Resource Type as "REST API" and use Dynamic (JavaScript) Mode to filter and modify the response.
Example Without operationName
In cases where the operation is specified within the query
field:
POST /graphql
{
"query": `
query getUsers {
users {
id
email
}
}
`
}
You can modify the response using a JavaScript snippet:
function modifyResponse(args) {
const { url, response, requestData, responseJSON } = args;
if (requestData.query?.includes("query getUsers")) {
console.log("Requestly: Modifying response", { query: requestData.query });
return { ...responseJSON, custom: true };
}
return response;
}
Modifying GraphQL Requests
If you need to alter the GraphQL query or its variables, use the Modify Request Body rule to change the request payload dynamically.
Steps to Modify Request Body
Open HTTP Rules
Navigate to the HTTP Rules tab in the web or desktop app.
Create a New Rule
Click on "+ New Rule" and select Modify Request Body.
Set Source Condition
Specify the conditions for when this rule should apply, such as URL, Host, or Path. You can use Regex, Contains, Wildcard, or Equals as operators.
Define the Modification Logic
For Dynamic JavaScript: Write a JavaScript function to modify the request body.
Example:
function modifyRequestBody(args) {
const { method, url, bodyAsJson } = args;
let newBody = bodyAsJson;
if (method === "POST") {
newBody.query = `query modifyUsers { users { name } }`;
}
return newBody;
}
Save and Activate
Click Save to store the rule and toggle it ON to apply the changes.
Test the Rule
Validate the rule by sending a request matching your conditions and verifying the modified request body.
For additional details, visit the Modify Request Rule Page.