Helper functions for operation processing, promise/observable conversion, and link validation.
Converts an observable to a promise, resolving with the first emitted value.
/**
* Converts an observable to a promise, resolving with the first emitted value
* @param observable - Observable to convert
* @returns Promise that resolves with the first emitted value
*/
function toPromise<R>(observable: Observable<R>): Promise<R>;Usage Example:
import { execute, toPromise } from "apollo-link";
const link = new HttpLink("/graphql");
const operation = { query: gql`query { users { id } }` };
// Convert observable result to promise
const result = await toPromise(execute(link, operation));
console.log(result.data);Alias for toPromise - use toPromise instead.
/**
* @deprecated Use toPromise instead
* Converts an observable to a promise
*/
function makePromise<R>(observable: Observable<R>): Promise<R>;Creates an observable from a promise.
/**
* Creates an observable from a promise
* @param promise - Promise to convert
* @returns Observable that emits the promise result
*/
function fromPromise<T>(promise: Promise<T>): Observable<T>;Usage Example:
import { fromPromise } from "apollo-link";
const dataPromise = fetch("/api/data").then(r => r.json());
const observable = fromPromise(dataPromise);
observable.subscribe({
next: data => console.log(data),
error: err => console.error(err)
});Creates an observable that immediately emits an error.
/**
* Creates an observable that immediately emits an error
* @param errorValue - Error value to emit
* @returns Observable that immediately errors
*/
function fromError<T>(errorValue: any): Observable<T>;Usage Example:
import { fromError } from "apollo-link";
// Create an error observable for testing or fallback scenarios
const errorObservable = fromError(new Error("Network unavailable"));
errorObservable.subscribe({
error: err => console.error("Expected error:", err.message)
});Creates an Operation object from a GraphQL request with additional context methods.
/**
* Creates an Operation object from a GraphQL request with context methods
* @param starting - Initial context object
* @param operation - GraphQL request to enhance
* @returns Operation with setContext, getContext, and toKey methods
*/
function createOperation(starting: any, operation: GraphQLRequest): Operation;Usage Example:
import { createOperation } from "apollo-link";
import gql from "graphql-tag";
const request = {
query: gql`query GetUser($id: ID!) { user(id: $id) { name } }`,
variables: { id: "123" },
operationName: "GetUser"
};
const operation = createOperation({ headers: {} }, request);
// Use operation methods
operation.setContext({ headers: { Authorization: "Bearer token" } });
console.log(operation.getContext()); // { headers: { Authorization: "Bearer token" } }
console.log(operation.toKey()); // Unique key for this operationExtracts the operation name from a GraphQL AST.
/**
* Extracts the operation name from a GraphQL AST
* @param query - GraphQL query AST
* @returns Operation name string or null
*/
function getOperationName(query: DocumentNode): string | null;Usage Example:
import { getOperationName } from "apollo-link";
import gql from "graphql-tag";
const query = gql`query GetUsers { users { id name } }`;
const operationName = getOperationName(query);
console.log(operationName); // "GetUsers"