QA Interview Hub · Knowledge base · Quizzes · Українська

API › Client-server and REST

REST and alternatives

REST is a set of Fielding's constraints (client-server, stateless, cacheable, uniform interface, layered system, code on demand), not a protocol. Most "REST APIs" implement only resources, methods and statuses. The alternatives (GraphQL, SOAP, gRPC, WebSocket) change how an error reaches the client: in GraphQL the errors in the body are checked, not just the HTTP status. The contract, validation, authorisation and error handling stay the same.

API · Client-server and REST

The project uses GraphQL, but your checks rest on status codes. Which of them still work?

On a new project the API is not REST but GraphQL. The test "unknown user" checks that the response is not 2xx and never fails, because the server answers 200 even on an execution error. To choose checks that make sense, you need to know what exactly is called REST, how the alternatives differ, and how that changes a test. It is also a typical interview question.

What REST is. REST was described by Roy Fielding in his 2000 dissertation as a set of constraints, not as a protocol or a standard. An API that follows them is called RESTful. In practice it looks like this: data are resources at URLs (/users/42), actions are set by HTTP methods (GET reads, POST creates, PUT and PATCH change, DELETE removes), the result is described by a status code, and the body is most often JSON. There are six constraints:

  • Client-server. Separation of concerns, as in the previous lesson.
  • Stateless. Each request contains everything needed to process it.
  • Cacheable. A response says whether it may be cached; this is how Cache-Control and ETag work.
  • Uniform interface. Resources have addresses, actions on them are the same (methods), messages describe themselves (content type, status), and a response may contain links to the next steps (hypermedia).
  • Layered system. A gateway, cache or proxy can sit between client and server, and the client does not notice.
  • Code on demand. The only optional one: the server may send code to run on the client.

In practice most "REST APIs" follow the middle of this list (resources, methods, codes) and barely use hypermedia. In the Richardson maturity model that is level 2 of four: level 0 is one endpoint for everything, level 1 separate resources, level 2 adds methods and statuses, level 3 hypermedia. A tester benefits from knowing this so as not to demand from an API something its contract never promised.

Alternatives and why they exist. Each solves a problem that hurts in REST:

ApproachWhat it isProblem it solvesWhat changes in tests
GraphQLOne endpoint, the client describes the fields it needsExtra data and many requests to different resourcesOften 200 even on an error (the newer GraphQL over HTTP specification recommends 4xx for request errors, but many servers don't follow it yet); check the errors array
SOAPXML messages, a strict WSDL contractA formal contract between enterprisesConformance to the XML schema; errors as a Fault element, often with 500
gRPCHTTP/2 and Protobuf, contract in .protoFast calls between servicesIts own status codes (NOT_FOUND, UNAVAILABLE), typed messages
WebSocketA persistent two-way connectionReal-time data: chat, quotesOrder and delivery of messages, reconnection

One error, two protocols

REST

GET /users/999999 returns 404 Not Found. The error is visible from the status alone.

GraphQL

POST /graphql returns 200 OK, and the body has data.user: null and an errors array.

Figure 1. The same "not found" looks different, so a status check works in only one of the cases.

Example: a check that never fails. The "unknown user" test carried over from a REST project:

test('unknown user', async ({ request }) => {
  const res = await request.post('/graphql', {
    data: { query: '{ user(id: "999999") { name } }' },
  });
  expect(res.ok()).toBeTruthy(); // green, but proves nothing
});
HTTP/1.1 200 OK
Content-Type: application/json

{"data":{"user":null},
 "errors":[{"message":"User not found","path":["user"],
            "extensions":{"code":"NOT_FOUND"}}]}

How to read it. The transport worked, so the HTTP status is 200, and the business error lies in the body. An "ok" check passes even when the request is broken. The fixed test looks at the body:

const body = await res.json();
expect(res.status()).toBe(200);
expect(body.data.user).toBeNull();
expect(body.errors?.[0].extensions.code).toBe('NOT_FOUND');

A code in extensions is more stable than a message text, which may be reworded. It is a convention of the particular server, not a requirement of the GraphQL specification, so the format is agreed with the team.

What stays the same. Whatever the style, the same things are checked: the contract (which fields and types), input validation, authorization (other people's data is unreachable), error handling and behavior on a repeat. What changes is how an error reaches the client, not the question of what should happen.

Limits and trade-offs.

  • REST constraints are a style, not a verifiable standard. Two "REST APIs" can differ a lot, so a tester compares behavior with the project's documentation, not with theory.
  • GraphQL still runs over HTTP, so authorization, limits and caching matter too, but caching POST is harder. A request with a partial failure can return both data and errors.
  • gRPC cannot be opened in a browser or checked with plain curl; you need Protobuf clients.

Common mistakes.

  • Checking only res.ok() in GraphQL: the test is green with any error in errors.
  • Calling any JSON over HTTP RESTful, even POST /doEverything: that is level 0.
  • Demanding hypermedia from an API that has no such contract.
  • Carrying the habit "404 means not found" into SOAP or gRPC: errors look different there.

Check yourself

A GraphQL request returned 200, and the body has data: { order: null } with no errors. Does it look like an error, and what do you check?

Answer

It can be fine: "no such order" is sometimes returned as null without an error. Compare with the project's contract: for an unknown id is null expected or an entry in errors, and write the assertion accordingly. The status 200 decides nothing here.

Why is it not right to describe "REST" as "an API that returns JSON"?

Answer

Because JSON is returned by GraphQL and any RPC style too. Signs of REST: resources at addresses, methods with defined semantics, status codes, stateless, cacheable. A single endpoint POST /api with JSON does not fit that description.

In an interview. Junior: REST is an API style where data are resources at URLs and actions are set by HTTP methods; it is stateless and usually works with JSON. Alternatives: GraphQL, SOAP, gRPC, WebSocket. Middle/Senior: REST is a set of Fielding's constraints, and most "REST APIs" implement only resources, methods and statuses. If the project uses GraphQL, I do not rely on the HTTP status but check errors in the body; in SOAP I compare the XML with the schema and the Fault; in WebSocket I check order and delivery. The principles are the same: contract, validation, authorization, errors.

More articles in this chapter