Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 30 ยท Advanced API Testing

Testing GraphQL, WebSocket and gRPC

Learn how GraphQL, WebSocket and gRPC differ from REST, how to send each kind of request in Postman, and how to test their unusual success and error rules.

Most of this tutorial has used REST over HTTP: one URL per resource, a method such as GET or POST, and a status code that says how it went. Other API styles exist because REST is not the best fit for every job. GraphQL lets a client ask for exactly the fields it wants. WebSocket keeps one connection open so either side can send messages at any time. gRPC calls remote functions with compact binary messages.

Postman can send all three, and the testing habits you already have still apply: send something, check what comes back. What changes is where success and failure live. A GraphQL error often arrives with status 200. A WebSocket has no single response, only a stream of messages. A gRPC call reports a gRPC status code instead of an HTTP one.

None of the real protocols can run on this page, and the Postman screens are described in words. Instead you get stand-ins: a miniature GraphQL-style executor that runs on Piston, and a fake socket that you can listen to and test.

Four API styles

REST Many URLs, status codes, JSON.
GraphQL One URL, a query picks the fields.
WebSocket One open connection, messages both ways.
gRPC Typed remote calls, binary messages.
1 / 2

GraphQL in Postman

A GraphQL API usually has one endpoint, typically a POST to a URL such as /graphql. The body carries a query (a text describing what you want), optional variables, and sometimes an operation name. In Postman, a request can be switched to the GraphQL body type: you type the query in one box and the variables as JSON in another. Postman can also fetch the API's schema by introspection, which gives you autocomplete for field names. Where exactly these controls sit depends on your version, so look for the GraphQL option in the request's Body tab.

Here is the request as plain JSON, which is what travels on the wire. It is read-only:

JSON
{
  "query": "query GetUser($id: Int!) { user(id: $id) { name posts { title } } }",
  "variables": { "id": 1 }
}

The query asks for a user's name and the title of each of their posts, and nothing else. Whatever fields the server could also return, the response contains only these. Using a variable ($id) rather than pasting the number into the query text keeps the query constant and the data separate, which is both cleaner and safer than building strings.

The rules that surprise REST testers

  • A failing query usually still returns status 200. The failure is reported inside the body, in an errors array. A test that checks only the status code will pass on a broken query.
  • Partial success is normal. A response can contain data for the fields that worked and errors for those that did not.
  • Everything is a POST to one URL, so you cannot tell requests apart in server logs by path or method. Name your operations.
  • A missing item is often null, not a 404. Asking for user(id: 99) may return { "data": { "user": null } }.

A solid GraphQL test therefore checks: status is 200, errors is absent (or is present with the expected message for a negative test), and data has the expected shape and values.

JavaScript
pm.test("Query succeeded without GraphQL errors", () => {
  pm.response.to.have.status(200);
  const body = pm.response.json();
  pm.expect(body.errors, JSON.stringify(body.errors)).to.be.undefined;
  pm.expect(body.data.user.name).to.be.a("string");
});

A stand-in GraphQL executor

The code below is a tiny GraphQL-style engine. It parses a query with fields, arguments, variables and nested selections, then resolves it against in-memory data. It is a teaching model, not the real GraphQL: it lacks fragments, types, validation and much more. It does show the core ideas: the response mirrors the query, unrequested fields are never returned, and errors travel beside data with status 200.

Node.js A miniature GraphQL-style executor

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Study the five outputs. The first returns only name and title, even though email and likes exist. The unknown user gives null data with no error. The unknown field gives partial data plus an errors entry. And the broken query still reports HTTP 200. These are exactly the cases a GraphQL test suite should cover.

WebSocket requests

A WebSocket starts as an HTTP request that asks to upgrade the connection. After that, the connection stays open and either side can send a message whenever it likes. Chat apps, live prices and notifications work this way.

