Skip to main content

Search

Search problems, hooks, and pages

Back to About

REST & GraphQL API Playgrounds

built with: Client-only simulation · Vitest-covered

No real network calls, but every mechanic a real API would have — including the race condition a naive rate limiter would miss.

One dataset, two API paradigms

The REST and GraphQL playgrounds mirror the same resources (a zodiac-signs demo dataset, a custom resource builder, plus Payments/Files/Uploads) so the same call can be made both ways and compared side by side — deliberately for teaching REST vs GraphQL rather than picking one.

Nothing here calls a real network. Every "response" is generated client-side, but shaped like a real one: realistic headers, status codes, and a copyable request snippet for whatever call was just made.

The rate limiter has to be a ref, not state

A shared 10-requests-per-30-seconds limiter guards the playground the way a real API would. Written with useState, rapid clicks race: several requests can read the same stale count before any of their state updates commit, letting more than 10 through.

A ref sidesteps that because mutating it is synchronous — every call sees every prior call's result immediately, with no render in between to go stale.

// src/lib/rate-limiter.ts
// a ref, not useState: state would let concurrent clicks race on stale
// closures before a re-render, a ref mutates synchronously so every
// call sees prior calls' results.
const requestLog = useRef<number[]>([]);

Idempotency keys, not just a happy path

Retrying a POST to Payments with the same idempotency key and the same body replays the original response; the same key with a different body returns a 409, matching how real payment APIs guard against double-charging a retried request.

Payments and rate-limit history are deliberately lifted to the top-level playground view rather than owned by whichever tab is active, so switching from Payments to Files and back does not unmount and silently reset an in-progress idempotency history.

Important

Gotcha

Everything here is simulated on purpose, including auth: the JWT it issues is real, valid, and decodable, but it is signed and verified entirely in the browser, so it demonstrates the mechanics of token auth without ever proving anything to a server that does not exist.

Where it lives in the repo

  • src/components/api-playground/
  • src/components/graphql-playground/
  • src/lib/rate-limiter.ts
  • src/lib/idempotency.ts
  • src/lib/zodiac-translations.ts