Need guidance for Debugging the response data

if (res.status === 200) {
console.log(JSON.stringify(res.body));
const body = JSON.parse(res.body);
const issue = body.data.repository.issues.edges[0].node;
console.log(issue.id, issue.number, issue.title);
}

I didn’t understand in lines 3-5. Please guide me where I can read about this or please explain for me thank!
source:https://k6.io/blog/load-testing-graphql-with-k6/

Hi @Pamorn

Thanks for reaching out! I’m not a GraphQL specialist in any ways, but I’ll give my best to try and support you.

My understanding of your question is that you’d like to know what the operations: JSON.parse(res.body) and body.data.repository.issues.edges[0].node do. Correct?

Assuming that’s the case, and because we were not specific as to what you didn’t understand, I’ll assume no prior knowledge:

  • const body = JSON.parse(res.body) parses the body of the HTTP response as JSON. That means that the body of the HTTP response is treated and interpreted as if it were JSON. Once the JSON.parse(res.body) operation is complete, its result, a JavaScript object matching the JSON document that was received in the body of the HTTP response’s body, is constantly bound to the name body.
  • const issue = body.data.repository.issues.edges[0].node extracts a value from the body object we’ve created during the previous step and binds it in a constant manner to the issue name. Looking at the GraphQL example you pointed from our blog, and being somewhat familiar with the GitHub API, I deduce that the actual body structure looks something like this:
{
  data: {
    repository: {
      issues: {
        edges: [
          {
            node: ...
          },
          // some other properties...
        ],
        // some other properties...
      },
      // some other properties...
    },
    // some other properties...
  } 
}

Thus, the operation body.data.respository.issues.edges[0].node returns the nested node properties of the first object in the edges property of the issues property of the repository property of the data object (that’s a mouthful :slight_smile: ).

So in summary, what the operation you outlined does is: if the result of the request (I assume it’s a GET operation) is successful, parse its JSON body as a JS object (GraphQL is JSON based if I’m not mistaken), bind it to the constant body name, extract the nested node property from it, and bind it to the constant issue name.

I hope it makes sense now. Let me know if that was helpful.

1 Like

yes! This helps me :heart_eyes:, thank you.