> ## Documentation Index
> Fetch the complete documentation index at: https://docs.requestly.com/llms.txt
> Use this file to discover all available pages before exploring further.

# GraphQL Support

> Learn to modify GraphQL API requests and responses using Requestly Interceptor to debug and test GraphQL APIs.

***

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 Interceptor.

## 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**

```bash theme={null}
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

<Steps>
  <Step title="Open HTTP Rules">
    Navigate to the HTTP Rules section in the web or desktop app.

    <img src="https://mintcdn.com/requestly/Z9psaKI2OUaqbNe1/images/graphql-modify-request-response/c2dbce79-cafa-4f4a-8fb3-3054f2027f85.png?fit=max&auto=format&n=Z9psaKI2OUaqbNe1&q=85&s=08a05c45c50ca98f60e4e82171f254c6" align="center" fullwidth="false" width="2400" height="1332" data-path="images/graphql-modify-request-response/c2dbce79-cafa-4f4a-8fb3-3054f2027f85.png" />
  </Step>

  <Step title="Create a New Rule">
    Click on "+ New Rule" and select **Modify API Response**.

    <img src="https://mintcdn.com/requestly/Z9psaKI2OUaqbNe1/images/graphql-modify-request-response/4f54d5ec-8d60-4edf-a0e0-45488977bcbc.png?fit=max&auto=format&n=Z9psaKI2OUaqbNe1&q=85&s=aa1aebed6ee7f167d065b594212eef8b" align="center" fullwidth="false" width="2400" height="1332" data-path="images/graphql-modify-request-response/4f54d5ec-8d60-4edf-a0e0-45488977bcbc.png" />
  </Step>

  <Step title="Configure Resource Type">
    Choose "GraphQL API" as the Resource Type.

    <img src="https://mintcdn.com/requestly/Z9psaKI2OUaqbNe1/images/graphql-modify-request-response/94125ed8-54e8-4a23-b0c4-83bf87af50f9.png?fit=max&auto=format&n=Z9psaKI2OUaqbNe1&q=85&s=f984b7af3849de20570ab83163455519" align="center" fullwidth="false" width="2400" height="1276" data-path="images/graphql-modify-request-response/94125ed8-54e8-4a23-b0c4-83bf87af50f9.png" />
  </Step>

  <Step title="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`).

    <img src="https://mintcdn.com/requestly/Z9psaKI2OUaqbNe1/images/graphql-modify-request-response/f308ed77-6708-4e56-9495-6cb08bd93f1a.png?fit=max&auto=format&n=Z9psaKI2OUaqbNe1&q=85&s=34fe548ccec98faa5b77cff193d395cc" align="center" fullwidth="false" width="2400" height="1276" data-path="images/graphql-modify-request-response/f308ed77-6708-4e56-9495-6cb08bd93f1a.png" />
  </Step>

  <Step title="Customize Response">
    Define the new response you want to serve for the specified operation.

    <img src="https://mintcdn.com/requestly/Z9psaKI2OUaqbNe1/images/graphql-modify-request-response/ade413a6-caeb-4f6a-b065-697ecd83ed69.png?fit=max&auto=format&n=Z9psaKI2OUaqbNe1&q=85&s=e9bc9de97fbacf7dd1cfae8519065416" align="center" fullwidth="false" width="2400" height="1276" data-path="images/graphql-modify-request-response/ade413a6-caeb-4f6a-b065-697ecd83ed69.png" />
  </Step>

  <Step title="Save and Activate">
    Click **Save** to store the rule and toggle it ON to apply the changes.

    <img src="https://mintcdn.com/requestly/Z9psaKI2OUaqbNe1/images/graphql-modify-request-response/7ef71084-b8cb-4e54-9ccb-633d11c77db1.png?fit=max&auto=format&n=Z9psaKI2OUaqbNe1&q=85&s=90ae6197c820ff51f76fafd6d1c0c890" align="center" fullwidth="false" width="2400" height="1332" data-path="images/graphql-modify-request-response/7ef71084-b8cb-4e54-9ccb-633d11c77db1.png" />
  </Step>

  <Step title="Test the Rule">
    Validate the rule by sending a request matching your conditions and verifying the modified response.

    <img src="https://mintcdn.com/requestly/Z9psaKI2OUaqbNe1/images/graphql-modify-request-response/a1e80ffd-39ed-4fa0-baca-656a432fa90d.png?fit=max&auto=format&n=Z9psaKI2OUaqbNe1&q=85&s=db5cd1fa94c4aae5be7065661bae6a59" align="center" fullwidth="false" width="2400" height="1404" data-path="images/graphql-modify-request-response/a1e80ffd-39ed-4fa0-baca-656a432fa90d.png" />
  </Step>
