Send a GraphQL query using Postman and verify the response
Preconditions (2)
✅ Expected Result: The response body contains the user object with id '1' and fields 'id', 'name', and 'email' with valid values
pm.test('Status code is 200', () => { pm.response.to.have.status(200); }); const responseJson = pm.response.json(); pm.test('User id is 1', () => { pm.expect(responseJson).to.have.nested.property('data.user.id', '1'); }); pm.test('User name is a non-empty string', () => { pm.expect(responseJson.data.user.name).to.be.a('string').and.not.empty; }); pm.test('User email is valid format', () => { const email = responseJson.data.user.email; pm.expect(email).to.be.a('string').and.match(/^\S+@\S+\.\S+$/); });
This test script runs after the GraphQL POST request in Postman.
First, it checks the HTTP status code is 200, meaning the request succeeded.
Then it parses the JSON response body safely.
Next, it verifies the user id is exactly '1' using nested property access.
It confirms the user name is a string and not empty, ensuring valid data.
Finally, it checks the user email matches a simple email pattern to confirm format correctness.
Using pm.expect and pm.test keeps tests clear and easy to maintain.
Now add data-driven testing with 3 different user IDs (e.g., '1', '2', '3') to verify the API returns correct user data for each.
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