I want to set null entity in the request body of graphQL

Hi Team,

I am working on graphQL APIs and writing regression scripts in postman.

I am giving sample of my project query in request body and variables json body below.

query CustomerDetails($customerId: Int!, $accountNumbers: [String!], $customerName: String) {
  customer(id: $customerId
           accountNumber: $accountNumbers
           customerName: $customerName
  ) {
    id
    name: customerName
    accountNumbers: accountNumbers
    loanTypes
    
  }
}

GraphQL variables body is:

{
  "customerId": {{id}},
  "accountNumbers": ["{{accnum}}"],
  "customerName": "{{name}}"
}

Here I am passing data from csv file. In negative testcases I want to set accountNumbers: null , customerName: null in graphQl variables. But here when data driven from csv file for failure testcases it is not accepting null and null is taking as a String. Is that possible we can directly set data to request body.

Kindly, please help me.

Here I want to replace entire array with null in the graphQL variables. Is there any chance that if we are getting null from the csv data file we can directly set the value in the request query.

Perhaps you can update the request body to something like:

{
    "accountNumbers": {{accnum}}
}

And then use a Before query script to read in the CSV data and handle accnum like:

let data = JSON.parse(pm.iterationData.get("accountNumber"))
pm.collectionVariables.set("accnum", data)

The problem is that if you have null in the CSV file, it will be treated as a string instead of actually null.

You will need to check if the relevant element is “null” in the CSV file, and then overwrite that in the value when you set it.

I’ve just had a look at what is available on a GraphQL request.

You can get access to the GraphQL variables using pm.request.variables.

image

image

image

Therefore, you should be able to update the variable using the following method or similar.

if (data.accountNumbers === "null") {
    let obj =
    {
        "customerId": null,
        "accountNumbers": null,
        "customerName": null
    }
    pm.request.variables = JSON.stringify(obj);
}