</Steps>

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 at `data.operationName`, specify `data.operationName`.

  * For array payloads, such as `[{"operationName": "value"}]`, use `0.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:

```bash theme={null}
POST /graphql
{
  "query": `
    query getUsers {
      users {
        id
        email
      }
    }
  `
}
```

You can modify the response using a JavaScript snippet:

```jsx theme={null}
function modifyResponse(args) {
  const { url, response, requestData, responseJSON } = args;

  if (requestData.query?.includes("query getUsers")) {
    console.log("Requestly Interceptor: 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

<Steps>
  <Step title="Open HTTP Rules">
    Navigate to the HTTP Rules tab in the web or desktop app.

    <img src="https://mintcdn.com/requestly/Z9psaKI2OUaqbNe1/images/graphql-modify-request-response/5bb28e10-7243-4eaa-8618-229476ccf604.png?fit=max&auto=format&n=Z9psaKI2OUaqbNe1&q=85&s=3667e1919da6fddde189071c2e2df008" align="center" fullwidth="false" width="2400" height="1332" data-path="images/graphql-modify-request-response/5bb28e10-7243-4eaa-8618-229476ccf604.png" />
  </Step>

  <Step title="Create a New Rule">
    Click on "+ New Rule" and select **Modify Request Body**.

    <img src="https://mintcdn.com/requestly/Z9psaKI2OUaqbNe1/images/graphql-modify-request-response/77286f4f-e8cc-4267-b18a-a715c6ae16f1.png?fit=max&auto=format&n=Z9psaKI2OUaqbNe1&q=85&s=777c0dc3690d2bdcabfc337695d21a8c" align="center" fullwidth="false" width="2400" height="1332" data-path="images/graphql-modify-request-response/77286f4f-e8cc-4267-b18a-a715c6ae16f1.png" />
  </Step>

  <Step title="Configure Resource Type">
    Choose "GraphQL API" as the Resource Type.

    <img src="https://mintcdn.com/requestly/Z9psaKI2OUaqbNe1/images/graphql-modify-request-response/112f8c75-4bb0-4e3d-91a6-c78c9f0434c5.png?fit=max&auto=format&n=Z9psaKI2OUaqbNe1&q=85&s=e42e2798093de4f84d58bea2fc774481" align="center" fullwidth="false" width="2250" height="1364" data-path="images/graphql-modify-request-response/112f8c75-4bb0-4e3d-91a6-c78c9f0434c5.png" />
  </Step>

  <Step title="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`).

    <img src="https://mintcdn.com/requestly/Z9psaKI2OUaqbNe1/images/graphql-modify-request-response/dde4dd52-6ffd-4c94-86d9-171a88d5a88d.png?fit=max&auto=format&n=Z9psaKI2OUaqbNe1&q=85&s=407b99c39b8a6edbdf3b5899e584ca1b" align="center" fullwidth="false" width="2236" height="1278" data-path="images/graphql-modify-request-response/dde4dd52-6ffd-4c94-86d9-171a88d5a88d.png" />
  </Step>

  <Step title="Customize Request">
    Define the new Request you want to serve for the specified operation

    <img src="https://mintcdn.com/requestly/Z9psaKI2OUaqbNe1/images/graphql-modify-request-response/027a3c69-b67f-4608-a5e5-7ae5e93fe875.png?fit=max&auto=format&n=Z9psaKI2OUaqbNe1&q=85&s=9c8a0d8a51da3f3df45c18c441695b03" align="center" fullwidth="false" width="2236" height="1278" data-path="images/graphql-modify-request-response/027a3c69-b67f-4608-a5e5-7ae5e93fe875.png" />
  </Step>

  <Step title="Save and Activate">
    Click **Save** to store the rule and toggle it ON to apply the changes

    <img src="https://mintcdn.com/requestly/Z9psaKI2OUaqbNe1/images/graphql-modify-request-response/6751b63e-802b-4efa-954c-124dbc162c86.png?fit=max&auto=format&n=Z9psaKI2OUaqbNe1&q=85&s=be57738da200a139b66ed5433bcb9c3e" align="center" fullwidth="false" width="2250" height="1364" data-path="images/graphql-modify-request-response/6751b63e-802b-4efa-954c-124dbc162c86.png" />
  </Step>

  <Step title="Test the Rule">
    Validate the rule by sending a request matching your conditions and verifying the modified response.

    <img src="https://mintcdn.com/requestly/Z9psaKI2OUaqbNe1/images/graphql-modify-request-response/fa513012-32fe-4203-848f-3b4edec6abda.png?fit=max&auto=format&n=Z9psaKI2OUaqbNe1&q=85&s=0c9f3ff0a0d628bb32ccca37d76d4b2c" align="center" fullwidth="false" width="2236" height="1278" data-path="images/graphql-modify-request-response/fa513012-32fe-4203-848f-3b4edec6abda.png" />
  </Step>
</Steps>

For additional details, visit the Modify Request Rule Page.
