Skip to main content

assertNotNull

Guard a step so that null values become execution errors. The returned step shares the same value as the original when it resolves to a non-null result. If a value of the wrapped step resolves to null, Grafast raises a SafeError with the message you provide, and any dependant steps are thereby prevented from running for this value.

Declarative flow

Only dependants of the returned step see the assertion. Other branches of the plan continue executing.

Example

const spec = specFromNodeId(userHandler, $id);
// `spec.id` will be a step resolving to the database ID if `$id` is a valid
// user ID, or `null` otherwise. Throw if `null`.
const $databaseId = assertNotNull(spec.id, "Expected a User ID");
const $user = loadOne($databaseId, batchGetUserById);

Trapping

Use trap when you need to turn the resulting error back into data (such as a null, or perhaps the error as a data value).

Plan diagrams

Internally, assertNotNull currently builds a __FlagStep, but that step is usually converted into a dependency constraint when another step depends on it. Thus in plan diagrams it's more common to see it on the dependency edge: instead of a visible node, you will see the dependency arrow annotated with labels such as rejectNull.

Advanced

Optionally pass { if: $cond } to toggle the assertion dynamically. When the condition resolves to false, assertNotNull behaves like a pass-through. See inhibitOnNull for a similar example.