GraphQL modules
Importing a .gql or .graphql file produces a module with a single default export.
F Default export
The default export is a factory function. You call it (optionally with variables) and it returns an object with query, subscribe, and unsubscribe methods bound to that query.
(vars?: Record<string, any>) => { query, subscribe, unsubscribe }
| Parameter | Type | Description |
|---|---|---|
vars | Record<string, any> | Optional. Variables to interpolate into the query — $varName tokens are replaced with their values. Strings are quoted; numbers and booleans are inserted as-is. |
F query
query(): Promise<{ data?: any; errors?: any[] }>
Executes a one-shot GraphQL query. Resolves to the full GraphQL envelope. See yeet.graph.query.
F subscribe
subscribe(cb: (data: any) => void): string
| Parameter | Type | Description |
|---|---|---|
cb | (data: any) => void | Called each time new data arrives from the subscription. |
Returns a ticket string. See yeet.graph.subscribe.
F unsubscribe
unsubscribe(ticket: string): Promise<boolean>
| Parameter | Type | Description |
|---|---|---|
ticket | string | The ticket returned by subscribe. |
Returns true if the subscription was found and removed, false otherwise. See yeet.graph.unsubscribe.
Example
import getStatus from './status.graphql';
// One-shot query
const { query } = getStatus({ nodeId: 42 });
const { data, errors } = await query();
// Live subscription
const { subscribe, unsubscribe } = getStatus();
const ticket = subscribe((data) => console.log(data));
// Cancel later
await unsubscribe(ticket);