Picks the response media type of a request from its Accept types and what the endpoint
can produce, as PostgREST's content negotiation does.
offered/1 lists the kinds every endpoint answers (a table, or a function of any return
type, as recorded from PostgREST): JSON, vnd.pgrst.object, vnd.pgrst.array and CSV.
application/geo+json is never offered (PostGIS is not supported), so it
gets the ordinary PGRST107.
The plan type
application/vnd.pgrst.plan answers with the execution plan of the statement the request
would run for the type it is for= (application/json by default): offered only for a
for= this library serves (JSON, */*, object, array, CSV), never for a custom media
handler (text/xml, application/vnd.twkb).
negotiate/1 is provisional (it fails early when nothing at all is acceptable and never
looks at the gate); resolve/5 picks the final media type once the plan is known, with
the plan gate and the after_load/2 restrictions applied to the same choice, so a plan
type that did not pass the gate cannot be returned. A plan is offered only when:
- the plug option
plan_enabledallows it (the function is called only when a plan type is inAccept); - the
:explainoperation is allowed on every resource the request touches (PgRest.Authorization); options=analyzeof something that writes hasPrefer: tx=rollbackhonoured (tx_end: :commit_allow_override).
A request the gate refuses gets the PGRST107 of a plan that is not there, by any of the
three reasons and whatever else is wrong with the request, so a denial is no oracle.
Differences from PostgREST
- PostgREST also serves
application/geo+json; here it is always aPGRST107(no PostGIS). - For a request that reads a resource with
after_load/2(a PgRest concept), CSV andnulls=strippedare not offered: the callbacks need decoded JSON rows with their nulls, and skipping them could leak what they redact. A client that also accepts JSON gets JSON; PostgREST would send CSV. options=analyzeon a write (or a call that may write) needsPrefer: tx=rollback, sinceEXPLAIN ANALYZEruns it. PostgREST runs it and commits unless the client asks for a rollback, soAccept: application/vnd.pgrst.plan; options=analyzeonPOST /tinserts a row there and is aPGRST107here. Rollback does not undo sequences or effects outside the transaction, and a customchangeset/3runs for a plan request as for any write.- A plan is also refused when the authorization hook denies
:explain; PostgREST has no such operation. - A plan
for=a custom media handler (text/xmlon a scalar function, an aggregate such asapplication/vnd.twkb) is aPGRST107here; PostgREST explains the handler.
Summary
Functions
The first of accepts (by descending q) whose kind is in offered; */* is JSON and no
Accept types at all is JSON. A plan type is chosen only when :plan is offered, its
for= type is one of offered and plan_ok? (a function of the plan media type, default
always true) agrees. Else the 406 PGRST107 listing the types by canonical name (as
PostgREST does), in the sorted order. Option :stripped? (default true) is false to
refuse nulls=stripped types.
The media kinds an endpoint answers. Option :plan? (default false) adds :plan.
The final media type, once the plan is known: the one structural step that decides it.
Functions
@spec choose([PgRest.MediaType.t()], [PgRest.MediaType.kind()], keyword()) :: {:ok, PgRest.MediaType.t()} | {:error, PgRest.Error.t()}
The first of accepts (by descending q) whose kind is in offered; */* is JSON and no
Accept types at all is JSON. A plan type is chosen only when :plan is offered, its
for= type is one of offered and plan_ok? (a function of the plan media type, default
always true) agrees. Else the 406 PGRST107 listing the types by canonical name (as
PostgREST does), in the sorted order. Option :stripped? (default true) is false to
refuse nulls=stripped types.
@spec negotiate(PgRest.Request.t()) :: {:ok, PgRest.MediaType.t()} | {:error, PgRest.Error.t()}
The provisional media type, before planning: choose/3 over every kind, plan included, so a
request nothing can answer fails before anything is planned. It never looks at the plan
gate, which resolve/5 applies to the final choice, so the order of the errors does not
depend on it.
@spec offered(keyword()) :: [PgRest.MediaType.kind()]
The media kinds an endpoint answers. Option :plan? (default false) adds :plan.
@spec resolve( PgRest.Request.t(), PgRest.Plan.Read.t() | nil, Plug.Conn.t(), map(), keyword() ) :: {:ok, PgRest.MediaType.t()} | {:error, PgRest.Error.t()}
The final media type, once the plan is known: the one structural step that decides it.
plan is the plan the response reads (nil for a write without a representation). A plan
with an after_load/2 resource (callbacks work on decoded JSON rows) is not answered as CSV
or with the nulls stripped, and the plan type is offered only through the gate (see the
moduledoc). Both restrictions shape one choose/3, so a client that also accepts JSON falls
back to it and anything else gets the PGRST107 listing every type it asked for.
context carries the plan_enabled setting (false, true, or (conn, context -> boolean); only true counts) and the authorization module. Options: :targets (the
resources and function :explain is asked on, default Plan.Read.targets(plan)) and
:mutating? (default false: a write, or a call that may write).