In Postman you create a WebSocket request (a separate request type from the HTTP one), enter a ws:// or wss:// URL, press Connect, then type a message and press Send. A message log shows what you sent and what the server pushed back, each with a timestamp. You can also add headers and query parameters to the initial handshake, for example an authorization token.

Testing differs from REST in three ways:

  1. There is no single response. You decide how long to listen and which message counts as "the answer".
  2. Order and timing matter. The server may send several messages, or none, or send them late.
  3. You must close cleanly. A test that leaves connections open can exhaust the server's limits.

Because tests here cannot open a real socket, the stand-in below uses a fake one built on a plain event list. The helper waitForMessage is the real lesson: it waits for a message that matches a condition, and gives up after a timeout, which is the pattern any socket test needs.

JS Testing a socket: wait for the right message, or time out

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The last test fails on purpose, and its message shows what the socket did send (["error"]). Reporting the messages you saw alongside a timeout is a habit worth copying: "no pong arrived" is far less useful than "no pong arrived, but an error did".

gRPC requests

gRPC is a framework for calling functions on a remote server. You describe the service in a .proto file: its methods and the typed messages they take and return. Messages travel in a compact binary format over HTTP/2, so they are small and fast, but you cannot read them by eye. A service definition looks like this (read-only):

PROTO
syntax = "proto3";

service Bookstore {
  rpc GetBook (GetBookRequest) returns (Book);
  rpc ListBooks (ListBooksRequest) returns (stream Book);
}

message GetBookRequest { int32 id = 1; }
message ListBooksRequest { string author = 1; }
message Book {
  int32 id = 1;
  string title = 2;
}

In Postman you create a gRPC request, enter the server address, and either import the .proto file or let Postman discover the methods through server reflection if the server offers it. You then pick a method, write the request message as JSON (Postman converts it to binary), and invoke it. For streaming methods the response pane shows each streamed message as it arrives.

Results are reported with a gRPC status code, not an HTTP status. A few common ones: OK, INVALID_ARGUMENT, NOT_FOUND, UNAUTHENTICATED, PERMISSION_DENIED and DEADLINE_EXCEEDED. Test them the same way you test HTTP codes: a missing book should give NOT_FOUND, a bad request INVALID_ARGUMENT, and so on. Because the formal contract is the .proto file, contract testing (see Contract testing and OpenAPI) is partly built in: the types are enforced by the protocol.

Support for each protocol, and the exact buttons for it, change as Postman evolves. If a menu is not where this lesson says, search the request-type picker for GraphQL, WebSocket or gRPC. The testing ideas on this page do not depend on the interface.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A GraphQL query contains a mistake in a field name, and the server replies with HTTP 200 and an errors array. What should a good test do?

    GraphQL usually reports query errors inside the body while keeping HTTP 200, so a status-only test would miss the failure.
  2. 2. Why use GraphQL variables instead of putting values into the query text?

    A fixed query with a separate variables object avoids building query strings by hand and keeps the operation reusable.
  3. 3. What is the main difference when testing a WebSocket compared with a REST call?

    A socket delivers a stream of messages over time, so tests wait for a matching message, give up after a timeout and close the connection.
  4. 4. How does a gRPC call report that an item was not found?

    gRPC has its own status codes (OK, NOT_FOUND, INVALID_ARGUMENT and others), which tests check instead of HTTP statuses.

Summary

  • GraphQL, WebSocket and gRPC need the same testing mindset as REST, but success and failure show up in different places.
  • GraphQL uses one endpoint, a query plus variables, returns only the requested fields, and reports many failures as errors inside a 200 response.
  • Test GraphQL by checking the status, the absence (or expected presence) of errors, and the data shape.
  • A WebSocket is an open connection with a stream of messages: wait for the message you expect, set a timeout, and report what you did receive.
  • gRPC calls typed methods defined in a .proto file, uses binary messages, and reports gRPC status codes such as NOT_FOUND.
  • The runnable examples are stand-ins (a toy executor and a fake socket), not the real protocols.

Next: Postman Flows, collaboration and AI features.