@graphile-contrib/pg-relation-not-null
v0.1.0
Published
Publish PostGraphile forward relations non-null where the PostgreSQL catalog proves the target row exists.
Readme
@graphile-contrib/pg-relation-not-null
A PostGraphile v5 plugin that marks a forward relation (from a row to the row its key references) as non-nullable where the PostgreSQL catalog proves the target row is always there for the role the schema is built under.
PostGraphile publishes every forward relation nullable, because row-level
security may hide the target row from the reading
role.
Often the catalog shows it cannot, and assuming things never change, the client
is left handling a null that never comes.
[!WARNING] Benjie's note: beware that changing a relation from non-nullable to nullable is a breaking change (existing clients won't have needed null handling code, but nulls are now possible), so be sure that these rules aren't just true for your rows now, but will remain true for the lifetime of all of your existing clients. In particular note that adding or changing your RLS policies may change the nullability assertions you can make about a row as seen by a consumer.
Install
npm install @graphile-contrib/pg-relation-not-nullUse
import { PgRelationNotNullPlugin } from '@graphile-contrib/pg-relation-not-null'
export default {
plugins: [PgRelationNotNullPlugin()]
}The plugin takes no options and knows no other plugin. It reads the catalog at
introspection, as the role the schema is built under (the service's
pgSettingsForIntrospection included), and decides each relation in
PgRelationsPlugin's pgRelations_relation hook, which runs after every
introspection hook — so after every smart tag, and every tag another plugin declares
in the introspection, is in place. It needs no ordering.
The answer is written where a |@notNull tail of a @foreignKey smart tag lands,
relation.extensions.tags.notNull: a relation the plugin proves publishes exactly
as one carrying the tail, and the tail is then redundant.
The rule
A forward relation is published non-null only when all of these hold; where any one is not proved, the relation is left exactly as it was. The plugin only ever adds non-nullness, never removes it.
- Every key column is non-null in the introspection:
NOT NULLin the catalog, or declared non-null by a tag (@notNull, a@primaryKeycolumn). - The target's existence is guaranteed by real foreign keys. For a real
constraint, itself; for a declared key, every key its
@backedBynames, and at least one (below). Each is apg_constraintrow withcontype = 'f'that references this relation's target table by the same columns and is validated (convalidated), enforced (conenforced, PostgreSQL 18; every key of an earlier major is enforced) and not deferrable (condeferrable), so no transaction sees a referencing row without its target. The referencing relation is not a materialized view, whose stored row outlives the row its key referenced. - The target hides no row from the role. The target is a table (
relkindrorp) androw_security_active(target)is false for the role: row security is off on it, or the role bypasses it (BYPASSRLS, or it owns the table and row security is not forced). - The role may read the target.
has_column_privilege(target, column, 'SELECT')for every referenced column.
@backedBy
A key a smart tag declares — @foreignKey, which PgFakeConstraintsPlugin turns into
a constraint with no pg_constraint row of its own — carries no guarantee that its
target exists. Whoever declares it states which real foreign keys back it, with a tag
on the key after |, where a |@notNull tail goes:
@foreignKey (currency_id) references app.currency (id)|@backedBy app.tx.tx_currency_id_fkeyThe value is one or more <schema>.<table>.<constraint>, comma-separated — one per
row source where the declaring relation has several (a UNION's branches). It is a
pointer the plugin verifies, not a claim it believes: each named constraint is read in
pg_constraint, and a name that does not exist, is not a foreign key, references
another target, or does not hold by the rule proves nothing. What the tag cannot be
checked for is the declaration itself — that the declaring relation's column carries
the base column's value — which is the declarer's statement, as the @foreignKey is.
PostGraphile ignores the tag; a schema built with and without it is the same.
@graphile-contrib/view-constraints
writes @backedBy on every relation of a view it leads from real foreign keys; a
human writing @foreignKey by hand may write it too.
Known limits
- A relation of a polymorphic codec (
@interface) is not proved. Such a relation is published on the interface, and its implementations reach it through copies thatPgPolymorphismPluginpublishes nullable; an interface field cannot be non-null while an implementation's field is nullable, so the schema would not build. Proving these relations needs the copies to carry the answer too. - A target that is a view, a materialized view or anything else whose rows a predicate may filter is not proved, even where it filters nothing.
- The answer is for the role the schema is built under. A schema served under another
role is answered for the build role only; a role that may not read the target gets
a permission error from the database, not a
null. - The rule reads no plugin that adds conditions to a forward relation's own query; a
plugin that filters a to-one relation's target would make a proved relation wrong
(
@graphile-contrib/pg-omit-archivedfilters only connections and lists). - A backward relation is never touched.
Tests
yarn test runs the rule over hand-built catalog facts, with no database: each
condition fails a relation on its own. yarn test:lab builds a schema on a database of
its own, as a role that does not own it, without the plugin and with it, and compares
each relation's published type: non-null in every proved case, unchanged in each
unproved one. It needs a superuser connection (PGHOST, PGPORT, PGUSER), as it
creates that role and the database; CI runs it against PostgreSQL 15 to 18.
