Test Overview
This test sends a GraphQL query in the request body using Postman. It verifies that the response contains the expected data field with correct values.
This test sends a GraphQL query in the request body using Postman. It verifies that the response contains the expected data field with correct values.
pm.test("GraphQL response contains user data", function () { const jsonData = pm.response.json(); pm.expect(jsonData.data).to.have.property('user'); pm.expect(jsonData.data.user).to.have.property('id').that.is.a('string'); pm.expect(jsonData.data.user).to.have.property('name').that.is.a('string'); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Postman is ready with GraphQL request body set | — | PASS |
| 2 | Send POST request with GraphQL query in body | Request sent to GraphQL endpoint with body containing query { user { id name } } | — | PASS |
| 3 | Receive response from server | Response received with JSON body containing data.user object | — | PASS |
| 4 | Parse response JSON | Response body parsed into jsonData object | — | PASS |
| 5 | Check if 'data' property exists | jsonData contains 'data' key | pm.expect(jsonData.data).to.have.property('user') | PASS |
| 6 | Check if 'user' object has 'id' property of type string | jsonData.data.user has 'id' key with string value | pm.expect(jsonData.data.user).to.have.property('id').that.is.a('string') | PASS |
| 7 | Check if 'user' object has 'name' property of type string | jsonData.data.user has 'name' key with string value | pm.expect(jsonData.data.user).to.have.property('name').that.is.a('string') | PASS |
| 8 | Test ends with all assertions passed | All expected fields verified in response | — | PASS |
query field in a GraphQL body when testing with Postman?query fieldquery field contains the GraphQL query or mutation string that tells the API exactly what data to return or change.query field is specifically for the GraphQL operation.query = data request [OK]query means what data you want [OK]query with HTTP headersqueryquery empty or missingquery field with a valid query string.query string. The structures with "url" and "headers" are for other parts of the Postman request, not the body. { "variables": { "id": 1 } } only has variables without a query, which is invalid.query string [OK]query string [OK]{
"query": "query getUser($id: ID!) { user(id: $id) { name email } }",
"variables": { "id": "123" }
}name and email of a user with a specific id passed as a variable.id is set to "123", the API will return the name and email of user 123 if they exist.{
"query": "query getUser($id: ID!) { user(id: $id) { name email } }"
}$id but no variables field was sent, so the API complains about missing input.variables JSON object with the id value, e.g., { "id": "123" }, to satisfy the query requirement.variables field with the required id value -> Option A$id: ID!, $email: String!.Pass the test and this pattern moves unknown