Skip to content

Injection

function

declare function useInjection<T>(query: GraphQLQuery<T>, params?: InjectParams): T;

Reads the data the server injected for the given query and subscribes the calling component to it.

Each query document is its own store: what this returns is a snapshot of it, and every component that got the same injection re-renders with a new snapshot when anything – an update() of that document, most of all – changes it.

The injection itself has already been prepared on the server side by the time this runs. We use static analysis to find the useInjection() calls in the code base and ship the data each view needs with the view.

The injection is the one of the view’s route: the location’s, or the one the nearest ViewRoute gives, for a view rendered anywhere but at its own URL.

Rules of hooks apply: call it at the top level of a view, unconditionally.

Parameters

query GraphQLQuery the data was injected for
params parameters, including __id where the same query is injected twice

Returns the injected value; a snapshot of it where it is a query document

type

type InjectParams = GraphQLParams & {
/**
* Used to disambiguate query results when the same named query gets used multiple times.
*/
__id?: string;
};

GraphQL types plus a declarative injection id that is declared with it to disambiguate injection but which is never sent as GraphQL parameter.

function

declare function data(injectionId: string): Injection | undefined;

Returns the converted injection for the given id, with the type and meta information it came with, or undefined while no view has read that id yet.

useInjection() is the normal way to read injected data; this is for the cases that need the type or the meta alongside the value. An id the page shipped but no view has claimed is undefined here and readable with injectionSource().

Parameters

injectionId injection id with its route, e.g. “grid/sorting/Q_FooList”

function

declare function injectionSource(injectionId: string): InjectionSource | undefined;

Returns the injection the server shipped under the given id, as received, or undefined if the page came with no injection for that id.

This is the raw GraphQL result: scalars are still in their wire format and query documents are still plain objects. inject() is what turns one of these into an Injection.

Parameters

injectionId injection id with its route, e.g. “grid/sorting/Q_FooList”

type

type Injection = {
value: any;
type: string;
meta: any;
};

Injection with converted value.

type

type InjectionSource = {
data: any;
type: string;
meta: any;
};

Injection as it comes in over the wire.