Skip to content
mentoor

Topic 1 of 5 · 4 min read

Choosing REST, RPC or GraphQL styles

Goal: Spot the endpoints in a generated API that break HTTP semantics or the existing API's conventions, and correct them.

An endpoint is part of a contract that many clients depend on, so review a generated one for two things: does it use HTTP the way HTTP defines it, and does it look like its neighbours. A model writes a plausible endpoint from the prompt. It does not know that your other 40 endpoints use plural nouns, snake_case and a shared error shape.

Check yourself · 1 of 3

An assistant generates GET /orders/17/cancel. What is the main problem?

Choose one answer

Why it matters

Clients, proxies, caches and retry libraries all act on method and status. They assume GET changes nothing and can be repeated, and that PUT and DELETE can be repeated safely (RFC 9110 calls these safe and idempotent). A generated GET /users/42/delete breaks those assumptions: a link preview, a crawler or a prefetch can delete the user.

How to review

  1. Pick the style first. REST models nouns (/orders/17) with standard methods. RPC models verbs (POST /orders/17:cancel). GraphQL exposes one endpoint and a schema the client queries. Mixed styles are fine only when the project already mixes them on purpose. The right style is the one the API already uses.
  2. Check method semantics. Read: GET. Create: POST to the collection. Replace: PUT. Partial update: PATCH. Remove: DELETE. Use a custom action (POST /orders/17:cancel) when the operation is not a plain CRUD change.
  3. Check status codes. 201 with a Location header for a create, 204 for an empty success, 404 for a missing resource, 409 for a state conflict, 422 or 400 for invalid input. A 200 with {"success": false} in the body hides failures from every client and monitor.
  4. Match the neighbours. Compare names, casing, plural forms, pagination and error shapes with two existing endpoints. Google's AIP series is a good model of this kind of consistency.

Before and after

GET /api/getOrders?userId=42&cancel=17
-> 200 {"success": true, "data": [...]}

A read that also cancels, a verb in the path, a body-level success flag. Reviewed:

GET  /api/v1/orders?user_id=42        -> 200 {"items": [...]}
POST /api/v1/orders/17:cancel         -> 200 {"id": 17, "status": "cancelled"}

What goes wrong

  • State change on GET. Anything that can be prefetched or retried will trigger it.
  • Always 200. Errors become invisible to caches, alerts and client retry logic.
  • A new style in an old API. One GraphQL-ish endpoint inside a REST API doubles the client's learning cost.
  • Verbs everywhere by accident. /createUser and /updateUser mean the model copied a tutorial, not your API.

Examples

A delete hidden in a GET

GET /users/42/delete
-> 200 {"success": true}

A prefetching browser extension or a link scanner can now delete users. Fix: DELETE /users/42 returning 204. Add a test that GET on the resource leaves the row untouched.

Matching the neighbours

GET /v1/invoices?customer_id=9      (existing)
GET /v1/getPayments?custId=9        (generated)

The generated route has a verb, a different casing and a different parameter name. Ask for GET /v1/payments?customer_id=9 and the same list envelope as invoices.

Key points

  • Method and status are part of the contract: GET must not change state, and clients, caches and retries rely on that.
  • The best style for a new endpoint is the one the existing API already uses, not the one the model likes.
  • Use the status code that says what happened: 201 for create, 404 for missing, 409 for a conflict, never 200 with a failure flag.
  • Model operations as resources and standard methods first, and use a named action only when CRUD does not fit.
  • Compare every generated endpoint with two existing ones for naming, casing, pagination and error shape.

Common mistakes

  • Accepting a state-changing GET because the demo worked in a browser.
  • Returning 200 for every response and putting the error in the body.
  • Letting the model pick REST, RPC or GraphQL instead of following the existing API.
  • Reviewing the new route alone and never comparing it with an existing one.

Check yourself · 2 of 3

A generated handler returns 200 {"success": false, "error": "not found"} for a missing user. What should it return?

Choose one answer

Check yourself · 3 of 3

Your API is REST with plural nouns and snake_case. A model adds POST /createInvoice with customerId in the body. What is the best review comment?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic