Skip to main content

Lifecycle of a Request

A request goes down through a Resource to your data, and comes back up as a serialized response.

RequestEndpointBackendJSON:APIResourcebase_scope + filters,sorts, paginationresolve(scope)to your ModelsserializeAdapterresponse

Graphiti is the highlighted part: the Resource, and the JSON:API it renders. The Endpoint is your Rails controller, which handles routing, response codes and MIME types. The Backend is yours too.

PieceWhat it does
EndpointYour controller. Graphiti registers its path and actions, which drives link generation and endpoint validation, and lets you vary a Resource's behavior per route.
ResourceTurns request params into a scope, resolves that scope into Models, and serializes them on the way back out.
AdapterReusable glue between a Resource and a Backend. Defaults to Graphiti::Adapters::ActiveRecord.
BackendWhatever you query: a database, a search index, an HTTP service.
ModelWhat you return and serialize. With ActiveRecord, the same object as the Backend.

The graph

Resources connect to other Resources:

  • Sideloading: fetch an employee, her positions, and those positions' departments in one request
  • Sideposting: save an employee and her positions in one request
  • Links: a URL to lazy-load positions in a separate request

Query logic written for one Resource applies at every level of that graph, so you can ask for an employee and her last three positions ordered by created_at. That's deep querying.